| 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 { |
| 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 [WebSocketTransformer] is implemented as a stream transformer that | 27 * The [WebSocketTransformer] provides the ability to upgrade a |
| 28 * transforms a stream of HttpRequest into a stream of WebSockets by upgrading | 28 * [HttpRequest] to a [WebSocket] connection. It supports both |
| 29 * each HttpRequest from the HTTP or HTTPS server, to the WebSocket protocol. | 29 * upgrading a single [HttpRequest] and upgrading a stream of |
| 30 * [HttpRequest]s. |
| 30 * | 31 * |
| 31 * Example of usage: | 32 * To upgrade a single [HttpRequest] use the static [upgrade] method. |
| 33 * |
| 34 * HttpServer server; |
| 35 * server.listen((request) { |
| 36 * if (...) { |
| 37 * WebSocketTransformer.upgrade(request).then(websocket) { |
| 38 * ... |
| 39 * } |
| 40 * } else { |
| 41 * // Do normal HTTP request processing. |
| 42 * } |
| 43 * }); |
| 44 * |
| 45 * To transform a stream of [HttpRequest] events as it implements a |
| 46 * stream transformer that transforms a stream of HttpRequest into a |
| 47 * stream of WebSockets by upgrading each HttpRequest from the HTTP or |
| 48 * HTTPS server, to the WebSocket protocol. |
| 32 * | 49 * |
| 33 * server.transform(new WebSocketTransformer()).listen((webSocket) => ...); | 50 * server.transform(new WebSocketTransformer()).listen((webSocket) => ...); |
| 34 * | 51 * |
| 35 * or | |
| 36 * | |
| 37 * server | |
| 38 * .where((request) => request.uri.scheme == "ws") | |
| 39 * .transform(new WebSocketTransformer()).listen((webSocket) => ...); | |
| 40 * | |
| 41 * This transformer strives to implement web sockets as specified by RFC6455. | 52 * This transformer strives to implement web sockets as specified by RFC6455. |
| 42 */ | 53 */ |
| 43 abstract class WebSocketTransformer | 54 abstract class WebSocketTransformer |
| 44 implements StreamTransformer<HttpRequest, WebSocket> { | 55 implements StreamTransformer<HttpRequest, WebSocket> { |
| 45 factory WebSocketTransformer() => new _WebSocketTransformerImpl(); | 56 factory WebSocketTransformer() => new _WebSocketTransformerImpl(); |
| 57 |
| 58 /** |
| 59 * Upgrades a [HttpRequest] to a [WebSocket] connection. If the |
| 60 * request is not a valid web socket upgrade request a HTTP response |
| 61 * with status code 500 will be returned. Otherwise the returned |
| 62 * future will complete with the [WebSocket] when the upgrade pocess |
| 63 * is complete. |
| 64 */ |
| 65 static Future<WebSocket> upgrade(HttpRequest request) { |
| 66 return _WebSocketTransformerImpl._upgrade(request); |
| 67 } |
| 68 |
| 69 /** |
| 70 * Checks whether the request is a valid WebSocket upgrade request. |
| 71 */ |
| 72 static bool isUpgradeRequest(HttpRequest request) { |
| 73 return _WebSocketTransformerImpl._isUpgradeRequest(request); |
| 74 } |
| 46 } | 75 } |
| 47 | 76 |
| 48 | 77 |
| 49 /** | 78 /** |
| 50 * A client or server web socket connection. The stream exposes the | 79 * A client or server web socket connection. The stream exposes the |
| 51 * messages received. A text message will be of type [:String:] and a | 80 * messages received. A text message will be of type [:String:] and a |
| 52 * binary message will be of type [:List<int>:]. | 81 * binary message will be of type [:List<int>:]. |
| 53 */ | 82 */ |
| 54 abstract class WebSocket implements Stream { | 83 abstract class WebSocket implements Stream { |
| 55 /** | 84 /** |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 */ | 141 */ |
| 113 void send(data); | 142 void send(data); |
| 114 } | 143 } |
| 115 | 144 |
| 116 | 145 |
| 117 class WebSocketException implements Exception { | 146 class WebSocketException implements Exception { |
| 118 const WebSocketException([String this.message = ""]); | 147 const WebSocketException([String this.message = ""]); |
| 119 String toString() => "WebSocketException: $message"; | 148 String toString() => "WebSocketException: $message"; |
| 120 final String message; | 149 final String message; |
| 121 } | 150 } |
| OLD | NEW |