| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 40 * | 40 * |
| 41 * This transformer strives to implement web sockets as specified by RFC6455. | 41 * This transformer strives to implement web sockets as specified by RFC6455. |
| 42 */ | 42 */ |
| 43 abstract class WebSocketTransformer | 43 abstract class WebSocketTransformer |
| 44 implements StreamTransformer<HttpRequest, WebSocket> { | 44 implements StreamTransformer<HttpRequest, WebSocket> { |
| 45 factory WebSocketTransformer() => new _WebSocketTransformerImpl(); | 45 factory WebSocketTransformer() => new _WebSocketTransformerImpl(); |
| 46 } | 46 } |
| 47 | 47 |
| 48 | 48 |
| 49 /** | 49 /** |
| 50 * Base class for the events generated by the [WebSocket] stream. | 50 * A client or server web socket connection. The stream exposes the |
| 51 * messages received. A text message will be of type [:String:] and a |
| 52 * binary message will be of type [:List<int>:]. |
| 51 */ | 53 */ |
| 52 abstract class Event { } | 54 abstract class WebSocket implements Stream { |
| 53 | |
| 54 /** | |
| 55 * Event delivered when there is data on a web socket connection. | |
| 56 */ | |
| 57 abstract class MessageEvent extends Event { | |
| 58 /** | |
| 59 * The type of [message] is either [:String:] or [:List<int>:] | |
| 60 * depending on whether it is a text or binary message. If the | |
| 61 * message is empty [message] will be [:null:] | |
| 62 * If the message is a [:List<int>:] then it will contain byte values | |
| 63 * from 0 to 255. | |
| 64 */ | |
| 65 get data; | |
| 66 } | |
| 67 | |
| 68 | |
| 69 /** | |
| 70 * Event delivered when a web socket connection is closed. | |
| 71 */ | |
| 72 abstract class CloseEvent extends Event { | |
| 73 /** | |
| 74 * Returns whether the connection was closed cleanly or not. | |
| 75 */ | |
| 76 bool get wasClean; | |
| 77 | |
| 78 /** | |
| 79 * Returns the web socket connection close code provided by the | |
| 80 * server. | |
| 81 */ | |
| 82 int get code; | |
| 83 | |
| 84 /** | |
| 85 * Returns the web socket connection close reason provided by the | |
| 86 * server. | |
| 87 */ | |
| 88 String get reason; | |
| 89 } | |
| 90 | |
| 91 | |
| 92 /** | |
| 93 * A client or server web socket connection. This interface is compliant | |
| 94 * with the W3C browser API for web sockets specified in | |
| 95 * http://dev.w3.org/html5/websockets/. | |
| 96 */ | |
| 97 abstract class WebSocket implements Stream<Event> { | |
| 98 /** | 55 /** |
| 99 * Possible states of the connection. | 56 * Possible states of the connection. |
| 100 */ | 57 */ |
| 101 static const int CONNECTING = 0; | 58 static const int CONNECTING = 0; |
| 102 static const int OPEN = 1; | 59 static const int OPEN = 1; |
| 103 static const int CLOSING = 2; | 60 static const int CLOSING = 2; |
| 104 static const int CLOSED = 3; | 61 static const int CLOSED = 3; |
| 105 | 62 |
| 106 /** | 63 /** |
| 107 * Create a new web socket connection. The URL supplied in [url] | 64 * Create a new web socket connection. The URL supplied in [url] |
| 108 * must use the scheme [:ws:] or [:wss:]. The [protocols] argument is either | 65 * must use the scheme [:ws:] or [:wss:]. The [protocols] argument is either |
| 109 * a [:String:] or [:List<String>:] specifying the subprotocols the | 66 * a [:String:] or [:List<String>:] specifying the subprotocols the |
| 110 * client is willing to speak. | 67 * client is willing to speak. |
| 111 */ | 68 */ |
| 112 static Future<WebSocket> connect(String url, [protocols]) => | 69 static Future<WebSocket> connect(String url, [protocols]) => |
| 113 _WebSocketImpl.connect(url, protocols); | 70 _WebSocketImpl.connect(url, protocols); |
| 114 | 71 |
| 115 /** | 72 /** |
| 116 * Returns the current state of the connection. | 73 * Returns the current state of the connection. |
| 117 */ | 74 */ |
| 118 int get readyState; | 75 int get readyState; |
| 119 | 76 |
| 120 /** | 77 /** |
| 121 * Returns the number of bytes currently buffered for transmission. | |
| 122 */ | |
| 123 int get bufferedAmount; | |
| 124 | |
| 125 /** | |
| 126 * The extensions property is initially the empty string. After the | 78 * The extensions property is initially the empty string. After the |
| 127 * web socket connection is established this string reflects the | 79 * web socket connection is established this string reflects the |
| 128 * extensions used by the server. | 80 * extensions used by the server. |
| 129 */ | 81 */ |
| 130 String get extensions; | 82 String get extensions; |
| 131 | 83 |
| 132 /** | 84 /** |
| 133 * The protocol property is initially the empty string. After the | 85 * The protocol property is initially the empty string. After the |
| 134 * web socket connection is established the value is the subprotocol | 86 * web socket connection is established the value is the subprotocol |
| 135 * selected by the server. If no subprotocol is negotiated the | 87 * selected by the server. If no subprotocol is negotiated the |
| 136 * value will remain [:null:]. | 88 * value will remain [:null:]. |
| 137 */ | 89 */ |
| 138 String get protocol; | 90 String get protocol; |
| 139 | 91 |
| 140 /** | 92 /** |
| 93 * The close code set when the web socket connection is closed. If |
| 94 * there is no close code available this property will be [:null:] |
| 95 */ |
| 96 int get closeCode; |
| 97 |
| 98 /** |
| 99 * The close reason set when the web socket connection is closed. If |
| 100 * there is no close reason available this property will be [:null:] |
| 101 */ |
| 102 String get closeReason; |
| 103 |
| 104 /** |
| 141 * Closes the web socket connection. | 105 * Closes the web socket connection. |
| 142 */ | 106 */ |
| 143 void close([int code, String reason]); | 107 void close([int code, String reason]); |
| 144 | 108 |
| 145 /** | 109 /** |
| 146 * Sends data on the web socket connection. The data in [data] must | 110 * Sends data on the web socket connection. The data in [data] must |
| 147 * be either a [:String:], or a [:List<int>:] holding bytes. | 111 * be either a [:String:], or a [:List<int>:] holding bytes. |
| 148 */ | 112 */ |
| 149 void send(data); | 113 void send(data); |
| 150 } | 114 } |
| 151 | 115 |
| 152 | 116 |
| 153 class WebSocketException implements Exception { | 117 class WebSocketException implements Exception { |
| 154 const WebSocketException([String this.message = ""]); | 118 const WebSocketException([String this.message = ""]); |
| 155 String toString() => "WebSocketException: $message"; | 119 String toString() => "WebSocketException: $message"; |
| 156 final String message; | 120 final String message; |
| 157 } | 121 } |
| OLD | NEW |