Code Monkey home page Code Monkey logo

web_socket_channel's Introduction

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, an implementation that wraps dart:io's WebSocket class, and a similar implementation that wrap's dart:html's.

It also provides constants for the WebSocket protocol's pre-defined status codes in the status.dart library. It's strongly recommended that users import this library should be imported with the prefix status.

import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/status.dart' as status;

main() async {
  var channel = await IOWebSocketChannel.connect("ws://localhost:1234");

  channel.stream.listen((message) {
    channel.sink.add("received!");
    channel.close(status.goingAway);
  });
}

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.stream.listen((message) {
    // ...
  });
}

HtmlWebSocketChannel

The HtmlWebSocketChannel class wraps dart:html's WebSocket class. Because it imports dart:html, it has its own library, package:web_socket_channel/html.dart. This allows the main WebSocketChannel class to be available on all platforms.

An HtmlWebSocketChannel can be created by passing a dart:html WebSocket to its constructor. It's more common to want to connect directly to a ws:// or wss:// URL, in which case new HtmlWebSocketChannel.connect() should be used.

import 'package:web_socket_channel/html.dart';

main() async {
  var channel = new HtmlWebSocketChannel.connect("ws://localhost:8181");
  channel.sink.add("connected!");
  channel.stream.listen((message) {
    // ...
  });
}

web_socket_channel's People

Contributors

devoncarew avatar fox32 avatar kevmoo avatar leafpetersen avatar loint avatar lrhn avatar nex3 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.