redstone_web_socket 0.1.0

  • README.md
  • Installing
  • Versions
  • 8

Redstone WebSocket

Build Status

redstone_web_socket is a web socket plugin for Redstone.dart. It uses the shelf_web_socket package to create web socket handlers.

Using @WebSocketHandler with functions

If a function is annotated with @WebSocketHandler, it'll be invoked with a WebSocketChannel object for every new established connection:

@WebSocketHandler("/ws")
onConnection(WebSocketChannel channel) {
  channel.stream.listen((message) {
    channel.sink.add("echo $message");
  });
}

Using @WebSocketHandler with classes

If a class is annotated with @WebSocketHandler, the plugin will install a event listener for every method annotated with @OnOpen, @OnMessage, @OnError and @OnClose:


@WebSocketHandler("/ws")
class ServerEndPoint {

  @OnOpen()
  void onOpen(WebSocketSession session) {
    print("connection established");
  }

  @OnMessage()
  void onMessage(String message, WebSocketSession session) {
    print("message received: $message");
    session.connection.add("echo $message");
  }

  @OnError()
  void onError(error, WebSocketSession session) {
    print("error: $error");
  }

  @OnClose()
  void onClose(WebSocketSession session) {
    print("connection closed");
  }

}

Like redstone groups, the class will be instantiated only once, and it can request injectable objects with a constructor (see dependency injection).

Installing handlers

To install web socket handlers, you just have to import redstone_web_socket.dart and call getWebSocketPlugin():

import 'package:redstone/server.dart' as app;
import 'package:redstone_web_socket/redstone_web_socket.dart';

void main() {
  app.setupConsoleLog();

  //install web socket handlers
  app.addPlugin(getWebSocketPlugin());

  app.start();
}

Unit Tests

This package also provides a simple mock client, which can be used in unit tests:

import 'package:redstone/server.dart' as app;
import 'package:redstone_web_socket/redstone_web_socket.dart';
import 'package:unittest/unittest.dart';

main() {

  test("Test echo service", () {

    var completer = new Completer();
    var socket = new MockWebSocket();

    socket.listen((message) {

      expect(message, equals("echo message"));

      completer.complete();
    });

    openMockConnection("/ws", socket);

    socket.add("message");

    return completer.future;

  });

}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  redstone_web_socket: ^0.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:redstone_web_socket/redstone_web_socket.dart';
  
Version Uploaded Documentation Archive
0.1.0 Dec 28, 2016 Go to the documentation of redstone_web_socket 0.1.0 Download redstone_web_socket 0.1.0 archive
0.0.2 Oct 12, 2014 Go to the documentation of redstone_web_socket 0.0.2 Download redstone_web_socket 0.0.2 archive
0.0.1 Aug 27, 2014 Go to the documentation of redstone_web_socket 0.0.1 Download redstone_web_socket 0.0.1 archive
0.1.0-beta.1 Mar 22, 2015 Go to the documentation of redstone_web_socket 0.1.0-beta.1 Download redstone_web_socket 0.1.0-beta.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
15
Health:
Code health derived from static analysis. [more]
0
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
0
Overall:
Weighted score of the above. [more]
8
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://www.dartlang.org/dart-2#migration.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.13.0 <2.0.0