wstalk 1.3.3

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 50

WSTalk

Library providing a natural interface to asynchronous network messaging by implementing a back-and-forth binary messaging protocol on top of WebSocket.

Message responses themselves can also be requests, which differs from plain RPC protocols, in that it allows back-and-forth trampolining between server and client without having to manually track conversation states. Additionally, requests can also be made for streams, which allows for efficient list queries to be easily implemented. Stream responses can recursively be requests in themselves, and so forth.

Usage

import 'dart:io';
import 'package:wstalk/wstalk.dart';

runServer() async {
  HttpServer server = await HttpServer.bind('127.0.0.1', 9090);
  await for (HttpRequest request in server) {
    if (request.uri.path == '/ws') {
      // Upgrade to WSTalk socket
      WebSocket ws = await WebSocketTransformer.upgrade(request);
      TalkSocket ts = new TalkSocket(ws);
      // Register incoming message types
      ts.stream(42).listen((TalkMessage) {
        ts.close();
        server.close();
      });
      // Listen
      ts.listen();
    } else {
      // ...
    }
  }
  print("Server exited");
}

runClient() async {
  TalkSocket ts = await TalkSocket.connect("ws://localhost:9090/ws");
  testClient(ts);
  await ts.listen();
  print("Client exited");
}

testClient(TalkSocket ts) async {
  // Ping three times
  for (int i = 0; i < 3; ++i) {
    print(await ts.ping());
  }
  // Multi-ping three times
  for (int i = 0; i < 3; ++i) {
    // Receive four responses for each
    await for (int dt in ts.multiPing()) {
      print(dt);
    }
  }
  // Tell the server to close
  ts.sendMessage(42, new List<int>());
}

main() {
  runServer();
  runClient(); 
}

1.3.3

  • Can now request for streams. Stream responses can recursively be requests as well.

1.2.1

  • Request messages can be replied to with an exception using sendException.
  • Request message timeout can be extended through the sendExtend function.
  • Formatted source.
  • Fixed some minor issues.

0.9.3

  • Initial version.

example/wstalk_example.dart

import 'dart:io';
import 'package:wstalk/wstalk.dart';

runServer() async {
  try {
    HttpServer server = await HttpServer.bind('127.0.0.1', 9090);
    try {
      await for (HttpRequest request in server) {
        try {
          if (request.uri.path == '/ws') {
            WebSocket ws = await WebSocketTransformer.upgrade(request);
            TalkSocket ts = new TalkSocket(ws);
            ts.stream(42).listen((TalkMessage) {
              ts.close();
              server.close();
            });
            () async { try {
              await ts.listen();
            } catch (ex) {
              print("Server socket listen exception:");
              print(ex);
            } }();
          } else {
            // ...
          }
        } catch (ex) {
          print("Request handling exception:");
          print(ex);
        }
      }
    } catch (ex) {
      print("Server listen exception:");
      print(ex);
    }
  } catch (ex) {
    print("Server bind exception:");
    print(ex);
  }
  print("Server exited");
}

runClient() async {
  try {
    TalkSocket ts = await TalkSocket.connect("ws://localhost:9090/ws");
    try {
      testClient(ts);
    } catch (ex) {
      print("Client test exception (should not occur):");
      print(ex);
    }
    try {
      await ts.listen();
    } catch (ex) {
      print("Client listen exception:");
      print(ex);
    }
  } catch (ex) {
    print("Client connect exception:");
    print(ex);
  }
  print("Client exited");
}

testClient(TalkSocket ts) async {
  // Ping three times
  for (int i = 0; i < 3; ++i) {
    print(await ts.ping());
  }
  // Tell the server to close
  ts.sendMessage(42, new List<int>());
}

main() {
  runServer();
  runClient();
}

Use this package as a library

1. Depend on it

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


dependencies:
  wstalk: ^1.3.3

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:


import 'package:wstalk/wstalk.dart';
  
Version Uploaded Documentation Archive
1.3.3 Aug 31, 2018 Go to the documentation of wstalk 1.3.3 Download wstalk 1.3.3 archive
1.2.1 Aug 6, 2018 Go to the documentation of wstalk 1.2.1 Download wstalk 1.2.1 archive
1.1.2 Jul 26, 2018 Go to the documentation of wstalk 1.1.2 Download wstalk 1.1.2 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Dec 5, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7

Platforms

Detected platforms: Flutter, other

Primary library: package:wstalk/wstalk.dart with components: io.

Health suggestions

Fix lib/src/wstalk_socket.dart. (-1 points)

Analysis of lib/src/wstalk_socket.dart reported 2 hints:

line 81 col 23: The stack trace variable 'stack' isn't used and can be removed.

line 91 col 23: The stack trace variable 'stack' isn't used and can be removed.

Format lib/src/wstalk_message.dart.

Run dartfmt to format lib/src/wstalk_message.dart.

Format lib/wstalk.dart.

Run dartfmt to format lib/wstalk.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.58.0 <3.0.0
meta ^1.1.5 1.1.6
Dev dependencies
test ^1.0.0