| 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 * Web socket status codes used when closing a web socket connection. |
| 9 */ | 9 */ |
| 10 abstract class WebSocketStatus { | 10 abstract class WebSocketStatus { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 web sockets 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 factory WebSocketTransformer() => new _WebSocketTransformerImpl(); | 56 |
| 57 /** |
| 58 * Create a new [WebSocketTransformer]. |
| 59 * |
| 60 * If [protocolSelector] is provided, [protocolSelector] will be called to |
| 61 * select what protocol to use, if any were provided by the client. |
| 62 * [protocolSelector] is should return either a [String] or a [Future] |
| 63 * completing with a [String]. The [String] must exist in the list of |
| 64 * protocols. |
| 65 */ |
| 66 factory WebSocketTransformer({protocolSelector(List<String> protocols)}) |
| 67 => new _WebSocketTransformerImpl(protocolSelector); |
| 57 | 68 |
| 58 /** | 69 /** |
| 59 * Upgrades a [HttpRequest] to a [WebSocket] connection. If the | 70 * Upgrades a [HttpRequest] to a [WebSocket] connection. If the |
| 60 * request is not a valid web socket upgrade request a HTTP response | 71 * request is not a valid web socket upgrade request a HTTP response |
| 61 * with status code 500 will be returned. Otherwise the returned | 72 * with status code 500 will be returned. Otherwise the returned |
| 62 * future will complete with the [WebSocket] when the upgrade pocess | 73 * future will complete with the [WebSocket] when the upgrade pocess |
| 63 * is complete. | 74 * is complete. |
| 75 * |
| 76 * If [protocolSelector] is provided, [protocolSelector] will be called to |
| 77 * select what protocol to use, if any were provided by the client. |
| 78 * [protocolSelector] is should return either a [String] or a [Future] |
| 79 * completing with a [String]. The [String] must exist in the list of |
| 80 * protocols. |
| 64 */ | 81 */ |
| 65 static Future<WebSocket> upgrade(HttpRequest request) { | 82 static Future<WebSocket> upgrade(HttpRequest request, |
| 66 return _WebSocketTransformerImpl._upgrade(request); | 83 {protocolSelector(List<String> protocols)}) { |
| 84 return _WebSocketTransformerImpl._upgrade(request, protocolSelector); |
| 67 } | 85 } |
| 68 | 86 |
| 69 /** | 87 /** |
| 70 * Checks whether the request is a valid WebSocket upgrade request. | 88 * Checks whether the request is a valid WebSocket upgrade request. |
| 71 */ | 89 */ |
| 72 static bool isUpgradeRequest(HttpRequest request) { | 90 static bool isUpgradeRequest(HttpRequest request) { |
| 73 return _WebSocketTransformerImpl._isUpgradeRequest(request); | 91 return _WebSocketTransformerImpl._isUpgradeRequest(request); |
| 74 } | 92 } |
| 75 } | 93 } |
| 76 | 94 |
| 77 | 95 |
| 78 /** | 96 /** |
| 79 * A two-way HTTP communication object for client or server applications. | 97 * A two-way HTTP communication object for client or server applications. |
| 80 * | 98 * |
| 81 * The stream exposes the messages received. A text message will be of type | 99 * The stream exposes the messages received. A text message will be of type |
| 82 * [:String:] and a binary message will be of type [:List<int>:]. | 100 * [:String:] and a binary message will be of type [:List<int>:]. |
| 83 */ | 101 */ |
| 84 abstract class WebSocket implements Stream, StreamSink { | 102 abstract class WebSocket implements Stream, StreamSink { |
| 85 /** | 103 /** |
| 86 * Possible states of the connection. | 104 * Possible states of the connection. |
| 87 */ | 105 */ |
| 88 static const int CONNECTING = 0; | 106 static const int CONNECTING = 0; |
| 89 static const int OPEN = 1; | 107 static const int OPEN = 1; |
| 90 static const int CLOSING = 2; | 108 static const int CLOSING = 2; |
| 91 static const int CLOSED = 3; | 109 static const int CLOSED = 3; |
| 92 | 110 |
| 93 /** | 111 /** |
| 94 * Create a new web socket connection. The URL supplied in [url] | 112 * Create a new web socket connection. The URL supplied in [url] |
| 95 * must use the scheme [:ws:] or [:wss:]. The [protocols] argument is either | 113 * must use the scheme [:ws:] or [:wss:]. The [protocols] argument is |
| 96 * a [:String:] or [:List<String>:] specifying the subprotocols the | 114 * specifying the subprotocols the client is willing to speak. |
| 97 * client is willing to speak. | |
| 98 */ | 115 */ |
| 99 static Future<WebSocket> connect(String url, [protocols]) => | 116 static Future<WebSocket> connect(String url, |
| 117 {List<String> protocols: const []}) => |
| 100 _WebSocketImpl.connect(url, protocols); | 118 _WebSocketImpl.connect(url, protocols); |
| 101 | 119 |
| 102 /** | 120 /** |
| 103 * Returns the current state of the connection. | 121 * Returns the current state of the connection. |
| 104 */ | 122 */ |
| 105 int get readyState; | 123 int get readyState; |
| 106 | 124 |
| 107 /** | 125 /** |
| 108 * The extensions property is initially the empty string. After the | 126 * The extensions property is initially the empty string. After the |
| 109 * web socket connection is established this string reflects the | 127 * web socket connection is established this string reflects the |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 */ | 186 */ |
| 169 Future addStream(Stream stream); | 187 Future addStream(Stream stream); |
| 170 } | 188 } |
| 171 | 189 |
| 172 | 190 |
| 173 class WebSocketException implements IOException { | 191 class WebSocketException implements IOException { |
| 174 const WebSocketException([String this.message = ""]); | 192 const WebSocketException([String this.message = ""]); |
| 175 String toString() => "WebSocketException: $message"; | 193 String toString() => "WebSocketException: $message"; |
| 176 final String message; | 194 final String message; |
| 177 } | 195 } |
| OLD | NEW |