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