OLD | NEW |
1 // Copyright (c) 2013, 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 * WebSocket status codes used when closing a WebSocket 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; |
(...skipping 23 matching lines...) Expand all Loading... |
42 * } | 42 * } |
43 * }); | 43 * }); |
44 * | 44 * |
45 * To transform a stream of [HttpRequest] events as it implements a | 45 * To transform a stream of [HttpRequest] events as it implements a |
46 * stream transformer that transforms a stream of HttpRequest into a | 46 * stream transformer that transforms a stream of HttpRequest into a |
47 * stream of WebSockets by upgrading each HttpRequest from the HTTP or | 47 * stream of WebSockets by upgrading each HttpRequest from the HTTP or |
48 * HTTPS server, to the WebSocket protocol. | 48 * HTTPS server, to the WebSocket protocol. |
49 * | 49 * |
50 * server.transform(new WebSocketTransformer()).listen((webSocket) => ...); | 50 * server.transform(new WebSocketTransformer()).listen((webSocket) => ...); |
51 * | 51 * |
52 * This transformer strives to implement web sockets as specified by RFC6455. | 52 * This transformer strives to implement WebSockets as specified by RFC6455. |
53 */ | 53 */ |
54 abstract class WebSocketTransformer | 54 abstract class WebSocketTransformer |
55 implements StreamTransformer<HttpRequest, WebSocket> { | 55 implements StreamTransformer<HttpRequest, WebSocket> { |
56 | 56 |
57 /** | 57 /** |
58 * Create a new [WebSocketTransformer]. | 58 * Create a new [WebSocketTransformer]. |
59 * | 59 * |
60 * If [protocolSelector] is provided, [protocolSelector] will be called to | 60 * If [protocolSelector] is provided, [protocolSelector] will be called to |
61 * select what protocol to use, if any were provided by the client. | 61 * select what protocol to use, if any were provided by the client. |
62 * [protocolSelector] is should return either a [String] or a [Future] | 62 * [protocolSelector] is should return either a [String] or a [Future] |
63 * completing with a [String]. The [String] must exist in the list of | 63 * completing with a [String]. The [String] must exist in the list of |
64 * protocols. | 64 * protocols. |
65 */ | 65 */ |
66 factory WebSocketTransformer({protocolSelector(List<String> protocols)}) | 66 factory WebSocketTransformer({protocolSelector(List<String> protocols)}) |
67 => new _WebSocketTransformerImpl(protocolSelector); | 67 => new _WebSocketTransformerImpl(protocolSelector); |
68 | 68 |
69 /** | 69 /** |
70 * Upgrades a [HttpRequest] to a [WebSocket] connection. If the | 70 * Upgrades a [HttpRequest] to a [WebSocket] connection. If the |
71 * request is not a valid web socket upgrade request a HTTP response | 71 * request is not a valid WebSocket upgrade request an HTTP response |
72 * with status code 500 will be returned. Otherwise the returned | 72 * with status code 500 will be returned. Otherwise the returned |
73 * future will complete with the [WebSocket] when the upgrade pocess | 73 * future will complete with the [WebSocket] when the upgrade pocess |
74 * is complete. | 74 * is complete. |
75 * | 75 * |
76 * If [protocolSelector] is provided, [protocolSelector] will be called to | 76 * If [protocolSelector] is provided, [protocolSelector] will be called to |
77 * select what protocol to use, if any were provided by the client. | 77 * select what protocol to use, if any were provided by the client. |
78 * [protocolSelector] is should return either a [String] or a [Future] | 78 * [protocolSelector] is should return either a [String] or a [Future] |
79 * completing with a [String]. The [String] must exist in the list of | 79 * completing with a [String]. The [String] must exist in the list of |
80 * protocols. | 80 * protocols. |
81 */ | 81 */ |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 * There are never two outstanding pings at any given time, and the next ping | 118 * There are never two outstanding pings at any given time, and the next ping |
119 * timer starts when the pong is received. | 119 * timer starts when the pong is received. |
120 * | 120 * |
121 * Set the [pingInterval] to `null` to disable sending ping messages. | 121 * Set the [pingInterval] to `null` to disable sending ping messages. |
122 * | 122 * |
123 * The default value is `null`. | 123 * The default value is `null`. |
124 */ | 124 */ |
125 Duration pingInterval; | 125 Duration pingInterval; |
126 | 126 |
127 /** | 127 /** |
128 * Create a new web socket connection. The URL supplied in [url] | 128 * Create a new WebSocket connection. The URL supplied in [url] |
129 * must use the scheme [:ws:] or [:wss:]. The [protocols] argument is | 129 * must use the scheme `ws` or `wss`. |
130 * specifying the subprotocols the client is willing to speak. | 130 * |
| 131 * The [protocols] argument is specifying the subprotocols the |
| 132 * client is willing to speak. |
| 133 * |
| 134 * The [headers] argument is specifying additional HTTP headers for |
| 135 * setting up the connection. This would typically be the `Origin` |
| 136 * header and potentially cookies. The keys of the map are the header |
| 137 * fields and the values are either String or List<String>. |
| 138 * |
| 139 * If [headers] is provided, there are a number of headers |
| 140 * which are controlled by the WebSocket connection process. These |
| 141 * headers are: |
| 142 * |
| 143 * - `connection` |
| 144 * - `sec-websocket-key` |
| 145 * - `sec-websocket-protocol` |
| 146 * - `sec-websocket-version` |
| 147 * - `upgrade` |
| 148 * |
| 149 * If any of these are passed in the `headers` map they will be ignored. |
131 */ | 150 */ |
132 static Future<WebSocket> connect(String url, | 151 static Future<WebSocket> connect(String url, |
133 {List<String> protocols: const []}) => | 152 {Iterable<String> protocols, |
134 _WebSocketImpl.connect(url, protocols); | 153 Map<String, dynamic> headers}) => |
| 154 _WebSocketImpl.connect(url, protocols, headers); |
135 | 155 |
136 @Deprecated('This constructor will be removed in Dart 2.0. Use `implements`' | 156 @Deprecated('This constructor will be removed in Dart 2.0. Use `implements`' |
137 ' instead of `extends` if implementing this abstract class.') | 157 ' instead of `extends` if implementing this abstract class.') |
138 WebSocket(); | 158 WebSocket(); |
139 | 159 |
140 /** | 160 /** |
141 * Creates a WebSocket from an already-upgraded socket. | 161 * Creates a WebSocket from an already-upgraded socket. |
142 * | 162 * |
143 * The initial WebSocket handshake must have occurred prior to this call. A | 163 * The initial WebSocket handshake must have occurred prior to this call. A |
144 * WebSocket client can automatically perform the handshake using | 164 * WebSocket client can automatically perform the handshake using |
(...skipping 16 matching lines...) Expand all Loading... |
161 return new _WebSocketImpl._fromSocket(socket, protocol, serverSide); | 181 return new _WebSocketImpl._fromSocket(socket, protocol, serverSide); |
162 } | 182 } |
163 | 183 |
164 /** | 184 /** |
165 * Returns the current state of the connection. | 185 * Returns the current state of the connection. |
166 */ | 186 */ |
167 int get readyState; | 187 int get readyState; |
168 | 188 |
169 /** | 189 /** |
170 * The extensions property is initially the empty string. After the | 190 * The extensions property is initially the empty string. After the |
171 * web socket connection is established this string reflects the | 191 * WebSocket connection is established this string reflects the |
172 * extensions used by the server. | 192 * extensions used by the server. |
173 */ | 193 */ |
174 String get extensions; | 194 String get extensions; |
175 | 195 |
176 /** | 196 /** |
177 * The protocol property is initially the empty string. After the | 197 * The protocol property is initially the empty string. After the |
178 * web socket connection is established the value is the subprotocol | 198 * WebSocket connection is established the value is the subprotocol |
179 * selected by the server. If no subprotocol is negotiated the | 199 * selected by the server. If no subprotocol is negotiated the |
180 * value will remain [:null:]. | 200 * value will remain [:null:]. |
181 */ | 201 */ |
182 String get protocol; | 202 String get protocol; |
183 | 203 |
184 /** | 204 /** |
185 * The close code set when the web socket connection is closed. If | 205 * The close code set when the WebSocket connection is closed. If |
186 * there is no close code available this property will be [:null:] | 206 * there is no close code available this property will be [:null:] |
187 */ | 207 */ |
188 int get closeCode; | 208 int get closeCode; |
189 | 209 |
190 /** | 210 /** |
191 * The close reason set when the web socket connection is closed. If | 211 * The close reason set when the WebSocket connection is closed. If |
192 * there is no close reason available this property will be [:null:] | 212 * there is no close reason available this property will be [:null:] |
193 */ | 213 */ |
194 String get closeReason; | 214 String get closeReason; |
195 | 215 |
196 /** | 216 /** |
197 * Closes the web socket connection. Set the optional [code] and [reason] | 217 * Closes the WebSocket connection. Set the optional [code] and [reason] |
198 * arguments to send close information to the remote peer. If they are | 218 * arguments to send close information to the remote peer. If they are |
199 * omitted, the peer will see [WebSocketStatus.NO_STATUS_RECEIVED] code | 219 * omitted, the peer will see [WebSocketStatus.NO_STATUS_RECEIVED] code |
200 * with no reason. | 220 * with no reason. |
201 */ | 221 */ |
202 Future close([int code, String reason]); | 222 Future close([int code, String reason]); |
203 | 223 |
204 /** | 224 /** |
205 * Sends data on the web socket connection. The data in [data] must | 225 * Sends data on the WebSocket connection. The data in [data] must |
206 * be either a [:String:], or a [:List<int>:] holding bytes. | 226 * be either a [:String:], or a [:List<int>:] holding bytes. |
207 */ | 227 */ |
208 void add(data); | 228 void add(data); |
209 | 229 |
210 /** | 230 /** |
211 * Sends data from a stream on web socket connection. Each data event from | 231 * Sends data from a stream on WebSocket connection. Each data event from |
212 * [stream] will be send as a single WebSocket frame. The data from [stream] | 232 * [stream] will be send as a single WebSocket frame. The data from [stream] |
213 * must be either [:String:]s, or [:List<int>:]s holding bytes. | 233 * must be either [:String:]s, or [:List<int>:]s holding bytes. |
214 */ | 234 */ |
215 Future addStream(Stream stream); | 235 Future addStream(Stream stream); |
216 } | 236 } |
217 | 237 |
218 | 238 |
219 class WebSocketException implements IOException { | 239 class WebSocketException implements IOException { |
220 final String message; | 240 final String message; |
221 const WebSocketException([this.message = ""]); | 241 const WebSocketException([this.message = ""]); |
222 String toString() => "WebSocketException: $message"; | 242 String toString() => "WebSocketException: $message"; |
223 } | 243 } |
OLD | NEW |