| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Web socket status codes used when closing a web socket connection. | 6 * Web socket status codes used when closing a web socket connection. |
| 7 */ | 7 */ |
| 8 interface WebSocketStatus { | 8 abstract class WebSocketStatus { |
| 9 static const int NORMAL_CLOSURE = 1000; | 9 static const int NORMAL_CLOSURE = 1000; |
| 10 static const int GOING_AWAY = 1001; | 10 static const int GOING_AWAY = 1001; |
| 11 static const int PROTOCOL_ERROR = 1002; | 11 static const int PROTOCOL_ERROR = 1002; |
| 12 static const int UNSUPPORTED_DATA = 1003; | 12 static const int UNSUPPORTED_DATA = 1003; |
| 13 static const int RESERVED_1004 = 1004; | 13 static const int RESERVED_1004 = 1004; |
| 14 static const int NO_STATUS_RECEIVED = 1005; | 14 static const int NO_STATUS_RECEIVED = 1005; |
| 15 static const int ABNORMAL_CLOSURE = 1006; | 15 static const int ABNORMAL_CLOSURE = 1006; |
| 16 static const int INVALID_FRAME_PAYLOAD_DATA = 1007; | 16 static const int INVALID_FRAME_PAYLOAD_DATA = 1007; |
| 17 static const int POLICY_VIOLATION = 1008; | 17 static const int POLICY_VIOLATION = 1008; |
| 18 static const int MESSAGE_TOO_BIG = 1009; | 18 static const int MESSAGE_TOO_BIG = 1009; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 31 * | 31 * |
| 32 * server.defaultHandler = wsHandler.onRequest; | 32 * server.defaultHandler = wsHandler.onRequest; |
| 33 * | 33 * |
| 34 * or | 34 * or |
| 35 * | 35 * |
| 36 * server.addRequestHandler((req) => req.path == "/ws", | 36 * server.addRequestHandler((req) => req.path == "/ws", |
| 37 * wsHandler.onRequest); | 37 * wsHandler.onRequest); |
| 38 * | 38 * |
| 39 * This handler strives to implement web sockets as specified by RFC6455. | 39 * This handler strives to implement web sockets as specified by RFC6455. |
| 40 */ | 40 */ |
| 41 interface WebSocketHandler default _WebSocketHandler { | 41 abstract class WebSocketHandler { |
| 42 WebSocketHandler(); | 42 factory WebSocketHandler() => new _WebSocketHandler(); |
| 43 | 43 |
| 44 /** | 44 /** |
| 45 * Request handler to be registered with the HTTP server. | 45 * Request handler to be registered with the HTTP server. |
| 46 */ | 46 */ |
| 47 void onRequest(HttpRequest request, HttpResponse response); | 47 void onRequest(HttpRequest request, HttpResponse response); |
| 48 | 48 |
| 49 /** | 49 /** |
| 50 * Sets the callback to be called when a new web socket connection | 50 * Sets the callback to be called when a new web socket connection |
| 51 * has been established. | 51 * has been established. |
| 52 */ | 52 */ |
| 53 void set onOpen(callback(WebSocketConnection connection)); | 53 void set onOpen(callback(WebSocketConnection connection)); |
| 54 } | 54 } |
| 55 | 55 |
| 56 | 56 |
| 57 /** | 57 /** |
| 58 * Server web socket connection. | 58 * Server web socket connection. |
| 59 */ | 59 */ |
| 60 interface WebSocketConnection extends Hashable { | 60 abstract class WebSocketConnection implements Hashable { |
| 61 /** | 61 /** |
| 62 * Sets the callback to be called when a message have been | 62 * Sets the callback to be called when a message have been |
| 63 * received. The type on [message] is either [:String:] or | 63 * received. The type on [message] is either [:String:] or |
| 64 * [:List<int>:] depending on whether it is a text or binary | 64 * [:List<int>:] depending on whether it is a text or binary |
| 65 * message. If the message is empty [message] will be [:null:]. | 65 * message. If the message is empty [message] will be [:null:]. |
| 66 */ | 66 */ |
| 67 void set onMessage(void callback(message)); | 67 void set onMessage(void callback(message)); |
| 68 | 68 |
| 69 /** | 69 /** |
| 70 * Sets the callback to be called when the web socket connection is | 70 * Sets the callback to be called when the web socket connection is |
| (...skipping 18 matching lines...) Expand all Loading... |
| 89 /** | 89 /** |
| 90 * WebSocketConnection is hashable. | 90 * WebSocketConnection is hashable. |
| 91 */ | 91 */ |
| 92 int hashCode(); | 92 int hashCode(); |
| 93 } | 93 } |
| 94 | 94 |
| 95 | 95 |
| 96 /** | 96 /** |
| 97 * Client web socket connection. | 97 * Client web socket connection. |
| 98 */ | 98 */ |
| 99 interface WebSocketClientConnection | 99 abstract class WebSocketClientConnection implements Hashable { |
| 100 extends Hashable default _WebSocketClientConnection { | |
| 101 /** | 100 /** |
| 102 * Creates a new web socket client connection based on a HTTP client | 101 * Creates a new web socket client connection based on a HTTP client |
| 103 * connection. The HTTP client connection must be freshly opened. | 102 * connection. The HTTP client connection must be freshly opened. |
| 104 */ | 103 */ |
| 105 WebSocketClientConnection(HttpClientConnection conn, | 104 factory WebSocketClientConnection(HttpClientConnection conn, |
| 106 [List<String> protocols]); | 105 [List<String> protocols]) { |
| 106 return new _WebSocketClientConnection(conn, protocols); |
| 107 } |
| 107 | 108 |
| 108 /** | 109 /** |
| 109 * Sets the callback to be called when the request object for the | 110 * Sets the callback to be called when the request object for the |
| 110 * opening handshake request is ready. This callback can be used if | 111 * opening handshake request is ready. This callback can be used if |
| 111 * one need to add additional headers to the opening handshake | 112 * one need to add additional headers to the opening handshake |
| 112 * request. | 113 * request. |
| 113 */ | 114 */ |
| 114 void set onRequest(void callback(HttpClientRequest request)); | 115 void set onRequest(void callback(HttpClientRequest request)); |
| 115 | 116 |
| 116 /** | 117 /** |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 close([int status, String reason]); | 160 close([int status, String reason]); |
| 160 | 161 |
| 161 /** | 162 /** |
| 162 * WebSocketClientConnection is hashable. | 163 * WebSocketClientConnection is hashable. |
| 163 */ | 164 */ |
| 164 int hashCode(); | 165 int hashCode(); |
| 165 } | 166 } |
| 166 | 167 |
| 167 | 168 |
| 168 /** | 169 /** |
| 169 * Base interface for the events generated by the W3C complient | 170 * Base class for the events generated by the W3C complient browser |
| 170 * browser API for web sockets. | 171 * API for web sockets. |
| 171 */ | 172 */ |
| 172 interface Event { } | 173 abstract class Event { } |
| 173 | 174 |
| 174 /** | 175 /** |
| 175 * Event delivered when there is data on a web socket connection. | 176 * Event delivered when there is data on a web socket connection. |
| 176 */ | 177 */ |
| 177 interface MessageEvent extends Event default _WebSocketMessageEvent { | 178 abstract class MessageEvent extends Event { |
| 178 /** | 179 /** |
| 179 * The type of [message] is either [:String:] or [:List<int>:] | 180 * The type of [message] is either [:String:] or [:List<int>:] |
| 180 * depending on whether it is a text or binary message. If the | 181 * depending on whether it is a text or binary message. If the |
| 181 * message is empty [message] will be [:null:] | 182 * message is empty [message] will be [:null:] |
| 182 */ | 183 */ |
| 183 get data; | 184 get data; |
| 184 } | 185 } |
| 185 | 186 |
| 186 | 187 |
| 187 /** | 188 /** |
| 188 * Event delivered when a web socket connection is closed. | 189 * Event delivered when a web socket connection is closed. |
| 189 */ | 190 */ |
| 190 interface CloseEvent extends Event default _WebSocketCloseEvent { | 191 abstract class CloseEvent extends Event { |
| 191 /** | 192 /** |
| 192 * Returns whether the connection was closed cleanly or not. | 193 * Returns whether the connection was closed cleanly or not. |
| 193 */ | 194 */ |
| 194 bool get wasClean; | 195 bool get wasClean; |
| 195 | 196 |
| 196 /** | 197 /** |
| 197 * Returns the web socket connection close code provided by the | 198 * Returns the web socket connection close code provided by the |
| 198 * server. | 199 * server. |
| 199 */ | 200 */ |
| 200 int get code; | 201 int get code; |
| 201 | 202 |
| 202 /** | 203 /** |
| 203 * Returns the web socket connection close reason provided by the | 204 * Returns the web socket connection close reason provided by the |
| 204 * server. | 205 * server. |
| 205 */ | 206 */ |
| 206 String get reason; | 207 String get reason; |
| 207 } | 208 } |
| 208 | 209 |
| 209 | 210 |
| 210 /** | 211 /** |
| 211 * Alternative web socket client interface. This interface is compliant | 212 * Alternative web socket client interface. This interface is compliant |
| 212 * with the W3C browser API for web sockets specified in | 213 * with the W3C browser API for web sockets specified in |
| 213 * http://dev.w3.org/html5/websockets/. | 214 * http://dev.w3.org/html5/websockets/. |
| 214 */ | 215 */ |
| 215 interface WebSocket default _WebSocket { | 216 abstract class WebSocket { |
| 216 /** | 217 /** |
| 217 * Possible states of the connection. | 218 * Possible states of the connection. |
| 218 */ | 219 */ |
| 219 static const int CONNECTING = 0; | 220 static const int CONNECTING = 0; |
| 220 static const int OPEN = 1; | 221 static const int OPEN = 1; |
| 221 static const int CLOSING = 2; | 222 static const int CLOSING = 2; |
| 222 static const int CLOSED = 3; | 223 static const int CLOSED = 3; |
| 223 | 224 |
| 224 /** | 225 /** |
| 225 * Create a new web socket connection. The URL supplied in [url] | 226 * Create a new web socket connection. The URL supplied in [url] |
| 226 * must use the scheme [:ws:]. The [protocols] argument is either a | 227 * must use the scheme [:ws:]. The [protocols] argument is either a |
| 227 * [:String:] or [:List<String>:] specifying the subprotocols the | 228 * [:String:] or [:List<String>:] specifying the subprotocols the |
| 228 * client is willing to speak. | 229 * client is willing to speak. |
| 229 */ | 230 */ |
| 230 WebSocket(String url, [protocols]); | 231 factory WebSocket(String url, [protocols]) => new _WebSocket(url, protocols); |
| 231 | 232 |
| 232 /** | 233 /** |
| 233 * Returns the current state of the connection. | 234 * Returns the current state of the connection. |
| 234 */ | 235 */ |
| 235 int get readyState; | 236 int get readyState; |
| 236 | 237 |
| 237 /** | 238 /** |
| 238 * Returns the number of bytes currently buffered for transmission. | 239 * Returns the number of bytes currently buffered for transmission. |
| 239 */ | 240 */ |
| 240 int get bufferedAmount; | 241 int get bufferedAmount; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 */ | 290 */ |
| 290 void send(data); | 291 void send(data); |
| 291 } | 292 } |
| 292 | 293 |
| 293 | 294 |
| 294 class WebSocketException implements Exception { | 295 class WebSocketException implements Exception { |
| 295 const WebSocketException([String this.message = ""]); | 296 const WebSocketException([String this.message = ""]); |
| 296 String toString() => "WebSocketException: $message"; | 297 String toString() => "WebSocketException: $message"; |
| 297 final String message; | 298 final String message; |
| 298 } | 299 } |
| OLD | NEW |