I use the Dart WebSocket classes (both dart: io and dart: html) to connect to the Dart WebSocket server. When my client closes the web socket connection with a custom closing code and reason, the custom code is installed correctly, but the custom closing reason is not.
Here is a 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.');
}
standard output
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 that connects to the local Dart server) connects and closes, but there is no reason for the absence (empty line). The second web socket (the one that connects to echo.websocket.org) connects and closes, and both the closing codes and the reason are set correctly.
, , . , Dart WebSocket? WebSocketTransformer/WebSocket?