Complete signaling server

Here is the entire code for our signaling server. This includes logging in and handling all response types. I also added a listening handler at the end to notify you when the server is ready to accept WebSocket connections:

var WebSocketServer = require(''ws'').Server,
    wss = new WebSocketServer({ port: 8888 }),
    users = {};

wss.on(''connection'', function (connection) {
  connection.on(''message'', function (message) {
    var data;

    try {
      data = JSON.parse(message);
    } catch (e) {
      console.log(""Error parsing JSON"");
      data = {};
    }

    switch (data.type) {
      case ""login"":
        console.log(""User logged in as"", data.name);
        if (users[data.name]) {
          sendTo(connection, {
            type: ""login",
            success: false
          });
        } else {
          users[data.name] = connection;
          connection.name = data.name;
          sendTo(connection, {
            type: "login",
            success: true
          });
        }

        break;
      case "offer":
        console.log("Sending offer to", data.name);
        var conn = users[data.name];

        if (conn != null) {
          connection.otherName = data.name;
          sendTo(conn, {
            type: "offer",
            offer: data.offer,
            name: connection.name
          });
        }

        break;
      case "answer":
        console.log("Sending answer to", data.name);
        var conn = users[data.name];

        if (conn != null) {
          connection.otherName = data.name;
          sendTo(conn, {
            type: "answer",
            answer: data.answer
          });
        }

        break;
      case "candidate":
        console.log("Sending candidate to", data.name);
        var conn = users[data.name];

        if (conn != null) {
          sendTo(conn, {
            type: "candidate",
            candidate: data.candidate
          });
        }

        break;
      case "leave":
        console.log("Disconnecting user from", data.name);
        var conn = users[data.name];
        conn.otherName = null;

        if (conn != null) {
          sendTo(conn, {
            type: "leave"
          });
        }

        break;
      default:
        sendTo(connection, {
          type: "error",
          message: "Unrecognized command: " + data.type
        });

        break;
    }
  });

  connection.on('close', function () {
    if (connection.name) {
      delete users[connection.name];

      if (connection.otherName) {
        console.log("Disconnecting user from", connection.otherName);
        var conn = users[connection.otherName];
        conn.otherName = null;

        if (conn != null) {
          sendTo(conn, {
            type: "leave"
          });
        }
      }
    }
  });
});

function sendTo(conn, message) {
  conn.send(JSON.stringify(message));
}

wss.on('listening', function () {
    console.log("Server started...");
});

Tip

You may notice that we are using an unsecure WebSocket server in our example. In the real world, the WebSocket protocol actually supports SSL similar to how HTTP supports HTTPS. You can simply use wss:// to enable this when connecting the server.

Feel free to test our server application using the WebSocket client just as you did earlier. You can even try connecting three, four, or more users to the server and see how it handles multiple connections. You will probably also find many use cases that our server does not handle. It is a good idea to note these cases and even improve the server to work around them.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.111.9