Book Image

Learning WebRTC

By : Daniel M. Ristic
Book Image

Learning WebRTC

By: Daniel M. Ristic

Overview of this book

Table of Contents (16 chapters)
Learning WebRTC
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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...