I want to provide a meaningful error to the client when too many users are connected or when they are connecting from an unsupported domain, so ...
I wrote a WebSocket server code:
var http = require('http'); var httpServer = http.createServer(function (request, response) { // i see this if i hit http://localhost:8001/ response.end('go away'); }); httpServer.listen(8001); // https://github.com/Worlize/WebSocket-Node/wiki/Documentation var webSocket = require('websocket'); var webSocketServer = new webSocket.server({ 'httpServer': httpServer }); webSocketServer.on('request', function (request) { var connection = request.reject(102, 'gtfo'); });
And some WebSocket client code:
var connection = new WebSocket('ws://127.0.0.1:8001'); connection.onopen = function (openEvent) { alert('onopen'); console.log(openEvent); }; connection.onclose = function (closeEvent) { alert('onclose'); console.log(closeEvent); } connection.onerror = function (errorEvent) { alert('onerror'); console.log(errorEvent); }; connection.onmessage = function (messageEvent) { alert('onmessage'); console.log(messageEvent); };
All I get is alert('onclose'); with a CloseEvent object registered in the console, without any status code or message that I can find. When I connect via ws://localhost:8001 , the httpServer callback is not included in the game, so I canβt catch it. The RFC assumes that I can send any status code other than 101 when there is a problem, but Chrome throws an error in the Unexpected response code: 102 console. If I call request.reject(101, 'gtfo') , implying that it was successful, I get a handshake error as I expected.
Not sure what else I can do. Can't get server response in Chrome WebSocket implementation now?
ETA:. This is really an unpleasant hack, I hope that is not what I should do.
var connection = request.accept(null, request.origin); connection.sendUTF('gtfo'); connection.close();
Langdon
source share