Index: lib/src/web_socket/channel.dart |
diff --git a/lib/src/web_socket.dart b/lib/src/web_socket/channel.dart |
similarity index 60% |
copy from lib/src/web_socket.dart |
copy to lib/src/web_socket/channel.dart |
index a11840c09f523322c9b0f3095ddea6ee98c7a102..339f06513e58f8b144e3f83b2db4f69d5311c271 100644 |
--- a/lib/src/web_socket.dart |
+++ b/lib/src/web_socket/channel.dart |
@@ -1,23 +1,31 @@ |
-// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
// for details. All rights reserved. Use of this source code is governed by a |
// BSD-style license that can be found in the LICENSE file. |
import 'dart:async'; |
+import 'package:async/async.dart'; |
import 'package:crypto/crypto.dart'; |
+import 'package:stream_channel/stream_channel.dart'; |
-import 'copy/web_socket_impl.dart'; |
+import '../copy/web_socket_impl.dart'; |
-/// An implementation of the WebSocket protocol that's not specific to "dart:io" |
-/// or to any particular HTTP API. |
+/// A [StreamChannel] implementation of the WebSocket protocol. |
/// |
-/// Because this is HTTP-API-agnostic, it doesn't handle the initial [WebSocket |
-/// handshake][]. This needs to be handled manually by the user of the code. |
-/// Once that's been done, [new CompatibleWebSocket] can be called with the |
-/// underlying socket and it will handle the remainder of the protocol. |
+/// This is not specific to `dart:io` or to any particular HTTP API. Because of |
+/// that, it doesn't handle the initial [WebSocket handshake][]. This needs to |
+/// be handled manually by the user of the code. Once that's been done, [new |
+/// WebSocketChannel] can be called with the underlying socket and it will |
+/// handle the remainder of the protocol. |
/// |
/// [WebSocket handshake]: https://tools.ietf.org/html/rfc6455#section-4 |
-abstract class CompatibleWebSocket implements Stream, StreamSink { |
+class WebSocketChannel extends StreamChannelMixin { |
+ /// The underlying web socket. |
+ /// |
+ /// This is essentially a copy of `dart:io`'s WebSocket implementation, with |
+ /// the IO-specific pieces factored out. |
+ final WebSocketImpl _webSocket; |
+ |
/// The interval for sending ping signals. |
/// |
/// If a ping message is not answered by a pong message from the peer, the |
@@ -30,21 +38,30 @@ abstract class CompatibleWebSocket implements Stream, StreamSink { |
/// |
/// By default, the [pingInterval] is `null`, indicating that ping messages |
/// are disabled. |
- Duration pingInterval; |
+ Duration get pingInterval => _webSocket.pingInterval; |
+ set pingInterval(Duration value) => _webSocket.pingInterval = value; |
/// The [close code][] set when the WebSocket connection is closed. |
/// |
/// [close code]: https://tools.ietf.org/html/rfc6455#section-7.1.5 |
/// |
/// Before the connection has been closed, this will be `null`. |
- int get closeCode; |
+ int get closeCode => _webSocket.closeCode; |
/// The [close reason][] set when the WebSocket connection is closed. |
/// |
/// [close reason]: https://tools.ietf.org/html/rfc6455#section-7.1.6 |
/// |
/// Before the connection has been closed, this will be `null`. |
- String get closeReason; |
+ String get closeReason => _webSocket.closeReason; |
+ |
+ Stream get stream => new StreamView(_webSocket); |
+ |
+ /// The sink for sending values to the other endpoint. |
+ /// |
+ /// This has additional arguments to [WebSocketSink.close] arguments that |
+ /// provide the remote endpoint reasons for closing the connection. |
+ WebSocketSink get sink => new WebSocketSink._(_webSocket); |
/// Signs a `Sec-WebSocket-Key` header sent by a WebSocket client as part of |
/// the [initial handshake]. |
@@ -76,18 +93,22 @@ abstract class CompatibleWebSocket implements Stream, StreamSink { |
/// default); if it's a client, [serverSide] should be `false`. |
/// |
/// [WebSocket handshake]: https://tools.ietf.org/html/rfc6455#section-4 |
- factory CompatibleWebSocket(Stream<List<int>> stream, |
- {StreamSink<List<int>> sink, String protocol, bool serverSide: true}) { |
- if (sink == null) { |
- if (stream is! StreamSink) { |
- throw new ArgumentError("If stream isn't also a StreamSink, sink must " |
- "be passed explicitly."); |
- } |
- sink = stream as StreamSink; |
- } |
+ WebSocketChannel(StreamChannel<List<int>> channel, |
+ {String protocol, bool serverSide: true}) |
+ : _webSocket = new WebSocketImpl.fromSocket( |
+ channel.stream, channel.sink, protocol, serverSide); |
+} |
- return new WebSocketImpl.fromSocket(stream, sink, protocol, serverSide); |
- } |
+/// The sink exposed by a [CompatibleWebSocket]. |
+/// |
+/// This is like a normal [StreamSink], except that it supports extra arguments |
+/// to [close]. |
+class WebSocketSink extends DelegatingStreamSink { |
+ final WebSocketImpl _webSocket; |
+ |
+ WebSocketSink._(WebSocketImpl webSocket) |
+ : super(webSocket), |
+ _webSocket = webSocket; |
/// Closes the web socket connection. |
/// |
@@ -97,16 +118,6 @@ abstract class CompatibleWebSocket implements Stream, StreamSink { |
/// |
/// [close code]: https://tools.ietf.org/html/rfc6455#section-7.1.5 |
/// [reason]: https://tools.ietf.org/html/rfc6455#section-7.1.6 |
- Future close([int closeCode, String closeReason]); |
-} |
- |
-/// An exception thrown by [CompatibleWebSocket]. |
-class CompatibleWebSocketException implements Exception { |
- final String message; |
- |
- CompatibleWebSocketException([this.message]); |
- |
- String toString() => message == null |
- ? "CompatibleWebSocketException" : |
- "CompatibleWebSocketException: $message"; |
+ Future close([int closeCode, String closeReason]) => |
+ _webSocket.close(closeCode, closeReason); |
} |