OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
| 7 import 'package:async/async.dart'; |
7 import 'package:crypto/crypto.dart'; | 8 import 'package:crypto/crypto.dart'; |
| 9 import 'package:stream_channel/stream_channel.dart'; |
8 | 10 |
9 import 'copy/web_socket_impl.dart'; | 11 import '../copy/web_socket_impl.dart'; |
10 | 12 |
11 /// An implementation of the WebSocket protocol that's not specific to "dart:io" | 13 /// A [StreamChannel] implementation of the WebSocket protocol. |
12 /// or to any particular HTTP API. | |
13 /// | 14 /// |
14 /// Because this is HTTP-API-agnostic, it doesn't handle the initial [WebSocket | 15 /// This is not specific to `dart:io` or to any particular HTTP API. Because of |
15 /// handshake][]. This needs to be handled manually by the user of the code. | 16 /// that, it doesn't handle the initial [WebSocket handshake][]. This needs to |
16 /// Once that's been done, [new CompatibleWebSocket] can be called with the | 17 /// be handled manually by the user of the code. Once that's been done, [new |
17 /// underlying socket and it will handle the remainder of the protocol. | 18 /// WebSocketChannel] can be called with the underlying socket and it will |
| 19 /// handle the remainder of the protocol. |
18 /// | 20 /// |
19 /// [WebSocket handshake]: https://tools.ietf.org/html/rfc6455#section-4 | 21 /// [WebSocket handshake]: https://tools.ietf.org/html/rfc6455#section-4 |
20 abstract class CompatibleWebSocket implements Stream, StreamSink { | 22 class WebSocketChannel extends StreamChannelMixin { |
| 23 /// The underlying web socket. |
| 24 /// |
| 25 /// This is essentially a copy of `dart:io`'s WebSocket implementation, with |
| 26 /// the IO-specific pieces factored out. |
| 27 final WebSocketImpl _webSocket; |
| 28 |
21 /// The interval for sending ping signals. | 29 /// The interval for sending ping signals. |
22 /// | 30 /// |
23 /// If a ping message is not answered by a pong message from the peer, the | 31 /// If a ping message is not answered by a pong message from the peer, the |
24 /// `WebSocket` is assumed disconnected and the connection is closed with a | 32 /// `WebSocket` is assumed disconnected and the connection is closed with a |
25 /// [WebSocketStatus.GOING_AWAY] close code. When a ping signal is sent, the | 33 /// [WebSocketStatus.GOING_AWAY] close code. When a ping signal is sent, the |
26 /// pong message must be received within [pingInterval]. | 34 /// pong message must be received within [pingInterval]. |
27 /// | 35 /// |
28 /// There are never two outstanding pings at any given time, and the next ping | 36 /// There are never two outstanding pings at any given time, and the next ping |
29 /// timer starts when the pong is received. | 37 /// timer starts when the pong is received. |
30 /// | 38 /// |
31 /// By default, the [pingInterval] is `null`, indicating that ping messages | 39 /// By default, the [pingInterval] is `null`, indicating that ping messages |
32 /// are disabled. | 40 /// are disabled. |
33 Duration pingInterval; | 41 Duration get pingInterval => _webSocket.pingInterval; |
| 42 set pingInterval(Duration value) => _webSocket.pingInterval = value; |
34 | 43 |
35 /// The [close code][] set when the WebSocket connection is closed. | 44 /// The [close code][] set when the WebSocket connection is closed. |
36 /// | 45 /// |
37 /// [close code]: https://tools.ietf.org/html/rfc6455#section-7.1.5 | 46 /// [close code]: https://tools.ietf.org/html/rfc6455#section-7.1.5 |
38 /// | 47 /// |
39 /// Before the connection has been closed, this will be `null`. | 48 /// Before the connection has been closed, this will be `null`. |
40 int get closeCode; | 49 int get closeCode => _webSocket.closeCode; |
41 | 50 |
42 /// The [close reason][] set when the WebSocket connection is closed. | 51 /// The [close reason][] set when the WebSocket connection is closed. |
43 /// | 52 /// |
44 /// [close reason]: https://tools.ietf.org/html/rfc6455#section-7.1.6 | 53 /// [close reason]: https://tools.ietf.org/html/rfc6455#section-7.1.6 |
45 /// | 54 /// |
46 /// Before the connection has been closed, this will be `null`. | 55 /// Before the connection has been closed, this will be `null`. |
47 String get closeReason; | 56 String get closeReason => _webSocket.closeReason; |
| 57 |
| 58 Stream get stream => new StreamView(_webSocket); |
| 59 |
| 60 /// The sink for sending values to the other endpoint. |
| 61 /// |
| 62 /// This has additional arguments to [WebSocketSink.close] arguments that |
| 63 /// provide the remote endpoint reasons for closing the connection. |
| 64 WebSocketSink get sink => new WebSocketSink._(_webSocket); |
48 | 65 |
49 /// Signs a `Sec-WebSocket-Key` header sent by a WebSocket client as part of | 66 /// Signs a `Sec-WebSocket-Key` header sent by a WebSocket client as part of |
50 /// the [initial handshake]. | 67 /// the [initial handshake]. |
51 /// | 68 /// |
52 /// The return value should be sent back to the client in a | 69 /// The return value should be sent back to the client in a |
53 /// `Sec-WebSocket-Accept` header. | 70 /// `Sec-WebSocket-Accept` header. |
54 /// | 71 /// |
55 /// [initial handshake]: https://tools.ietf.org/html/rfc6455#section-4.2.2 | 72 /// [initial handshake]: https://tools.ietf.org/html/rfc6455#section-4.2.2 |
56 static String signKey(String key) { | 73 static String signKey(String key) { |
57 var hash = new SHA1(); | 74 var hash = new SHA1(); |
(...skipping 11 matching lines...) Expand all Loading... |
69 /// If [stream] is also a [StreamSink] (for example, if it's a "dart:io" | 86 /// If [stream] is also a [StreamSink] (for example, if it's a "dart:io" |
70 /// `Socket`), it will be used for both sending and receiving data. Otherwise, | 87 /// `Socket`), it will be used for both sending and receiving data. Otherwise, |
71 /// it will be used for receiving data and [sink] will be used for sending it. | 88 /// it will be used for receiving data and [sink] will be used for sending it. |
72 /// | 89 /// |
73 /// [protocol] should be the protocol negotiated by this handshake, if any. | 90 /// [protocol] should be the protocol negotiated by this handshake, if any. |
74 /// | 91 /// |
75 /// If this is a WebSocket server, [serverSide] should be `true` (the | 92 /// If this is a WebSocket server, [serverSide] should be `true` (the |
76 /// default); if it's a client, [serverSide] should be `false`. | 93 /// default); if it's a client, [serverSide] should be `false`. |
77 /// | 94 /// |
78 /// [WebSocket handshake]: https://tools.ietf.org/html/rfc6455#section-4 | 95 /// [WebSocket handshake]: https://tools.ietf.org/html/rfc6455#section-4 |
79 factory CompatibleWebSocket(Stream<List<int>> stream, | 96 WebSocketChannel(StreamChannel<List<int>> channel, |
80 {StreamSink<List<int>> sink, String protocol, bool serverSide: true}) { | 97 {String protocol, bool serverSide: true}) |
81 if (sink == null) { | 98 : _webSocket = new WebSocketImpl.fromSocket( |
82 if (stream is! StreamSink) { | 99 channel.stream, channel.sink, protocol, serverSide); |
83 throw new ArgumentError("If stream isn't also a StreamSink, sink must " | 100 } |
84 "be passed explicitly."); | |
85 } | |
86 sink = stream as StreamSink; | |
87 } | |
88 | 101 |
89 return new WebSocketImpl.fromSocket(stream, sink, protocol, serverSide); | 102 /// The sink exposed by a [CompatibleWebSocket]. |
90 } | 103 /// |
| 104 /// This is like a normal [StreamSink], except that it supports extra arguments |
| 105 /// to [close]. |
| 106 class WebSocketSink extends DelegatingStreamSink { |
| 107 final WebSocketImpl _webSocket; |
| 108 |
| 109 WebSocketSink._(WebSocketImpl webSocket) |
| 110 : super(webSocket), |
| 111 _webSocket = webSocket; |
91 | 112 |
92 /// Closes the web socket connection. | 113 /// Closes the web socket connection. |
93 /// | 114 /// |
94 /// [closeCode] and [closeReason] are the [close code][] and [reason][] sent | 115 /// [closeCode] and [closeReason] are the [close code][] and [reason][] sent |
95 /// to the remote peer, respectively. If they are omitted, the peer will see | 116 /// to the remote peer, respectively. If they are omitted, the peer will see |
96 /// a "no status received" code with no reason. | 117 /// a "no status received" code with no reason. |
97 /// | 118 /// |
98 /// [close code]: https://tools.ietf.org/html/rfc6455#section-7.1.5 | 119 /// [close code]: https://tools.ietf.org/html/rfc6455#section-7.1.5 |
99 /// [reason]: https://tools.ietf.org/html/rfc6455#section-7.1.6 | 120 /// [reason]: https://tools.ietf.org/html/rfc6455#section-7.1.6 |
100 Future close([int closeCode, String closeReason]); | 121 Future close([int closeCode, String closeReason]) => |
| 122 _webSocket.close(closeCode, closeReason); |
101 } | 123 } |
102 | |
103 /// An exception thrown by [CompatibleWebSocket]. | |
104 class CompatibleWebSocketException implements Exception { | |
105 final String message; | |
106 | |
107 CompatibleWebSocketException([this.message]); | |
108 | |
109 String toString() => message == null | |
110 ? "CompatibleWebSocketException" : | |
111 "CompatibleWebSocketException: $message"; | |
112 } | |
OLD | NEW |