OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import 'dart:async'; | |
6 import 'dart:convert'; | |
7 | |
8 import 'package:async/async.dart'; | |
9 import 'package:crypto/crypto.dart'; | |
10 import 'package:stream_channel/stream_channel.dart'; | |
11 | |
12 import '../copy/web_socket_impl.dart'; | |
13 | |
14 /// This class is deprecated. | |
15 /// | |
16 /// Use the [`web_socket_channel`][web_socket_channel] package instead. | |
17 /// | |
18 /// [web_socket_channel]: https://pub.dartlang.org/packages/web_socket_channel | |
19 @Deprecated("Will be removed in 3.0.0.") | |
20 class WebSocketChannel extends StreamChannelMixin { | |
21 /// The underlying web socket. | |
22 /// | |
23 /// This is essentially a copy of `dart:io`'s WebSocket implementation, with | |
24 /// the IO-specific pieces factored out. | |
25 final WebSocketImpl _webSocket; | |
26 | |
27 /// The interval for sending ping signals. | |
28 /// | |
29 /// If a ping message is not answered by a pong message from the peer, the | |
30 /// `WebSocket` is assumed disconnected and the connection is closed with a | |
31 /// [WebSocketStatus.GOING_AWAY] close code. When a ping signal is sent, the | |
32 /// pong message must be received within [pingInterval]. | |
33 /// | |
34 /// There are never two outstanding pings at any given time, and the next ping | |
35 /// timer starts when the pong is received. | |
36 /// | |
37 /// By default, the [pingInterval] is `null`, indicating that ping messages | |
38 /// are disabled. | |
39 Duration get pingInterval => _webSocket.pingInterval; | |
40 set pingInterval(Duration value) => _webSocket.pingInterval = value; | |
41 | |
42 /// The [close code][] set when the WebSocket connection is closed. | |
43 /// | |
44 /// [close code]: https://tools.ietf.org/html/rfc6455#section-7.1.5 | |
45 /// | |
46 /// Before the connection has been closed, this will be `null`. | |
47 int get closeCode => _webSocket.closeCode; | |
48 | |
49 /// The [close reason][] set when the WebSocket connection is closed. | |
50 /// | |
51 /// [close reason]: https://tools.ietf.org/html/rfc6455#section-7.1.6 | |
52 /// | |
53 /// Before the connection has been closed, this will be `null`. | |
54 String get closeReason => _webSocket.closeReason; | |
55 | |
56 Stream get stream => new StreamView(_webSocket); | |
57 | |
58 /// The sink for sending values to the other endpoint. | |
59 /// | |
60 /// This has additional arguments to [WebSocketSink.close] arguments that | |
61 /// provide the remote endpoint reasons for closing the connection. | |
62 WebSocketSink get sink => new WebSocketSink._(_webSocket); | |
63 | |
64 /// Signs a `Sec-WebSocket-Key` header sent by a WebSocket client as part of | |
65 /// the [initial handshake]. | |
66 /// | |
67 /// The return value should be sent back to the client in a | |
68 /// `Sec-WebSocket-Accept` header. | |
69 /// | |
70 /// [initial handshake]: https://tools.ietf.org/html/rfc6455#section-4.2.2 | |
71 static String signKey(String key) { | |
72 // We use [codeUnits] here rather than UTF-8-decoding the string because | |
73 // [key] is expected to be base64 encoded, and so will be pure ASCII. | |
74 return BASE64.encode(sha1.convert((key + webSocketGUID).codeUnits).bytes); | |
75 } | |
76 | |
77 /// Creates a new WebSocket handling messaging across an existing socket. | |
78 /// | |
79 /// Because this is HTTP-API-agnostic, the initial [WebSocket handshake][] | |
80 /// must have already been completed on the socket before this is called. | |
81 /// | |
82 /// If [stream] is also a [StreamSink] (for example, if it's a "dart:io" | |
83 /// `Socket`), it will be used for both sending and receiving data. Otherwise, | |
84 /// it will be used for receiving data and [sink] will be used for sending it. | |
85 /// | |
86 /// [protocol] should be the protocol negotiated by this handshake, if any. | |
87 /// | |
88 /// If this is a WebSocket server, [serverSide] should be `true` (the | |
89 /// default); if it's a client, [serverSide] should be `false`. | |
90 /// | |
91 /// [WebSocket handshake]: https://tools.ietf.org/html/rfc6455#section-4 | |
92 WebSocketChannel(StreamChannel<List<int>> channel, | |
93 {String protocol, bool serverSide: true}) | |
94 : _webSocket = new WebSocketImpl.fromSocket( | |
95 channel.stream, channel.sink, protocol, serverSide); | |
96 } | |
97 | |
98 /// This class is deprecated. | |
99 /// | |
100 /// Use the [`web_socket_channel`][web_socket_channel] package instead. | |
101 /// | |
102 /// [web_socket_channel]: https://pub.dartlang.org/packages/web_socket_channel | |
103 @Deprecated("Will be removed in 3.0.0.") | |
104 class WebSocketSink extends DelegatingStreamSink { | |
105 final WebSocketImpl _webSocket; | |
106 | |
107 WebSocketSink._(WebSocketImpl webSocket) | |
108 : super(webSocket), | |
109 _webSocket = webSocket; | |
110 | |
111 /// Closes the web socket connection. | |
112 /// | |
113 /// [closeCode] and [closeReason] are the [close code][] and [reason][] sent | |
114 /// to the remote peer, respectively. If they are omitted, the peer will see | |
115 /// a "no status received" code with no reason. | |
116 /// | |
117 /// [close code]: https://tools.ietf.org/html/rfc6455#section-7.1.5 | |
118 /// [reason]: https://tools.ietf.org/html/rfc6455#section-7.1.6 | |
119 Future close([int closeCode, String closeReason]) => | |
120 _webSocket.close(closeCode, closeReason); | |
121 } | |
OLD | NEW |