Clone this repo:
  1. ed1dfc1 fix links in changelog by Kevin Moore · 3 months ago master
  2. a6725b4 test on oldest supported SDK by Kevin Moore · 3 months ago
  3. 35e194e enable and fix a number of lints by Kevin Moore · 3 months ago
  4. 3db5195 Fix readme URLs by Kevin Moore · 3 months ago
  5. 0f3c625 Support the latest pkg:http (dev dependency) by Kevin Moore · 3 months ago

Web Socket Handler for Shelf

shelf_web_socket is a Shelf handler for establishing WebSocket connections. It exposes a single function, webSocketHandler, which calls an onConnection callback with a WebSocketChannel object for every connection that's established.

import 'package:shelf/shelf_io.dart' as shelf_io;
import 'package:shelf_web_socket/shelf_web_socket.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

void main() {
  var handler = webSocketHandler((webSocket) {
    webSocket.stream.listen((message) {
      webSocket.sink.add("echo $message");
    });
  });

  shelf_io.serve(handler, 'localhost', 8080).then((server) {
    print('Serving at ws://${server.address.host}:${server.port}');
  });
}