Question

i've got a nowjs question:

how do you refuse a client connection from the event handler for the connect event?

i'd like to check some values on the server when a client connects and then decide if i drop the connection or let him connect.

ok, since stackoverflow somehow thinks my question is somehow below their quality standard, i'll try adding a code sample...

everyone.on('connect', function(){
    if (!niceClient) {
        // do something to kick him out
    }
});

thanks in advance!

Was it helpful?

Solution

I'm not sure about the specifics of nowjs but using plain node.js you could do something like this:

var http = require('http')
http.createServer(function (req, res) {
  // ...
}).on('connection', function(sock) {
  if (!niceClient) {
    sock.end(); // Close the client connection.
  }
}).listen(8080, 'localhost');

It looks like nowjs just wraps a node HTTP server so presumably this sort of 'connection' event listener will work with that library as well.

OTHER TIPS

try this

nowjs.on('connect', function(){
    nowjs.getClient(this.user.clientId, function(err){ 
        var address = this.socket.handshake.address;
        //@todo test if ip is blocked or banned
        console.log("New connection from " + address.address + ":" + address.port);

         //bla bla bla ....
           if (!niceClient) {
               this.socket.end(); // Close the client connection.
           }               
    });
});

i now found out that access control is just not (yet) implemented in nowjs. this might be a feature for later addition but as of right now there is not much to do than to implement a homegrown solution.

i will now solve this as follows:

all the relevant action is done through a group (e.g. "niceusers") and after successful validation the user is placed in this group. if there is no successful validation he remains in "limbo" and does not recieve any of the group's communication.

this solves my case but it might not be enough for real access control.

still hope this might help someone!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top