-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to Reject a Connection Request? #517
Comments
This is not straight up built in, but you could add it into your program (Note, this will not block the connection from ever happening, more it will allow you to implement a handshake of your own).
For example you could check the ip of the socket. http://stackoverflow.com/questions/14822708/how-to-get-client-ip-address-using-websocket-einaros-ws-lib-in-node-js Also, your link is broken. |
Sorry for the broken link. It looks like the Stack Overflow moderators deleted my question. I'm not sure why. Thank you for suggesting I close a connection immediately after it's been accepted. The |
I am not a ws dev, so there may be a super special undocumented way that is not obvious by looking at the code. However... I figured it out! Now gaze upon my ugly test code!
You need to create your own http server and then listen for when it gets upgraded. |
@mattdipasquale There is such a thing, as @woverton mentioned:
The WebSocketServer server constructor accepts a parameter called verifyClient. You can figure out more on how to use this from the WebSocketServer unit tests found here: https://github.com/websockets/ws/blob/master/test/WebSocketServer.test.js Also, if you want to detect in the client that the connection was closed due to authentication/authorization issues, you can use the 'unexpected-response' event. This event is specific to the ws module, other clients may handle this differently or not handle it at all. You can find more details on how to use this in the WebSocket unit tests found here: |
You can use |
how would you detect the client's IP, for example, at verifyClient? For example, if you wanted to blacklist them? |
options.verifyClient = (info) => {
if (info.req.connection.remoteAddress === IP ) return true;
return false;
}; |
Why following code causes that after first connection with invalid JSON data any other connections are closed (error from chrome: "WebSocket connection to ... failed: Connection closed before receiving a handshake response") although JSON data are valid? Is it correct to use "return" keyword in onconnection callback? wss.on('connection', function(ws, req){
// validate input
try {
var data = JSON.parse(decodeURIComponent(req.url.substring(1)));
}
catch(e) {
console.log('connection closed due to invalid JSON input data');
ws.close();
return;
};
// input is ok, do something...
console.log('Hello');
} In browser I use somethink like this: // to test connection with valid input JSON data
new WebSocket('ws://server:port/' + encodeURIComponent(JSON.stringify({ key: 'value' })));
// to test connection with INVALID input JSON data
new WebSocket('ws://server:port/invalidjson'); |
http://stackoverflow.com/questions/31030115/node-js-ws-reject-connection-request
The text was updated successfully, but these errors were encountered: