ekweible
ekweible

Reputation: 499

Support WebSocket close reason with a Dart WebSocket server

I'm using Dart's WebSocket classes (both the dart:io and dart:html versions) to connect to a Dart WebSocket server. When I have the client close the web socket connection with a custom close code and reason, the custom code is set properly but the custom close reason is not.

Here's a reduced test case:

server.dart

library server;

import 'dart:io';

main() async {
  HttpServer server = await HttpServer.bind('localhost', 8081);
  server
      .transform(new WebSocketTransformer())
      .listen((WebSocket webSocket) {
    print('WebSocket opened.');
    webSocket.listen((_) {}, onDone: () {
      print('WebSocket closed.');
    });
  });
  print('Listening..');
}

test.dart

library test;

import 'dart:io';

main() async {
  WebSocket webSocket1 = await WebSocket.connect('ws://localhost:8081');
  webSocket1.listen((_) {}, onDone: () {
    print('Local Dart ws connection: closed with\n\t' +
          'code: ${webSocket1.closeCode}\n\t' +
          'reason: ${webSocket1.closeReason}');
  });
  webSocket1.close(4001, 'Custom close reason.');

  WebSocket webSocket2 = await WebSocket.connect('ws://echo.websocket.org');
  webSocket2.listen((_) {}, onDone: () {
    print('echo.websocket.org connection: closed with\n\t' +
          'code: ${webSocket2.closeCode}\n\t' +
          'reason: ${webSocket2.closeReason}');
  });
  webSocket2.close(4001, 'Custom close reason.');
}

stdout

Local Dart ws connection: closed with
    code: 4001
    reason:
echo.websocket.org connection: closed with
    code: 4001
    reason: Custom close reason.

The first web socket (the one connecting to the local Dart server) connects and closes, but the close reason is missing (blank string). The second web socket (the one connecting to echo.websocket.org) connects and closes, and both the close code and reason are set correctly.

It can't be an issue with the way the client connects, because it's identical in both cases. So is it an issue with the way I'm setting up the Dart WebSocket? Or is it a bug in the WebSocketTransformer/WebSocket class?

Upvotes: 4

Views: 911

Answers (1)

ekweible
ekweible

Reputation: 499

This has been addressed with an addition to dart-lang/sdk: https://github.com/dart-lang/sdk/issues/23964

Upvotes: 1

Related Questions