Nodejs + WebSockets - reject connection to the message

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(); 
+8
javascript websocket
source share
1 answer

I am the author of WebSocket-Node, and I also posted this answer to the corresponding issue on GitHub: https://github.com/Worlize/WebSocket-Node/issues/46

Unfortunately, the WebSocket protocol does not provide any specific mechanism for providing closed source code or reason at this stage when a client connection is refused. The rejection occurs as an HTTP response with an HTTP status of approximately 40x or 50x. The specification allows this, but does not specify the specific way in which the client should try to explain any specific error messages from such an answer.

In fact, connections should be rejected at this stage only when you reject the user from a prohibited source (i.e., someone from another site tries to connect to your website server without any connections) or when the user does not have permission to connect (i.e. they are not logged in). In the latter case, you should use a different code on your site: the user should not try to connect to the web connection if he is not logged in.

The code and reason why the WebSocket-Node allows you to specify here is the HTTP status code (for example, 404, 500, etc.) and the reason for including the HTTP header in the response as a non-standard "X-WebSocket-Reject-Reason" . This is mainly useful when analyzing a connection with a Sniffer package, such as WireShark. No browser has the ability to provide rejection codes or reasons for client-side JavaScript code when a connection is rejected in this way because it is not provided for in the WebSocket specification.

+11
source share

All Articles