| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2014, 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:crypto/crypto.dart'; | |
| 9 | |
| 10 import '../copy/web_socket_impl.dart'; | |
| 11 | |
| 12 /// This class is deprecated. | |
| 13 /// | |
| 14 /// Use the [`web_socket_channel`][web_socket_channel] package instead. | |
| 15 /// | |
| 16 /// [web_socket_channel]: https://pub.dartlang.org/packages/web_socket_channel | |
| 17 @Deprecated("Will be removed in 3.0.0.") | |
| 18 abstract class CompatibleWebSocket implements Stream, StreamSink { | |
| 19 /// The interval for sending ping signals. | |
| 20 /// | |
| 21 /// If a ping message is not answered by a pong message from the peer, the | |
| 22 /// `WebSocket` is assumed disconnected and the connection is closed with a | |
| 23 /// [WebSocketStatus.GOING_AWAY] close code. When a ping signal is sent, the | |
| 24 /// pong message must be received within [pingInterval]. | |
| 25 /// | |
| 26 /// There are never two outstanding pings at any given time, and the next ping | |
| 27 /// timer starts when the pong is received. | |
| 28 /// | |
| 29 /// By default, the [pingInterval] is `null`, indicating that ping messages | |
| 30 /// are disabled. | |
| 31 Duration pingInterval; | |
| 32 | |
| 33 /// The [close code][] set when the WebSocket connection is closed. | |
| 34 /// | |
| 35 /// [close code]: https://tools.ietf.org/html/rfc6455#section-7.1.5 | |
| 36 /// | |
| 37 /// Before the connection has been closed, this will be `null`. | |
| 38 int get closeCode; | |
| 39 | |
| 40 /// The [close reason][] set when the WebSocket connection is closed. | |
| 41 /// | |
| 42 /// [close reason]: https://tools.ietf.org/html/rfc6455#section-7.1.6 | |
| 43 /// | |
| 44 /// Before the connection has been closed, this will be `null`. | |
| 45 String get closeReason; | |
| 46 | |
| 47 /// Signs a `Sec-WebSocket-Key` header sent by a WebSocket client as part of | |
| 48 /// the [initial handshake]. | |
| 49 /// | |
| 50 /// The return value should be sent back to the client in a | |
| 51 /// `Sec-WebSocket-Accept` header. | |
| 52 /// | |
| 53 /// [initial handshake]: https://tools.ietf.org/html/rfc6455#section-4.2.2 | |
| 54 static String signKey(String key) { | |
| 55 // We use [codeUnits] here rather than UTF-8-decoding the string because | |
| 56 // [key] is expected to be base64 encoded, and so will be pure ASCII. | |
| 57 return BASE64.encode(sha1.convert((key + webSocketGUID).codeUnits).bytes); | |
| 58 } | |
| 59 | |
| 60 /// Creates a new WebSocket handling messaging across an existing socket. | |
| 61 /// | |
| 62 /// Because this is HTTP-API-agnostic, the initial [WebSocket handshake][] | |
| 63 /// must have already been completed on the socket before this is called. | |
| 64 /// | |
| 65 /// If [stream] is also a [StreamSink] (for example, if it's a "dart:io" | |
| 66 /// `Socket`), it will be used for both sending and receiving data. Otherwise, | |
| 67 /// it will be used for receiving data and [sink] will be used for sending it. | |
| 68 /// | |
| 69 /// [protocol] should be the protocol negotiated by this handshake, if any. | |
| 70 /// | |
| 71 /// If this is a WebSocket server, [serverSide] should be `true` (the | |
| 72 /// default); if it's a client, [serverSide] should be `false`. | |
| 73 /// | |
| 74 /// [WebSocket handshake]: https://tools.ietf.org/html/rfc6455#section-4 | |
| 75 factory CompatibleWebSocket(Stream<List<int>> stream, | |
| 76 {StreamSink<List<int>> sink, String protocol, bool serverSide: true}) { | |
| 77 if (sink == null) { | |
| 78 if (stream is! StreamSink) { | |
| 79 throw new ArgumentError("If stream isn't also a StreamSink, sink must " | |
| 80 "be passed explicitly."); | |
| 81 } | |
| 82 sink = stream as StreamSink; | |
| 83 } | |
| 84 | |
| 85 return new WebSocketImpl.fromSocket(stream, sink, protocol, serverSide); | |
| 86 } | |
| 87 | |
| 88 /// Closes the web socket connection. | |
| 89 /// | |
| 90 /// [closeCode] and [closeReason] are the [close code][] and [reason][] sent | |
| 91 /// to the remote peer, respectively. If they are omitted, the peer will see | |
| 92 /// a "no status received" code with no reason. | |
| 93 /// | |
| 94 /// [close code]: https://tools.ietf.org/html/rfc6455#section-7.1.5 | |
| 95 /// [reason]: https://tools.ietf.org/html/rfc6455#section-7.1.6 | |
| 96 Future close([int closeCode, String closeReason]); | |
| 97 } | |
| OLD | NEW |