Benedict Lewis
Benedict Lewis

Reputation: 2813

Pair WebSocket Clients

I am creating a basic one-on-one chat, using Node.js and WebSocket. Every time a client connects, they are sent their ID, and the MD5 hash of a salt+id. Then, they need to be paired with another client. When they are paired, they are sent the ID and MD5 hash of the salt+partnerid. Every time a message is sent, the hash is checked. This is to ensure that they cannot just change the value of the Javascript ID variable and reroute their message.

[part of] server.js

var salt = "kPNtvp2UoBQRBcJ";
var count = 0;
var clients = {};

wsServer.on('request', function(r){
    var connection = r.accept('echo-protocol', r.origin);

    var id = count++;

    clients[id] = connection;
    console.log((new Date()) + ' Connection accepted [' + id + ']');
    clients[id].sendUTF(JSON.stringify({"type": "id", "id": id, "hash": md5(salt+id)}));

    connection.on('message', function(message) {
        var data = JSON.parse(message.utf8Data);
        console.log((new Date()) + ' New ' + data.type + ' sent from ' + data.from.id + ' to ' + data.to.id + ': ' + data.message);

        if(checkHash(data.from.id, data.from.hash) && checkHash(data.to.id, data.to.hash)){
            clients[data.to.id].sendUTF(message.utf8Data);
            clients[data.from.id].sendUTF(message.utf8Data);
        }else{
            console.log((new Date()) + ' Client hashes invalid, alerting sender and intended recipient.');
            clients[data.from.id].sendUTF(JSON.stringify({"type": "message", "message": "Our system has detected that you attempted to reroute your message by modifying the Javascript variables. This is not allowed, and subsequent attempts may result in a ban. The user you attempted to contact has also been notified.", "from": {"id": "system", "hash": ""}, "to": {"id": data.to.id, "hash": ""}}));
            clients[data.to.id].sendUTF(JSON.stringify({"type": "message", "message": "Someone you are not chatting with just attempted to send you a message by exploiting our system, however we detected it and blocked the message. If you recieve any subsequent messages that seem unusual, please be sure to report them.", "from": {"id": "system", "hash": ""}, "to": {"id": data.to.id, "hash": ""}}));
        }
    });
    connection.on('close', function(reasonCode, description) {
        delete clients[id];
        console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
    });
});

Now this all works fine, but my problem is pairing the clients. In order to pair them, a message has to be sent to both clients that looks like this:

{"type": "partner", "id": PARTNERID, "hash": md5(sald+id)}

One way I thought of finding partners would be to send a message to all clients asking if they had a partner, and then matching the ones which replied false but I think that it might be easier to keep track of clients server side. Which one should I do, and what would the code look like?

Upvotes: 0

Views: 875

Answers (1)

xvidun
xvidun

Reputation: 477

Instead of broadcasting from the server to every client for activity, why not keep track in the server by checking for the number of clients disconnected from a chat and requesting for partners. Using this data you can pair the clients required by taking them from the queue. You can also ensure that you are pairing clients that are only requesting to be paired.

Upvotes: 2

Related Questions