Identifying users
In a typical web application, the server will need a way to identify between connected clients. Most applications today use the one-identity rule and have each user login to a respective string-based identifier known as their username. We will also be using the same rule in our signaling application. It will not be as sophisticated as some of the methods used today since we will not even require a password from the user. We simply need an ID for each connection so we know where to send messages.
To start, we are going to change our connection
handler a bit, to look similar to this:
connection.on(''message'', function (message) { var data; try { data = JSON.parse(message); } catch (e) { console.log(""Error parsing JSON""); data = {}; } });
This will change our WebSocket implementation to only accept JSON messages. Since WebSocket connections are limited to strings and binary data, we need a way to send structured data across the wire. JSON...