| 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 part of dart.io; | 5 part of dart.io; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Web socket status codes used when closing a web socket connection. | 8 * Web socket status codes used when closing a web socket connection. |
| 9 */ | 9 */ |
| 10 abstract class WebSocketStatus { | 10 abstract class WebSocketStatus { |
| 11 static const int NORMAL_CLOSURE = 1000; | 11 static const int NORMAL_CLOSURE = 1000; |
| 12 static const int GOING_AWAY = 1001; | 12 static const int GOING_AWAY = 1001; |
| 13 static const int PROTOCOL_ERROR = 1002; | 13 static const int PROTOCOL_ERROR = 1002; |
| 14 static const int UNSUPPORTED_DATA = 1003; | 14 static const int UNSUPPORTED_DATA = 1003; |
| 15 static const int RESERVED_1004 = 1004; | 15 static const int RESERVED_1004 = 1004; |
| 16 static const int NO_STATUS_RECEIVED = 1005; | 16 static const int NO_STATUS_RECEIVED = 1005; |
| 17 static const int ABNORMAL_CLOSURE = 1006; | 17 static const int ABNORMAL_CLOSURE = 1006; |
| 18 static const int INVALID_FRAME_PAYLOAD_DATA = 1007; | 18 static const int INVALID_FRAME_PAYLOAD_DATA = 1007; |
| 19 static const int POLICY_VIOLATION = 1008; | 19 static const int POLICY_VIOLATION = 1008; |
| 20 static const int MESSAGE_TOO_BIG = 1009; | 20 static const int MESSAGE_TOO_BIG = 1009; |
| 21 static const int MISSING_MANDATORY_EXTENSION = 1010; | 21 static const int MISSING_MANDATORY_EXTENSION = 1010; |
| 22 static const int INTERNAL_SERVER_ERROR = 1011; | 22 static const int INTERNAL_SERVER_ERROR = 1011; |
| 23 static const int RESERVED_1015 = 1015; | 23 static const int RESERVED_1015 = 1015; |
| 24 } | 24 } |
| 25 | 25 |
| 26 /** | 26 /** |
| 27 * The web socket protocol is implemented by a HTTP server handler | 27 * The web socket protocol is implemented by a HTTP or HTTPS server handler |
| 28 * which can be instantiated like this: | 28 * which can be instantiated like this: |
| 29 * | 29 * |
| 30 * WebSocketHandler wsHandler = new WebSocketHandler(); | 30 * WebSocketHandler wsHandler = new WebSocketHandler(); |
| 31 * | 31 * |
| 32 * and then its onRequest method can be assigned to the HTTP server, e.g. | 32 * and then its onRequest method can be assigned to the HTTP server, e.g. |
| 33 * | 33 * |
| 34 * server.defaultHandler = wsHandler.onRequest; | 34 * server.defaultHandler = wsHandler.onRequest; |
| 35 * | 35 * |
| 36 * or | 36 * or |
| 37 * | 37 * |
| (...skipping 16 matching lines...) Expand all Loading... |
| 54 */ | 54 */ |
| 55 void set onOpen(callback(WebSocketConnection connection)); | 55 void set onOpen(callback(WebSocketConnection connection)); |
| 56 } | 56 } |
| 57 | 57 |
| 58 | 58 |
| 59 /** | 59 /** |
| 60 * Server web socket connection. | 60 * Server web socket connection. |
| 61 */ | 61 */ |
| 62 abstract class WebSocketConnection { | 62 abstract class WebSocketConnection { |
| 63 /** | 63 /** |
| 64 * Sets the callback to be called when a message have been | 64 * Sets the callback to be called when a message has been |
| 65 * received. The type on [message] is either [:String:] or | 65 * received. The type on [message] is either [:String:] or |
| 66 * [:List<int>:] depending on whether it is a text or binary | 66 * [:List<int>:] depending on whether it is a text or binary |
| 67 * message. If the message is empty [message] will be [:null:]. | 67 * message. If the message is empty [message] will be [:null:]. |
| 68 * If [message] is a [:List<int>:] then it will contain byte values |
| 69 * from 0 to 255. |
| 68 */ | 70 */ |
| 69 void set onMessage(void callback(message)); | 71 void set onMessage(void callback(message)); |
| 70 | 72 |
| 71 /** | 73 /** |
| 72 * Sets the callback to be called when the web socket connection is | 74 * Sets the callback to be called when the web socket connection is |
| 73 * closed. [status] indicate the reason for closing. For network | 75 * closed. [status] indicate the reason for closing. For network |
| 74 * errors the value of [status] will be | 76 * errors the value of [status] will be |
| 75 * WebSocketStatus.ABNORMAL_CLOSURE]. In this callbach it is | 77 * WebSocketStatus.ABNORMAL_CLOSURE]. In this callback it is |
| 76 * possible to call [close] if [close] has not already been called. | 78 * possible to call [close] if [close] has not already been called. |
| 77 * If [close] has still not been called after the close callback | 79 * If [close] has still not been called after the close callback |
| 78 * returns the received close status will automatically be echoed | 80 * returns the received close status will automatically be echoed |
| 79 * back to the other end to finish the close handshake. | 81 * back to the other end to finish the close handshake. |
| 80 */ | 82 */ |
| 81 void set onClosed(void callback(int status, String reason)); | 83 void set onClosed(void callback(int status, String reason)); |
| 82 | 84 |
| 83 /** | 85 /** |
| 84 * Sends a message. The [message] must be a [:String:] a | 86 * Sends a message. The [message] must be a [:String:], a |
| 85 * [:List<int>:] or [:null:]. | 87 * [:List<int>:] containing bytes, or [:null:]. |
| 86 */ | 88 */ |
| 87 send(Object message); | 89 send(Object message); |
| 88 | 90 |
| 89 /** | 91 /** |
| 90 * Close the web socket connection. The default value for [status] | 92 * Close the web socket connection. The default value for [status] |
| 91 * and [reason] are [:null:]. | 93 * and [reason] are [:null:]. |
| 92 */ | 94 */ |
| 93 close([int status, String reason]); | 95 close([int status, String reason]); |
| 94 | |
| 95 /** | |
| 96 * WebSocketConnection is hashable. | |
| 97 */ | |
| 98 int get hashCode; | |
| 99 } | 96 } |
| 100 | 97 |
| 101 | 98 |
| 102 /** | 99 /** |
| 103 * Client web socket connection. | 100 * Client web socket connection. |
| 104 */ | 101 */ |
| 105 abstract class WebSocketClientConnection { | 102 abstract class WebSocketClientConnection { |
| 106 /** | 103 /** |
| 107 * Creates a new web socket client connection based on a HTTP client | 104 * Creates a new web socket client connection based on a HTTP(S) client |
| 108 * connection. The HTTP client connection must be freshly opened. | 105 * connection. The HTTP or HTTPS client connection must be freshly opened. |
| 109 */ | 106 */ |
| 110 factory WebSocketClientConnection(HttpClientConnection conn, | 107 factory WebSocketClientConnection(HttpClientConnection conn, |
| 111 [List<String> protocols]) { | 108 [List<String> protocols]) { |
| 112 return new _WebSocketClientConnection(conn, protocols); | 109 return new _WebSocketClientConnection(conn, protocols); |
| 113 } | 110 } |
| 114 | 111 |
| 115 /** | 112 /** |
| 116 * Sets the callback to be called when the request object for the | 113 * Sets the callback to be called when the request object for the |
| 117 * opening handshake request is ready. This callback can be used if | 114 * opening handshake request is ready. This callback can be used if |
| 118 * one need to add additional headers to the opening handshake | 115 * one needs to add additional headers to the opening handshake |
| 119 * request. | 116 * request. |
| 120 */ | 117 */ |
| 121 void set onRequest(void callback(HttpClientRequest request)); | 118 void set onRequest(void callback(HttpClientRequest request)); |
| 122 | 119 |
| 123 /** | 120 /** |
| 124 * Sets the callback to be called when a web socket connection has | 121 * Sets the callback to be called when a web socket connection has |
| 125 * been established. | 122 * been established. |
| 126 */ | 123 */ |
| 127 void set onOpen(void callback()); | 124 void set onOpen(void callback()); |
| 128 | 125 |
| 129 /** | 126 /** |
| 130 * Sets the callback to be called when a message have been | 127 * Sets the callback to be called when a message has been |
| 131 * received. The type of [message] is either [:String:] or | 128 * received. The type of [message] is either [:String:] or |
| 132 * [:List<int>:] depending on whether it is a text or binary | 129 * [:List<int>:], depending on whether it is a text or binary |
| 133 * message. If the message is empty [message] will be [:null:]. | 130 * message. If the message is empty [message] will be [:null:]. |
| 131 * If the message is a [:List<int>:] then it will contain byte values |
| 132 * from 0 to 255. |
| 134 */ | 133 */ |
| 135 void set onMessage(void callback(message)); | 134 void set onMessage(void callback(message)); |
| 136 | 135 |
| 137 /** | 136 /** |
| 138 * Sets the callback to be called when the web socket connection is | 137 * Sets the callback to be called when the web socket connection is |
| 139 * closed. [status] indicate the reason for closing. For network | 138 * closed. [status] indicates the reason for closing. For network |
| 140 * errors the value of [status] will be | 139 * errors the value of [status] will be |
| 141 * WebSocketStatus.ABNORMAL_CLOSURE]. | 140 * WebSocketStatus.ABNORMAL_CLOSURE]. |
| 142 */ | 141 */ |
| 143 void set onClosed(void callback(int status, String reason)); | 142 void set onClosed(void callback(int status, String reason)); |
| 144 | 143 |
| 145 /** | 144 /** |
| 146 * Sets the callback to be called when the response object for the | 145 * Sets the callback to be called when the response object for the |
| 147 * opening handshake did not cause a web socket connection | 146 * opening handshake did not cause a web socket connection |
| 148 * upgrade. This will be called in case the response status code is | 147 * upgrade. This will be called in case the response status code is |
| 149 * not 101 (Switching Protocols). If this callback is not set the | 148 * not 101 (Switching Protocols). If this callback is not set and the |
| 150 * [:onError:] callback will be called if the server did not upgrade | 149 * server does not upgrade the connection, the [:onError:] callback will |
| 151 * the connection. | 150 * be called. |
| 152 */ | 151 */ |
| 153 void set onNoUpgrade(void callback(HttpClientResponse response)); | 152 void set onNoUpgrade(void callback(HttpClientResponse response)); |
| 154 | 153 |
| 155 /** | 154 /** |
| 156 * Sends a message. The [message] must be a [:String:] or a | 155 * Sends a message. The [message] must be a [:String:] or a |
| 157 * [:List<int>:]. To send an empty message use either an empty | 156 * [:List<int>:] containing bytes. To send an empty message send either |
| 158 * [:String:] or an empty [:List<int>:]. [:null:] cannot be used. | 157 * an empty [:String:] or an empty [:List<int>:]. [:null:] cannot be sent. |
| 159 */ | 158 */ |
| 160 send(message); | 159 send(message); |
| 161 | 160 |
| 162 /** | 161 /** |
| 163 * Close the web socket connection. The default value for [status] | 162 * Close the web socket connection. The default value for [status] |
| 164 * and [reason] are [:null:]. | 163 * and [reason] are [:null:]. |
| 165 */ | 164 */ |
| 166 close([int status, String reason]); | 165 close([int status, String reason]); |
| 167 | |
| 168 /** | |
| 169 * WebSocketClientConnection is hashable. | |
| 170 */ | |
| 171 int get hashCode; | |
| 172 } | 166 } |
| 173 | 167 |
| 174 | 168 |
| 175 /** | 169 /** |
| 176 * Base class for the events generated by the W3C complient browser | 170 * Base class for the events generated by the W3C complient browser |
| 177 * API for web sockets. | 171 * API for web sockets. |
| 178 */ | 172 */ |
| 179 abstract class Event { } | 173 abstract class Event { } |
| 180 | 174 |
| 181 /** | 175 /** |
| 182 * Event delivered when there is data on a web socket connection. | 176 * Event delivered when there is data on a web socket connection. |
| 183 */ | 177 */ |
| 184 abstract class MessageEvent extends Event { | 178 abstract class MessageEvent extends Event { |
| 185 /** | 179 /** |
| 186 * The type of [message] is either [:String:] or [:List<int>:] | 180 * The type of [message] is either [:String:] or [:List<int>:] |
| 187 * 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 |
| 188 * message is empty [message] will be [:null:] | 182 * message is empty [message] will be [:null:] |
| 183 * If the message is a [:List<int>:] then it will contain byte values |
| 184 * from 0 to 255. |
| 185 |
| 189 */ | 186 */ |
| 190 get data; | 187 get data; |
| 191 } | 188 } |
| 192 | 189 |
| 193 | 190 |
| 194 /** | 191 /** |
| 195 * Event delivered when a web socket connection is closed. | 192 * Event delivered when a web socket connection is closed. |
| 196 */ | 193 */ |
| 197 abstract class CloseEvent extends Event { | 194 abstract class CloseEvent extends Event { |
| 198 /** | 195 /** |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 * value will remain [:null:]. | 275 * value will remain [:null:]. |
| 279 */ | 276 */ |
| 280 String get protocol; | 277 String get protocol; |
| 281 | 278 |
| 282 /** | 279 /** |
| 283 * Closes the web socket connection. | 280 * Closes the web socket connection. |
| 284 */ | 281 */ |
| 285 void close(int code, String reason); | 282 void close(int code, String reason); |
| 286 | 283 |
| 287 /** | 284 /** |
| 288 * Sets the callback to be called when a message have been | 285 * Sets the callback to be called when a message has been |
| 289 * received. | 286 * received. |
| 290 */ | 287 */ |
| 291 void set onmessage(void callback(MessageEvent event)); | 288 void set onmessage(void callback(MessageEvent event)); |
| 292 | 289 |
| 293 /** | 290 /** |
| 294 * Sends data on the web socket connection. The data in [data] must | 291 * Sends data on the web socket connection. The data in [data] must |
| 295 * be either a [:String:] or [:List<int>:] holding bytes. | 292 * be either a [:String:], or a [:List<int>:] holding bytes. |
| 296 */ | 293 */ |
| 297 void send(data); | 294 void send(data); |
| 298 } | 295 } |
| 299 | 296 |
| 300 | 297 |
| 301 class WebSocketException implements Exception { | 298 class WebSocketException implements Exception { |
| 302 const WebSocketException([String this.message = ""]); | 299 const WebSocketException([String this.message = ""]); |
| 303 String toString() => "WebSocketException: $message"; | 300 String toString() => "WebSocketException: $message"; |
| 304 final String message; | 301 final String message; |
| 305 } | 302 } |
| OLD | NEW |