improve pub score (#33)
Update analyzer and clean up warnings
diff --git a/.travis.yml b/.travis.yml
index 3ef6c26..d8ab216 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -5,7 +5,7 @@
dart_task:
- test: --platform vm
- test: --platform firefox -j 1
- - dartanalyzer
+ - dartanalyzer --fatal-infos --fatal-warnings
matrix:
include:
@@ -17,5 +17,5 @@
only: [master]
cache:
- directories:
- - $HOME/.pub-cache
+ directories:
+ - $HOME/.pub-cache
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index fd217bd..d7111dd 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -23,7 +23,7 @@
### File headers
All files in the project must start with the following header.
- // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
+ // Copyright (c) 2018, 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.
diff --git a/README.md b/README.md
index 453022a..b2fbea6 100644
--- a/README.md
+++ b/README.md
@@ -56,7 +56,7 @@
`WebSocketChannel` also works as a cross-platform implementation of the
WebSocket protocol. Because it can't initiate or handle HTTP requests in a
-cross-platform way, the [`new WebSocketChannel()` constructor][new] takes an
+cross-platform way, the [`WebSocketChannel()` constructor][new] takes an
underlying [`StreamChannel`][stream_channel] over which it communicates using
the WebSocket protocol. It also provides the static [`signKey()`][signKey]
method to make it easier to implement the [initial WebSocket handshake][]. These
@@ -78,18 +78,18 @@
[io.WebSocket]: https://api.dartlang.org/latest/dart-io/WebSocket-class.html
An `IOWebSocketChannel` can be created by passing a `dart:io` WebSocket to
-[its constructor][new IOWebSocketChannel]. It's more common to want to connect
+[its constructor][IOWebSocketChannel]. It's more common to want to connect
directly to a `ws://` or `wss://` URL, in which case
-[`new IOWebSocketChannel.connect()`][IOWebSocketChannel.connect] should be used.
+[`IOWebSocketChannel.connect()`][IOWebSocketChannel.connect] should be used.
-[new IOWebSocketChannel]: https://www.dartdocs.org/documentation/web_socket_channel/latest/io/IOWebSocketChannel/IOWebSocketChannel.html
+[IOWebSocketChannel]: https://www.dartdocs.org/documentation/web_socket_channel/latest/io/IOWebSocketChannel/IOWebSocketChannel.html
[IOWebSocketChannel.connect]: https://www.dartdocs.org/documentation/web_socket_channel/latest/io/IOWebSocketChannel/IOWebSocketChannel.connect.html
```dart
import 'package:web_socket_channel/io.dart';
main() async {
- var channel = new IOWebSocketChannel.connect("ws://localhost:8181");
+ var channel = IOWebSocketChannel.connect("ws://localhost:8181");
channel.sink.add("connected!");
channel.stream.listen((message) {
// ...
@@ -107,18 +107,18 @@
[html.WebSocket]: https://api.dartlang.org/latest/dart-html/WebSocket-class.html
An `HtmlWebSocketChannel` can be created by passing a `dart:html` WebSocket to
-[its constructor][new HtmlWebSocketChannel]. It's more common to want to connect
+[its constructor][HtmlWebSocketChannel]. It's more common to want to connect
directly to a `ws://` or `wss://` URL, in which case
-[`new HtmlWebSocketChannel.connect()`][HtmlWebSocketChannel.connect] should be used.
+[`HtmlWebSocketChannel.connect()`][HtmlWebSocketChannel.connect] should be used.
-[new HtmlWebSocketChannel]: https://www.dartdocs.org/documentation/web_socket_channel/latest/html/HtmlWebSocketChannel/HtmlWebSocketChannel.html
+[HtmlWebSocketChannel]: https://www.dartdocs.org/documentation/web_socket_channel/latest/html/HtmlWebSocketChannel/HtmlWebSocketChannel.html
[HtmlWebSocketChannel.connect]: https://www.dartdocs.org/documentation/web_socket_channel/latest/html/HtmlWebSocketChannel/HtmlWebSocketChannel.connect.html
```dart
import 'package:web_socket_channel/html.dart';
main() async {
- var channel = new HtmlWebSocketChannel.connect("ws://localhost:8181");
+ var channel = HtmlWebSocketChannel.connect("ws://localhost:8181");
channel.sink.add("connected!");
channel.stream.listen((message) {
// ...
diff --git a/example/example.dart b/example/example.dart
new file mode 100644
index 0000000..b641fd0
--- /dev/null
+++ b/example/example.dart
@@ -0,0 +1,15 @@
+// Copyright (c) 2018, 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 'package:web_socket_channel/io.dart';
+import 'package:web_socket_channel/status.dart' as status;
+
+void main() async {
+ final channel = await IOWebSocketChannel.connect("ws://localhost:1234");
+
+ channel.stream.listen((message) {
+ channel.sink.add("received!");
+ channel.sink.close(status.goingAway);
+ });
+}
diff --git a/lib/html.dart b/lib/html.dart
index 6d5007a..3a34572 100644
--- a/lib/html.dart
+++ b/lib/html.dart
@@ -46,7 +46,7 @@
Stream get stream => _controller.foreign.stream;
final _controller =
- new StreamChannelController(sync: true, allowForeignErrors: false);
+ StreamChannelController(sync: true, allowForeignErrors: false);
WebSocketSink get sink => _sink;
WebSocketSink _sink;
@@ -64,12 +64,12 @@
/// [BinaryType.blob], they're delivered as [Blob]s instead.
HtmlWebSocketChannel.connect(url,
{Iterable<String> protocols, BinaryType binaryType})
- : this(new WebSocket(url.toString(), protocols)
+ : this(WebSocket(url.toString(), protocols)
..binaryType = (binaryType ?? BinaryType.list).value);
/// Creates a channel wrapping [webSocket].
HtmlWebSocketChannel(this._webSocket) {
- _sink = new _HtmlWebSocketSink(this);
+ _sink = _HtmlWebSocketSink(this);
if (_webSocket.readyState == WebSocket.OPEN) {
_listen();
@@ -84,8 +84,8 @@
// The socket API guarantees that only a single error event will be emitted,
// and that once it is no open or message events will be emitted.
_webSocket.onError.first.then((_) {
- _controller.local.sink.addError(
- new WebSocketChannelException("WebSocket connection failed."));
+ _controller.local.sink
+ .addError(WebSocketChannelException("WebSocket connection failed."));
_controller.local.sink.close();
});
diff --git a/lib/io.dart b/lib/io.dart
index 607d225..34c6bff 100644
--- a/lib/io.dart
+++ b/lib/io.dart
@@ -53,16 +53,16 @@
Map<String, dynamic> headers,
Duration pingInterval}) {
var channel;
- var sinkCompleter = new WebSocketSinkCompleter();
+ var sinkCompleter = WebSocketSinkCompleter();
var stream = StreamCompleter.fromFuture(
WebSocket.connect(url.toString(), headers: headers).then((webSocket) {
webSocket.pingInterval = pingInterval;
channel._webSocket = webSocket;
- sinkCompleter.setDestinationSink(new _IOWebSocketSink(webSocket));
+ sinkCompleter.setDestinationSink(_IOWebSocketSink(webSocket));
return webSocket;
- }).catchError((error) => throw new WebSocketChannelException.from(error)));
+ }).catchError((error) => throw WebSocketChannelException.from(error)));
- channel = new IOWebSocketChannel._withoutSocket(stream, sinkCompleter.sink);
+ channel = IOWebSocketChannel._withoutSocket(stream, sinkCompleter.sink);
return channel;
}
@@ -70,8 +70,8 @@
IOWebSocketChannel(WebSocket socket)
: _webSocket = socket,
stream = socket.handleError(
- (error) => throw new WebSocketChannelException.from(error)),
- sink = new _IOWebSocketSink(socket);
+ (error) => throw WebSocketChannelException.from(error)),
+ sink = _IOWebSocketSink(socket);
/// Creates a channel without a socket.
///
@@ -80,7 +80,7 @@
IOWebSocketChannel._withoutSocket(Stream stream, this.sink)
: _webSocket = null,
stream = stream.handleError(
- (error) => throw new WebSocketChannelException.from(error));
+ (error) => throw WebSocketChannelException.from(error));
}
/// A [WebSocketSink] that forwards [close] calls to a `dart:io` [WebSocket].
diff --git a/lib/src/channel.dart b/lib/src/channel.dart
index c62fd44..d701792 100644
--- a/lib/src/channel.dart
+++ b/lib/src/channel.dart
@@ -47,13 +47,13 @@
/// Before the connection has been closed, this will be `null`.
String get closeReason => _webSocket.closeReason;
- Stream get stream => new StreamView(_webSocket);
+ Stream get stream => StreamView(_webSocket);
/// The sink for sending values to the other endpoint.
///
/// This supports additional arguments to [WebSocketSink.close] that provide
/// the remote endpoint reasons for closing the connection.
- WebSocketSink get sink => new WebSocketSink._(_webSocket);
+ WebSocketSink get sink => WebSocketSink._(_webSocket);
/// Signs a `Sec-WebSocket-Key` header sent by a WebSocket client as part of
/// the [initial handshake][].
@@ -91,7 +91,7 @@
/// [WebSocket handshake]: https://tools.ietf.org/html/rfc6455#section-4
WebSocketChannel(StreamChannel<List<int>> channel,
{String protocol, Duration pingInterval, bool serverSide: true})
- : _webSocket = new WebSocketImpl.fromSocket(
+ : _webSocket = WebSocketImpl.fromSocket(
channel.stream, channel.sink, protocol, serverSide)
..pingInterval = pingInterval;
}
diff --git a/lib/src/copy/bytes_builder.dart b/lib/src/copy/bytes_builder.dart
index 1e37093..dfa392b 100644
--- a/lib/src/copy/bytes_builder.dart
+++ b/lib/src/copy/bytes_builder.dart
@@ -26,9 +26,9 @@
/// output. Default is `true`.
factory BytesBuilder({bool copy: true}) {
if (copy) {
- return new _CopyingBytesBuilder();
+ return _CopyingBytesBuilder();
} else {
- return new _BytesBuilder();
+ return _BytesBuilder();
}
}
@@ -71,7 +71,7 @@
// Start with 1024 bytes.
static const int _INIT_SIZE = 1024;
- static final _emptyList = new Uint8List(0);
+ static final _emptyList = Uint8List(0);
int _length = 0;
Uint8List _buffer;
@@ -79,7 +79,7 @@
_CopyingBytesBuilder([int initialCapacity = 0])
: _buffer = (initialCapacity <= 0)
? _emptyList
- : new Uint8List(_pow2roundup(initialCapacity));
+ : Uint8List(_pow2roundup(initialCapacity));
void add(List<int> bytes) {
int bytesLength = bytes.length;
@@ -119,22 +119,21 @@
} else {
newSize = _pow2roundup(newSize);
}
- var newBuffer = new Uint8List(newSize);
+ var newBuffer = Uint8List(newSize);
newBuffer.setRange(0, _buffer.length, _buffer);
_buffer = newBuffer;
}
List<int> takeBytes() {
if (_length == 0) return _emptyList;
- var buffer = new Uint8List.view(_buffer.buffer, 0, _length);
+ var buffer = Uint8List.view(_buffer.buffer, 0, _length);
clear();
return buffer;
}
List<int> toBytes() {
if (_length == 0) return _emptyList;
- return new Uint8List.fromList(
- new Uint8List.view(_buffer.buffer, 0, _length));
+ return Uint8List.fromList(Uint8List.view(_buffer.buffer, 0, _length));
}
int get length => _length;
@@ -169,14 +168,14 @@
if (bytes is Uint8List) {
typedBytes = bytes;
} else {
- typedBytes = new Uint8List.fromList(bytes);
+ typedBytes = Uint8List.fromList(bytes);
}
_chunks.add(typedBytes);
_length += typedBytes.length;
}
void addByte(int byte) {
- _chunks.add(new Uint8List(1)..[0] = byte);
+ _chunks.add(Uint8List(1)..[0] = byte);
_length++;
}
@@ -187,7 +186,7 @@
clear();
return buffer;
}
- var buffer = new Uint8List(_length);
+ var buffer = Uint8List(_length);
int offset = 0;
for (var chunk in _chunks) {
buffer.setRange(offset, offset + chunk.length, chunk);
@@ -199,7 +198,7 @@
List<int> toBytes() {
if (_length == 0) return _CopyingBytesBuilder._emptyList;
- var buffer = new Uint8List(_length);
+ var buffer = Uint8List(_length);
int offset = 0;
for (var chunk in _chunks) {
buffer.setRange(offset, offset + chunk.length, chunk);
diff --git a/lib/src/copy/io_sink.dart b/lib/src/copy/io_sink.dart
index 3a51ff1..9e9b7b9 100644
--- a/lib/src/copy/io_sink.dart
+++ b/lib/src/copy/io_sink.dart
@@ -15,7 +15,7 @@
class StreamSinkImpl<T> implements StreamSink<T> {
final StreamConsumer<T> _target;
- final Completer _doneCompleter = new Completer();
+ final Completer _doneCompleter = Completer();
StreamController<T> _controllerInstance;
Completer _controllerCompleter;
bool _isClosed = false;
@@ -43,7 +43,7 @@
Future addStream(Stream<T> stream) {
if (_isBound) {
- throw new StateError("StreamSink is already bound to a stream");
+ throw StateError("StreamSink is already bound to a stream");
}
if (_hasError) return done;
@@ -62,9 +62,9 @@
Future flush() {
if (_isBound) {
- throw new StateError("StreamSink is bound to a stream");
+ throw StateError("StreamSink is bound to a stream");
}
- if (_controllerInstance == null) return new Future.value(this);
+ if (_controllerInstance == null) return Future.value(this);
// Adding an empty stream-controller will return a future that will complete
// when all data is done.
_isBound = true;
@@ -77,7 +77,7 @@
Future close() {
if (_isBound) {
- throw new StateError("StreamSink is bound to a stream");
+ throw StateError("StreamSink is bound to a stream");
}
if (!_isClosed) {
_isClosed = true;
@@ -111,14 +111,14 @@
StreamController<T> get _controller {
if (_isBound) {
- throw new StateError("StreamSink is bound to a stream");
+ throw StateError("StreamSink is bound to a stream");
}
if (_isClosed) {
- throw new StateError("StreamSink is closed");
+ throw StateError("StreamSink is closed");
}
if (_controllerInstance == null) {
- _controllerInstance = new StreamController<T>(sync: true);
- _controllerCompleter = new Completer();
+ _controllerInstance = StreamController<T>(sync: true);
+ _controllerCompleter = Completer();
_target.addStream(_controller.stream).then((_) {
if (_isBound) {
// A new stream takes over - forward values to that stream.
diff --git a/lib/src/copy/web_socket_impl.dart b/lib/src/copy/web_socket_impl.dart
index a11072d..36ebd23 100644
--- a/lib/src/copy/web_socket_impl.dart
+++ b/lib/src/copy/web_socket_impl.dart
@@ -23,13 +23,8 @@
import 'web_socket.dart';
const String webSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
-const String _webSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
-const String _clientNoContextTakeover = "client_no_context_takeover";
-const String _serverNoContextTakeover = "server_no_context_takeover";
-const String _clientMaxWindowBits = "client_max_window_bits";
-const String _serverMaxWindowBits = "server_max_window_bits";
-final _random = new Random();
+final _random = Random();
// Matches _WebSocketOpcode.
class _WebSocketMessageType {
@@ -101,16 +96,16 @@
EventSink<dynamic /*List<int>|_WebSocketPing|_WebSocketPong*/ > _eventSink;
final bool _serverSide;
- final List _maskingBytes = new List(4);
- final BytesBuilder _payload = new BytesBuilder(copy: false);
+ final List _maskingBytes = List(4);
+ final BytesBuilder _payload = BytesBuilder(copy: false);
_WebSocketProtocolTransformer([this._serverSide = false]);
Stream<dynamic /*List<int>|_WebSocketPing|_WebSocketPong*/ > bind(
Stream<List<int>> stream) {
- return new Stream.eventTransformed(stream, (EventSink eventSink) {
+ return Stream.eventTransformed(stream, (EventSink eventSink) {
if (_eventSink != null) {
- throw new StateError("WebSocket transformer already used.");
+ throw StateError("WebSocket transformer already used.");
}
_eventSink = eventSink;
return this;
@@ -127,14 +122,14 @@
/// Process data received from the underlying communication channel.
void add(List<int> bytes) {
- var buffer = bytes is Uint8List ? bytes : new Uint8List.fromList(bytes);
+ var buffer = bytes is Uint8List ? bytes : Uint8List.fromList(bytes);
int index = 0;
int lastIndex = buffer.length;
if (_state == CLOSED) {
- throw new WebSocketChannelException("Data on closed connection");
+ throw WebSocketChannelException("Data on closed connection");
}
if (_state == FAILURE) {
- throw new WebSocketChannelException("Data on failed connection");
+ throw WebSocketChannelException("Data on failed connection");
}
while ((index < lastIndex) && _state != CLOSED && _state != FAILURE) {
int byte = buffer[index];
@@ -144,7 +139,7 @@
if ((byte & (RSV2 | RSV3)) != 0) {
// The RSV2, RSV3 bits must both be zero.
- throw new WebSocketChannelException("Protocol error");
+ throw WebSocketChannelException("Protocol error");
}
_opcode = (byte & OPCODE);
@@ -152,29 +147,29 @@
if (_opcode <= _WebSocketOpcode.BINARY) {
if (_opcode == _WebSocketOpcode.CONTINUATION) {
if (_currentMessageType == _WebSocketMessageType.NONE) {
- throw new WebSocketChannelException("Protocol error");
+ throw WebSocketChannelException("Protocol error");
}
} else {
assert(_opcode == _WebSocketOpcode.TEXT ||
_opcode == _WebSocketOpcode.BINARY);
if (_currentMessageType != _WebSocketMessageType.NONE) {
- throw new WebSocketChannelException("Protocol error");
+ throw WebSocketChannelException("Protocol error");
}
_currentMessageType = _opcode;
}
} else if (_opcode >= _WebSocketOpcode.CLOSE &&
_opcode <= _WebSocketOpcode.PONG) {
// Control frames cannot be fragmented.
- if (!_fin) throw new WebSocketChannelException("Protocol error");
+ if (!_fin) throw WebSocketChannelException("Protocol error");
} else {
- throw new WebSocketChannelException("Protocol error");
+ throw WebSocketChannelException("Protocol error");
}
_state = LEN_FIRST;
} else if (_state == LEN_FIRST) {
_masked = (byte & 0x80) != 0;
_len = byte & 0x7F;
if (_isControlFrame() && _len > 125) {
- throw new WebSocketChannelException("Protocol error");
+ throw WebSocketChannelException("Protocol error");
}
if (_len == 126) {
_len = 0;
@@ -212,14 +207,14 @@
_unmask(index, payloadLength, buffer);
}
// Control frame and data frame share _payloads.
- _payload.add(new Uint8List.view(buffer.buffer, index, payloadLength));
+ _payload.add(Uint8List.view(buffer.buffer, index, payloadLength));
index += payloadLength;
if (_isControlFrame()) {
if (_remainingPayloadBytes == 0) _controlFrameEnd();
} else {
if (_currentMessageType != _WebSocketMessageType.TEXT &&
_currentMessageType != _WebSocketMessageType.BINARY) {
- throw new WebSocketChannelException("Protocol error");
+ throw WebSocketChannelException("Protocol error");
}
if (_remainingPayloadBytes == 0) _messageFrameEnd();
}
@@ -253,9 +248,9 @@
for (int i = 3; i >= 0; i--) {
mask = (mask << 8) | _maskingBytes[(_unmaskingIndex + i) & 3];
}
- Int32x4 blockMask = new Int32x4(mask, mask, mask, mask);
+ Int32x4 blockMask = Int32x4(mask, mask, mask, mask);
Int32x4List blockBuffer =
- new Int32x4List.view(buffer.buffer, index, blockCount);
+ Int32x4List.view(buffer.buffer, index, blockCount);
for (int i = 0; i < blockBuffer.length; i++) {
blockBuffer[i] ^= blockMask;
}
@@ -274,14 +269,12 @@
void _lengthDone() {
if (_masked) {
if (!_serverSide) {
- throw new WebSocketChannelException(
- "Received masked frame from server");
+ throw WebSocketChannelException("Received masked frame from server");
}
_state = MASK;
} else {
if (_serverSide) {
- throw new WebSocketChannelException(
- "Received unmasked frame from client");
+ throw WebSocketChannelException("Received unmasked frame from client");
}
_remainingPayloadBytes = _len;
_startPayload();
@@ -304,10 +297,10 @@
_eventSink.close();
break;
case _WebSocketOpcode.PING:
- _eventSink.add(new _WebSocketPing());
+ _eventSink.add(_WebSocketPing());
break;
case _WebSocketOpcode.PONG:
- _eventSink.add(new _WebSocketPong());
+ _eventSink.add(_WebSocketPong());
break;
}
_prepareForNextFrame();
@@ -343,11 +336,11 @@
var payload = _payload.takeBytes();
if (payload.length > 0) {
if (payload.length == 1) {
- throw new WebSocketChannelException("Protocol error");
+ throw WebSocketChannelException("Protocol error");
}
closeCode = payload[0] << 8 | payload[1];
if (closeCode == WebSocketStatus.NO_STATUS_RECEIVED) {
- throw new WebSocketChannelException("Protocol error");
+ throw WebSocketChannelException("Protocol error");
}
if (payload.length > 2) {
closeReason = utf8.decode(payload.sublist(2));
@@ -358,11 +351,11 @@
break;
case _WebSocketOpcode.PING:
- _eventSink.add(new _WebSocketPing(_payload.takeBytes()));
+ _eventSink.add(_WebSocketPing(_payload.takeBytes()));
break;
case _WebSocketOpcode.PONG:
- _eventSink.add(new _WebSocketPong(_payload.takeBytes()));
+ _eventSink.add(_WebSocketPong(_payload.takeBytes()));
break;
}
_prepareForNextFrame();
@@ -405,10 +398,10 @@
_WebSocketOutgoingTransformer(this.webSocket);
Stream<List<int>> bind(Stream stream) {
- return new Stream<List<int>>.eventTransformed(stream,
+ return Stream<List<int>>.eventTransformed(stream,
(EventSink<List<int>> eventSink) {
if (_eventSink != null) {
- throw new StateError("WebSocket transformer already used");
+ throw StateError("WebSocket transformer already used");
}
_eventSink = eventSink;
return this;
@@ -437,7 +430,7 @@
opcode = _WebSocketOpcode.TEXT;
data = message.bytes;
} else {
- throw new ArgumentError(message);
+ throw ArgumentError(message);
}
} else {
opcode = _WebSocketOpcode.TEXT;
@@ -454,7 +447,7 @@
String reason = webSocket._outCloseReason;
List<int> data;
if (code != null) {
- data = new List<int>();
+ data = List<int>();
data.add((code >> 8) & 0xFF);
data.add(code & 0xFF);
if (reason != null) {
@@ -489,7 +482,7 @@
} else if (dataLength > 125) {
headerSize += 2;
}
- Uint8List header = new Uint8List(headerSize);
+ Uint8List header = Uint8List(headerSize);
int index = 0;
// Set FIN and opcode.
@@ -529,12 +522,12 @@
list = data;
} else {
if (data is Uint8List) {
- list = new Uint8List.fromList(data);
+ list = Uint8List.fromList(data);
} else {
- list = new Uint8List(data.length);
+ list = Uint8List(data.length);
for (int i = 0; i < data.length; i++) {
if (data[i] < 0 || 255 < data[i]) {
- throw new ArgumentError("List element is not a byte value "
+ throw ArgumentError("List element is not a byte value "
"(value ${data[i]} at index $i)");
}
list[i] = data[i];
@@ -549,9 +542,9 @@
for (int i = 3; i >= 0; i--) {
mask = (mask << 8) | maskBytes[i];
}
- Int32x4 blockMask = new Int32x4(mask, mask, mask, mask);
+ Int32x4 blockMask = Int32x4(mask, mask, mask, mask);
Int32x4List blockBuffer =
- new Int32x4List.view(list.buffer, 0, blockCount);
+ Int32x4List.view(list.buffer, 0, blockCount);
for (int i = 0; i < blockBuffer.length; i++) {
blockBuffer[i] ^= blockMask;
}
@@ -579,7 +572,7 @@
StreamSubscription _subscription;
bool _issuedPause = false;
bool _closed = false;
- final Completer _closeCompleter = new Completer<WebSocketImpl>();
+ final Completer _closeCompleter = Completer<WebSocketImpl>();
Completer _completer;
_WebSocketConsumer(this.webSocket, this.sink);
@@ -616,13 +609,13 @@
_ensureController() {
if (_controller != null) return;
- _controller = new StreamController(
+ _controller = StreamController(
sync: true,
onPause: _onPause,
onResume: _onResume,
onCancel: _onListen);
- var stream = _controller.stream
- .transform(new _WebSocketOutgoingTransformer(webSocket));
+ var stream =
+ _controller.stream.transform(_WebSocketOutgoingTransformer(webSocket));
sink.addStream(stream).then((_) {
_done();
_closeCompleter.complete(webSocket);
@@ -654,10 +647,10 @@
Future addStream(var stream) {
if (_closed) {
stream.listen(null).cancel();
- return new Future.value(webSocket);
+ return Future.value(webSocket);
}
_ensureController();
- _completer = new Completer();
+ _completer = Completer();
_subscription = stream.listen((data) {
_controller.add(data);
}, onDone: _done, onError: _done, cancelOnError: true);
@@ -693,8 +686,7 @@
class WebSocketImpl extends Stream with _ServiceObject implements StreamSink {
// Use default Map so we keep order.
- static final Map<int, WebSocketImpl> _webSockets =
- new Map<int, WebSocketImpl>();
+ static final Map<int, WebSocketImpl> _webSockets = Map<int, WebSocketImpl>();
static const int DEFAULT_WINDOW_BITS = 15;
static const String PER_MESSAGE_DEFLATE = "permessage-deflate";
@@ -720,14 +712,14 @@
WebSocketImpl.fromSocket(
Stream<List<int>> stream, StreamSink<List<int>> sink, this.protocol,
[this._serverSide = false]) {
- _consumer = new _WebSocketConsumer(this, sink);
- _sink = new StreamSinkImpl(_consumer);
+ _consumer = _WebSocketConsumer(this, sink);
+ _sink = StreamSinkImpl(_consumer);
_readyState = WebSocket.OPEN;
- var transformer = new _WebSocketProtocolTransformer(_serverSide);
+ var transformer = _WebSocketProtocolTransformer(_serverSide);
_subscription = stream.transform(transformer).listen((data) {
if (data is _WebSocketPing) {
- if (!_writeClosed) _consumer.add(new _WebSocketPong(data.payload));
+ if (!_writeClosed) _consumer.add(_WebSocketPong(data.payload));
} else if (data is _WebSocketPong) {
// Simply set pingInterval, as it'll cancel any timers.
pingInterval = _pingInterval;
@@ -762,7 +754,7 @@
_controller.close();
}, cancelOnError: true);
_subscription.pause();
- _controller = new StreamController(
+ _controller = StreamController(
sync: true,
onListen: () => _subscription.resume(),
onCancel: () {
@@ -790,10 +782,10 @@
if (_pingInterval == null) return;
- _pingTimer = new Timer(_pingInterval, () {
+ _pingTimer = Timer(_pingInterval, () {
if (_writeClosed) return;
- _consumer.add(new _WebSocketPing());
- _pingTimer = new Timer(_pingInterval, () {
+ _consumer.add(_WebSocketPing());
+ _pingTimer = Timer(_pingInterval, () {
// No pong received.
_close(WebSocketStatus.GOING_AWAY);
});
@@ -819,7 +811,7 @@
Future close([int code, String reason]) {
if (_isReservedStatusCode(code)) {
- throw new WebSocketChannelException("Reserved status code $code");
+ throw WebSocketChannelException("Reserved status code $code");
}
if (_outCloseCode == null) {
_outCloseCode = code;
@@ -836,7 +828,7 @@
}
if (_closeTimer == null) {
// When closing the web-socket, we no longer accept data.
- _closeTimer = new Timer(const Duration(seconds: 5), () {
+ _closeTimer = Timer(const Duration(seconds: 5), () {
// Reuse code and reason from the local close.
_closeCode = _outCloseCode;
_closeReason = _outCloseReason;
diff --git a/lib/src/sink_completer.dart b/lib/src/sink_completer.dart
index 04a915c..ae6e64e 100644
--- a/lib/src/sink_completer.dart
+++ b/lib/src/sink_completer.dart
@@ -18,7 +18,7 @@
///
/// Events can be added to the sink either before or after a destination sink
/// is set.
- final WebSocketSink sink = new _CompleterSink();
+ final WebSocketSink sink = _CompleterSink();
/// Returns [sink] typed as a [_CompleterSink].
_CompleterSink get _sink => sink;
@@ -37,7 +37,7 @@
/// A destination sink may be set at most once.
void setDestinationSink(WebSocketSink destinationSink) {
if (_sink._destinationSink != null) {
- throw new StateError("Destination sink already set");
+ throw StateError("Destination sink already set");
}
_sink._setDestinationSink(destinationSink);
}
@@ -75,7 +75,7 @@
Future get done {
if (_doneCompleter != null) return _doneCompleter.future;
if (_destinationSink == null) {
- _doneCompleter = new Completer.sync();
+ _doneCompleter = Completer.sync();
return _doneCompleter.future;
}
return _destinationSink.done;
@@ -120,7 +120,7 @@
/// Create [_controller] if it doesn't yet exist.
void _ensureController() {
- if (_controller == null) _controller = new StreamController(sync: true);
+ if (_controller == null) _controller = StreamController(sync: true);
}
/// Sets the destination sink to which events from this sink will be provided.
diff --git a/pubspec.yaml b/pubspec.yaml
index 6345dfe..8f1bf89 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,7 +1,7 @@
name: web_socket_channel
version: 1.0.9
-description: StreamChannel wrappers for WebSockets.
+description: StreamChannel wrappers for WebSockets. This package provides a cross-platform WebSocketChannel API, a cross-platform implementation of that API that communicates over an underlying StreamChannel, IOWebSocketChannel that wraps dart:io's WebSocket class, and a HtmlWebSocketChannel that wrap's dart:html's.
author: Dart Team <misc@dartlang.org>
homepage: https://github.com/dart-lang/web_socket_channel
diff --git a/test/html_test.dart b/test/html_test.dart
index 3ac49bc..fac1e91 100644
--- a/test/html_test.dart
+++ b/test/html_test.dart
@@ -24,7 +24,7 @@
hybridMain(StreamChannel channel) async {
var server = await HttpServer.bind('localhost', 0);
- server.transform(new WebSocketTransformer()).listen((webSocket) {
+ server.transform(WebSocketTransformer()).listen((webSocket) {
webSocket.listen((request) {
webSocket.add(request);
});
@@ -42,52 +42,52 @@
});
test("communicates using an existing WebSocket", () async {
- var webSocket = new WebSocket("ws://localhost:$port");
- channel = new HtmlWebSocketChannel(webSocket);
+ var webSocket = WebSocket("ws://localhost:$port");
+ channel = HtmlWebSocketChannel(webSocket);
- var queue = new StreamQueue(channel.stream);
+ var queue = StreamQueue(channel.stream);
channel.sink.add("foo");
expect(await queue.next, equals("foo"));
- channel.sink.add(new Uint8List.fromList([1, 2, 3, 4, 5]));
+ channel.sink.add(Uint8List.fromList([1, 2, 3, 4, 5]));
expect(await _decodeBlob(await queue.next), equals([1, 2, 3, 4, 5]));
webSocket.binaryType = "arraybuffer";
- channel.sink.add(new Uint8List.fromList([1, 2, 3, 4, 5]));
+ channel.sink.add(Uint8List.fromList([1, 2, 3, 4, 5]));
expect(await queue.next, equals([1, 2, 3, 4, 5]));
});
test("communicates using an existing open WebSocket", () async {
- var webSocket = new WebSocket("ws://localhost:$port");
+ var webSocket = WebSocket("ws://localhost:$port");
await webSocket.onOpen.first;
- channel = new HtmlWebSocketChannel(webSocket);
+ channel = HtmlWebSocketChannel(webSocket);
- var queue = new StreamQueue(channel.stream);
+ var queue = StreamQueue(channel.stream);
channel.sink.add("foo");
expect(await queue.next, equals("foo"));
});
test(".connect defaults to binary lists", () async {
- channel = new HtmlWebSocketChannel.connect("ws://localhost:$port");
+ channel = HtmlWebSocketChannel.connect("ws://localhost:$port");
- var queue = new StreamQueue(channel.stream);
+ var queue = StreamQueue(channel.stream);
channel.sink.add("foo");
expect(await queue.next, equals("foo"));
- channel.sink.add(new Uint8List.fromList([1, 2, 3, 4, 5]));
+ channel.sink.add(Uint8List.fromList([1, 2, 3, 4, 5]));
expect(await queue.next, equals([1, 2, 3, 4, 5]));
});
test(".connect can use blobs", () async {
- channel = new HtmlWebSocketChannel.connect("ws://localhost:$port",
+ channel = HtmlWebSocketChannel.connect("ws://localhost:$port",
binaryType: BinaryType.blob);
- var queue = new StreamQueue(channel.stream);
+ var queue = StreamQueue(channel.stream);
channel.sink.add("foo");
expect(await queue.next, equals("foo"));
- channel.sink.add(new Uint8List.fromList([1, 2, 3, 4, 5]));
+ channel.sink.add(Uint8List.fromList([1, 2, 3, 4, 5]));
expect(await _decodeBlob(await queue.next), equals([1, 2, 3, 4, 5]));
});
@@ -110,15 +110,15 @@
// TODO(nweiz): Make this channel use a port number that's guaranteed to be
// invalid.
- var channel = new HtmlWebSocketChannel.connect(
+ var channel = HtmlWebSocketChannel.connect(
"ws://localhost:${await serverChannel.stream.first}");
expect(channel.stream.toList(),
- throwsA(new TypeMatcher<WebSocketChannelException>()));
+ throwsA(TypeMatcher<WebSocketChannelException>()));
});
}
Future<List<int>> _decodeBlob(Blob blob) async {
- var reader = new FileReader();
+ var reader = FileReader();
reader.readAsArrayBuffer(blob);
await reader.onLoad.first;
return reader.result as Uint8List;
diff --git a/test/io_test.dart b/test/io_test.dart
index 5d0d433..187c1c9 100644
--- a/test/io_test.dart
+++ b/test/io_test.dart
@@ -19,8 +19,8 @@
test("communicates using existing WebSockets", () async {
server = await HttpServer.bind("localhost", 0);
- server.transform(new WebSocketTransformer()).listen((webSocket) {
- var channel = new IOWebSocketChannel(webSocket);
+ server.transform(WebSocketTransformer()).listen((webSocket) {
+ var channel = IOWebSocketChannel(webSocket);
channel.sink.add("hello!");
channel.stream.listen((request) {
expect(request, equals("ping"));
@@ -30,7 +30,7 @@
});
var webSocket = await WebSocket.connect("ws://localhost:${server.port}");
- var channel = new IOWebSocketChannel(webSocket);
+ var channel = IOWebSocketChannel(webSocket);
var n = 0;
channel.stream.listen((message) {
@@ -51,16 +51,15 @@
test(".connect communicates immediately", () async {
server = await HttpServer.bind("localhost", 0);
- server.transform(new WebSocketTransformer()).listen((webSocket) {
- var channel = new IOWebSocketChannel(webSocket);
+ server.transform(WebSocketTransformer()).listen((webSocket) {
+ var channel = IOWebSocketChannel(webSocket);
channel.stream.listen((request) {
expect(request, equals("ping"));
channel.sink.add("pong");
});
});
- var channel =
- new IOWebSocketChannel.connect("ws://localhost:${server.port}");
+ var channel = IOWebSocketChannel.connect("ws://localhost:${server.port}");
channel.sink.add("ping");
channel.stream.listen(
@@ -73,17 +72,16 @@
test(".connect with an immediate call to close", () async {
server = await HttpServer.bind("localhost", 0);
- server.transform(new WebSocketTransformer()).listen((webSocket) {
+ server.transform(WebSocketTransformer()).listen((webSocket) {
expect(() async {
- var channel = new IOWebSocketChannel(webSocket);
+ var channel = IOWebSocketChannel(webSocket);
await channel.stream.listen(null).asFuture();
expect(channel.closeCode, equals(5678));
expect(channel.closeReason, equals("raisin"));
}(), completes);
});
- var channel =
- new IOWebSocketChannel.connect("ws://localhost:${server.port}");
+ var channel = IOWebSocketChannel.connect("ws://localhost:${server.port}");
channel.sink.close(5678, "raisin");
});
@@ -95,9 +93,8 @@
request.response.close();
});
- var channel =
- new IOWebSocketChannel.connect("ws://localhost:${server.port}");
+ var channel = IOWebSocketChannel.connect("ws://localhost:${server.port}");
expect(channel.stream.toList(),
- throwsA(new TypeMatcher<WebSocketChannelException>()));
+ throwsA(TypeMatcher<WebSocketChannelException>()));
});
}
diff --git a/test/web_socket_test.dart b/test/web_socket_test.dart
index f2f263f..dab9656 100644
--- a/test/web_socket_test.dart
+++ b/test/web_socket_test.dart
@@ -15,7 +15,7 @@
group("using WebSocketChannel", () {
test("a client can communicate with a WebSocket server", () async {
var server = await HttpServer.bind("localhost", 0);
- server.transform(new WebSocketTransformer()).listen((webSocket) {
+ server.transform(WebSocketTransformer()).listen((webSocket) {
webSocket.add("hello!");
webSocket.listen((request) {
expect(request, equals("ping"));
@@ -24,7 +24,7 @@
});
});
- var client = new HttpClient();
+ var client = HttpClient();
var request = await client.openUrl(
"GET", Uri.parse("http://localhost:${server.port}"));
request.headers
@@ -35,8 +35,8 @@
var response = await request.close();
var socket = await response.detachSocket();
- var innerChannel = new StreamChannel<List<int>>(socket, socket);
- var webSocket = new WebSocketChannel(innerChannel, serverSide: false);
+ var innerChannel = StreamChannel<List<int>>(socket, socket);
+ var webSocket = WebSocketChannel(innerChannel, serverSide: false);
var n = 0;
await webSocket.stream.listen((message) {
@@ -69,8 +69,8 @@
response.contentLength = 0;
var socket = await response.detachSocket();
- var innerChannel = new StreamChannel<List<int>>(socket, socket);
- var webSocket = new WebSocketChannel(innerChannel);
+ var innerChannel = StreamChannel<List<int>>(socket, socket);
+ var webSocket = WebSocketChannel(innerChannel);
webSocket.sink.add("hello!");
var message = await webSocket.stream.first;