OLD | NEW |
1 // Copyright (c) 2016, 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:async/async.dart'; |
8 import 'package:crypto/crypto.dart'; | 8 import 'package:crypto/crypto.dart'; |
9 import 'package:stream_channel/stream_channel.dart'; | 9 import 'package:stream_channel/stream_channel.dart'; |
10 | 10 |
11 import 'copy/web_socket_impl.dart'; | 11 import 'copy/web_socket_impl.dart'; |
12 | 12 |
13 /// A [StreamChannel] that communicates over a WebSocket. | 13 /// A [StreamChannel] that communicates over a WebSocket. |
14 /// | 14 /// |
15 /// This is implemented by classes that use `dart:io` and `dart:html`. The [new | 15 /// This is implemented by classes that use `dart:io` and `dart:html`. The [new |
16 /// WebSocketChannel] constructor can also be used on any platform to connect to | 16 /// WebSocketChannel] constructor can also be used on any platform to connect to |
17 /// use the WebSocket protocol over a pre-existing channel. | 17 /// use the WebSocket protocol over a pre-existing channel. |
18 /// | 18 /// |
19 /// All implementations emit [WebSocketChannelException]s. These exceptions wrap | 19 /// All implementations emit [WebSocketChannelException]s. These exceptions wrap |
20 /// the native exception types where possible. | 20 /// the native exception types where possible. |
21 class WebSocketChannel extends StreamChannelMixin { | 21 class WebSocketChannel extends StreamChannelMixin { |
22 /// The underlying web socket. | 22 /// The underlying web socket. |
23 /// | 23 /// |
24 /// This is essentially a copy of `dart:io`'s WebSocket implementation, with | 24 /// This is essentially a copy of `dart:io`'s WebSocket implementation, with |
25 /// the IO-specific pieces factored out. | 25 /// the IO-specific pieces factored out. |
26 final WebSocketImpl _webSocket; | 26 final WebSocketImpl _webSocket; |
27 | 27 |
28 /// The interval for sending ping signals. | |
29 /// | |
30 /// If a ping message is not answered by a pong message from the peer, the | |
31 /// `WebSocket` is assumed disconnected and the connection is closed with a | |
32 /// [WebSocketStatus.GOING_AWAY] close code. When a ping signal is sent, the | |
33 /// pong message must be received within [pingInterval]. | |
34 /// | |
35 /// There are never two outstanding pings at any given time, and the next ping | |
36 /// timer starts when the pong is received. | |
37 /// | |
38 /// By default, the [pingInterval] is `null`, indicating that ping messages | |
39 /// are disabled. Some implementations may not support setting it. | |
40 Duration get pingInterval => _webSocket.pingInterval; | |
41 set pingInterval(Duration value) => _webSocket.pingInterval = value; | |
42 | |
43 /// The subprotocol selected by the server. | 28 /// The subprotocol selected by the server. |
44 /// | 29 /// |
45 /// For a client socket, this is initially `null`. After the WebSocket | 30 /// For a client socket, this is initially `null`. After the WebSocket |
46 /// connection is established the value is set to the subprotocol selected by | 31 /// connection is established the value is set to the subprotocol selected by |
47 /// the server. If no subprotocol is negotiated the value will remain `null`. | 32 /// the server. If no subprotocol is negotiated the value will remain `null`. |
48 String get protocol => _webSocket.protocol; | 33 String get protocol => _webSocket.protocol; |
49 | 34 |
50 /// The [close code][] set when the WebSocket connection is closed. | 35 /// The [close code][] set when the WebSocket connection is closed. |
51 /// | 36 /// |
52 /// [close code]: https://tools.ietf.org/html/rfc6455#section-7.1.5 | 37 /// [close code]: https://tools.ietf.org/html/rfc6455#section-7.1.5 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 | 71 |
87 /// Creates a new WebSocket handling messaging across an existing [channel]. | 72 /// Creates a new WebSocket handling messaging across an existing [channel]. |
88 /// | 73 /// |
89 /// This is a cross-platform constructor; it doesn't use either `dart:io` or | 74 /// This is a cross-platform constructor; it doesn't use either `dart:io` or |
90 /// `dart:html`. It's also HTTP-API-agnostic, which means that the initial | 75 /// `dart:html`. It's also HTTP-API-agnostic, which means that the initial |
91 /// [WebSocket handshake][] must have already been completed on the socket | 76 /// [WebSocket handshake][] must have already been completed on the socket |
92 /// before this is called. | 77 /// before this is called. |
93 /// | 78 /// |
94 /// [protocol] should be the protocol negotiated by this handshake, if any. | 79 /// [protocol] should be the protocol negotiated by this handshake, if any. |
95 /// | 80 /// |
| 81 /// [pingInterval] controls the interval for sending ping signals. If a ping |
| 82 /// message is not answered by a pong message from the peer, the WebSocket is |
| 83 /// assumed disconnected and the connection is closed with a |
| 84 /// [WebSocketStatus.GOING_AWAY] close code. When a ping signal is sent, the |
| 85 /// pong message must be received within [pingInterval]. It defaults to |
| 86 /// `null`, indicating that ping messages are disabled. |
| 87 /// |
96 /// If this is a WebSocket server, [serverSide] should be `true` (the | 88 /// If this is a WebSocket server, [serverSide] should be `true` (the |
97 /// default); if it's a client, [serverSide] should be `false`. | 89 /// default); if it's a client, [serverSide] should be `false`. |
98 /// | 90 /// |
99 /// [WebSocket handshake]: https://tools.ietf.org/html/rfc6455#section-4 | 91 /// [WebSocket handshake]: https://tools.ietf.org/html/rfc6455#section-4 |
100 WebSocketChannel(StreamChannel<List<int>> channel, | 92 WebSocketChannel(StreamChannel<List<int>> channel, |
101 {String protocol, bool serverSide: true}) | 93 {String protocol, Duration pingInterval, bool serverSide: true}) |
102 : _webSocket = new WebSocketImpl.fromSocket( | 94 : _webSocket = new WebSocketImpl.fromSocket( |
103 channel.stream, channel.sink, protocol, serverSide); | 95 channel.stream, channel.sink, protocol, serverSide) |
| 96 ..pingInterval = pingInterval; |
104 } | 97 } |
105 | 98 |
106 /// The sink exposed by a [WebSocketChannel]. | 99 /// The sink exposed by a [WebSocketChannel]. |
107 /// | 100 /// |
108 /// This is like a normal [StreamSink], except that it supports extra arguments | 101 /// This is like a normal [StreamSink], except that it supports extra arguments |
109 /// to [close]. | 102 /// to [close]. |
110 class WebSocketSink extends DelegatingStreamSink { | 103 class WebSocketSink extends DelegatingStreamSink { |
111 final WebSocketImpl _webSocket; | 104 final WebSocketImpl _webSocket; |
112 | 105 |
113 WebSocketSink._(WebSocketImpl webSocket) | 106 WebSocketSink._(WebSocketImpl webSocket) |
114 : super(webSocket), | 107 : super(webSocket), |
115 _webSocket = webSocket; | 108 _webSocket = webSocket; |
116 | 109 |
117 /// Closes the web socket connection. | 110 /// Closes the web socket connection. |
118 /// | 111 /// |
119 /// [closeCode] and [closeReason] are the [close code][] and [reason][] sent | 112 /// [closeCode] and [closeReason] are the [close code][] and [reason][] sent |
120 /// to the remote peer, respectively. If they are omitted, the peer will see | 113 /// to the remote peer, respectively. If they are omitted, the peer will see |
121 /// a "no status received" code with no reason. | 114 /// a "no status received" code with no reason. |
122 /// | 115 /// |
123 /// [close code]: https://tools.ietf.org/html/rfc6455#section-7.1.5 | 116 /// [close code]: https://tools.ietf.org/html/rfc6455#section-7.1.5 |
124 /// [reason]: https://tools.ietf.org/html/rfc6455#section-7.1.6 | 117 /// [reason]: https://tools.ietf.org/html/rfc6455#section-7.1.6 |
125 Future close([int closeCode, String closeReason]) => | 118 Future close([int closeCode, String closeReason]) => |
126 _webSocket.close(closeCode, closeReason); | 119 _webSocket.close(closeCode, closeReason); |
127 } | 120 } |
OLD | NEW |