Yes you are missing something, the complete server side implementation. ServerSocket
is just a plain socket used for TCP (or UDP).
But a websocket requires a HTTP server, that does the handling of the HTTP request and that upgrades the connection to a Websocket connection:
import 'dart:io';
void main() {
HttpServer.bind('127.0.0.1', port)
.then((HttpServer server) {
print('listening for connections on $port');
server.listen((HttpRequest request) {
if (request.uri.path == '/ws') {
WebSocketTransformer.upgrade(request).then((WebSocket websocket) {
websocket.listen((message) => print('Message from client'));
print('Client connected');
});
} else {
/* ... */
}
});
},
onError: (error) => print("Error starting HTTP server: $error"));
}
A simple example from SethLadd. But I would probably do also a check if the requests CONNECTION
and UPGRADE
headers are correct before upgrading to a websocket. After upgrading the connection you have a Websocket
instance that is similar to the on one the client side.