commit | 51f8eff2f1cb5fad1592ddd17b227457a4369a0f | [log] [tgz] |
---|---|---|
author | Natalie Weizenbaum <nweiz@google.com> | Tue Mar 01 16:27:15 2016 -0800 |
committer | Natalie Weizenbaum <nweiz@google.com> | Tue Mar 01 16:27:15 2016 -0800 |
tree | 6fdf7f2b34a4d8ea83f568bbba4141222f049e13 | |
parent | cda6f1ecd05eedb88c49b3988c70f8b0dbc23c95 [diff] |
Get rid of WebSocketChannel.pingInterval. This is now set as a constructor parameter for implementations that support it. R=kevmoo@google.com Review URL: https://codereview.chromium.org//1759493002 .
The web_socket_channel
package provides StreamChannel
wrappers for WebSocket connections. It provides a cross-platform WebSocketChannel
API, a cross-platform implementation of that API that communicates over an underlying StreamChannel
, and an implementation that wraps dart:io
's WebSocket
class.
WebSocketChannel
The WebSocketChannel
class's most important role is as the interface for WebSocket stream channels across all implementations and all platforms. In addition to the base StreamChannel
interface, it adds a protocol
getter that returns the negotiated protocol for the socket, as well as closeCode
and closeReason
getters that provide information about why the socket closed.
The channel‘s sink
property is also special. It returns a WebSocketSink
, which is just like a StreamSink
except that its close()
method supports optional closeCode
and closeReason
parameters. These parameters allow the caller to signal to the other socket exactly why they’re closing the connection.
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 takes an underlying StreamChannel
over which it communicates using the WebSocket protocol. It also provides the static signKey()
method to make it easier to implement the initial WebSocket handshake. These are used in the shelf_web_socket
package to support WebSockets in a cross-platform way.
IOWebSocketChannel
The IOWebSocketChannel
class wraps dart:io
's WebSocket
class. Because it imports dart:io
, it has its own library, package:web_socket_channel/io.dart
. This allows the main WebSocketChannel
class to be available on all platforms.
An IOWebSocketChannel
can be created by passing a dart:io
WebSocket to its constructor. It's more common to want to connect directly to a ws://
or wss://
URL, in which case new IOWebSocketChannel.connect()
should be used.
import 'package:web_socket_channel/io.dart'; main() async { var channel = new IOWebSocketChannel.connect("ws://localhost:8181"); channel.sink.add("connected!"); channel.sink.listen((message) { // ... }); }