startWebSocketServer function

Stream<WebSocketChannel> startWebSocketServer (Uri uri)

Starts a WebSocket server at uri.

A stream returned by this function will emit a WebSocketChannel everytime a new client connects to the server. Each channel can be used to communicate with its respective client.

Implementation

Stream<WebSocketChannel> startWebSocketServer(Uri uri) {
  final controller = new StreamController<WebSocketChannel>();
  final handler = webSocketHandler((WebSocketChannel channel) {
    controller.add(channel);
  });
  io.serve(handler, uri.host, uri.port).then((server) {
    new Logger(loggers.webSocket).info(
        'Running WebSocket server at ws://${server.address.host}:${server.port}');
  });
  return controller.stream;
}