Chromium Code Reviews| 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 * The web socket protocol is implemented by a HTTP server handler | 6 * The web socket protocol is implemented by a HTTP server handler |
| 7 * which can be instantiated like this: | 7 * which can be instantiated like this: |
| 8 * | 8 * |
| 9 * WebSocketHandler wsHandler = new WebSocketHandler(); | 9 * WebSocketHandler wsHandler = new WebSocketHandler(); |
| 10 * | 10 * |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 send(message); | 134 send(message); |
| 135 | 135 |
| 136 /** | 136 /** |
| 137 * Close the web socket connection. The default value for [status] | 137 * Close the web socket connection. The default value for [status] |
| 138 * and [reason] are [:null:]. | 138 * and [reason] are [:null:]. |
| 139 */ | 139 */ |
| 140 close([int status, String reason]); | 140 close([int status, String reason]); |
| 141 } | 141 } |
| 142 | 142 |
| 143 | 143 |
| 144 /** | |
| 145 * Base interface for the events generated by the W3C complient | |
| 146 * browser API for web sockets. | |
| 147 */ | |
| 148 interface Event { } | |
|
Jacob
2012/05/09 17:06:43
It would be nice if this event interface could be
| |
| 149 | |
| 150 /** | |
| 151 * Event delivered when there is data on a web socket connection. | |
| 152 */ | |
| 153 interface MessageEvent extends Event default _WebSocketMessageEvent { | |
| 154 /** | |
| 155 * The type of [message] is either [:String:] or [:List<int>:] | |
| 156 * depending on whether it is a text or binary message. If the | |
| 157 * message is empty [message] will be [:null:] | |
| 158 */ | |
| 159 get data(); | |
| 160 } | |
| 161 | |
| 162 | |
| 163 /** | |
| 164 * Event delivered when a web socket connection is closed. | |
| 165 */ | |
| 166 interface CloseEvent extends Event default _WebSocketCloseEvent { | |
| 167 /** | |
| 168 * Returns whether the connection was closed cleanly or not. | |
| 169 */ | |
| 170 bool get wasClean(); | |
| 171 | |
| 172 /** | |
| 173 * Returns the web socket connection close code provided by the | |
| 174 * server. | |
| 175 */ | |
| 176 int get code(); | |
| 177 | |
| 178 /** | |
| 179 * Returns the web socket connection close reason provided by the | |
| 180 * server. | |
| 181 */ | |
| 182 String get reason(); | |
| 183 } | |
| 184 | |
| 185 | |
| 186 /** | |
| 187 * Alternative web socket client interface. This interface is compliant | |
| 188 * with the W3C browser API for web sockets specified in | |
| 189 * http://dev.w3.org/html5/websockets/. | |
| 190 */ | |
| 191 interface WebSocket default _WebSocket { | |
| 192 /** | |
| 193 * Possible states of the connection. | |
| 194 */ | |
| 195 static final int CONNECTING = 0; | |
| 196 static final int OPEN = 1; | |
| 197 static final int CLOSING = 2; | |
| 198 static final int CLOSED = 3; | |
| 199 | |
| 200 /** | |
| 201 * Create a new web socket connection. The URL supplied in [url] | |
| 202 * must use the scheme [:ws:]. The [protocols] argument is either a | |
| 203 * [:String:] or [:List<String>:] specifying the subprotocols the | |
| 204 * client is willing to speak. | |
| 205 */ | |
| 206 WebSocket(String url, [protocols]); | |
| 207 | |
| 208 /** | |
| 209 * Returns the current state of the connection. | |
| 210 */ | |
| 211 int get readyState(); | |
| 212 | |
| 213 /** | |
| 214 * Returns the number of bytes currently buffered for transmission. | |
| 215 */ | |
| 216 int get bufferedAmount(); | |
| 217 | |
| 218 /** | |
| 219 * Sets the callback to be called when a web socket connection has | |
| 220 * been established. | |
| 221 */ | |
| 222 void set onopen(void callback()); | |
| 223 | |
| 224 /** | |
| 225 * Sets the callback to be called when the web socket connection | |
| 226 * encountered an error. | |
| 227 */ | |
| 228 void set onerror(void callback(e)); | |
| 229 | |
| 230 /** | |
| 231 * Sets the callback to be called when the web socket connection is | |
| 232 * closed. | |
| 233 */ | |
| 234 void set onclose(void callback(CloseEvent event)); | |
| 235 | |
| 236 /** | |
| 237 * The extensions property is initially the empty string. After the | |
| 238 * web socket connection is established this string reflects the | |
| 239 * extensions used by the server. | |
| 240 */ | |
| 241 String get extensions(); | |
| 242 | |
| 243 /** | |
| 244 * The protocol property is initially the empty string. After the | |
| 245 * web socket connection is established the value is the subprotocol | |
| 246 * selected by the server. If no subprotocol is negotiated the | |
| 247 * value will remain [:null:]. | |
| 248 */ | |
| 249 String get protocol(); | |
| 250 | |
| 251 /** | |
| 252 * Closes the web socket connection. | |
| 253 */ | |
| 254 void close(int code, String reason); | |
| 255 | |
| 256 /** | |
| 257 * Sets the callback to be called when a message have been | |
| 258 * received. | |
| 259 */ | |
| 260 void set onmessage(void callback(MessageEvent event)); | |
| 261 | |
| 262 /** | |
| 263 * Sends data on the web socket connection. The data in [data] must | |
| 264 * be either a [:String:] or [:List<int>:] holding bytes. | |
| 265 */ | |
| 266 void send(data); | |
| 267 } | |
| 268 | |
| 269 | |
| 144 class WebSocketException implements Exception { | 270 class WebSocketException implements Exception { |
| 145 const WebSocketException([String this.message = ""]); | 271 const WebSocketException([String this.message = ""]); |
| 146 String toString() => "WebSocketException: $message"; | 272 String toString() => "WebSocketException: $message"; |
| 147 final String message; | 273 final String message; |
| 148 } | 274 } |
| OLD | NEW |