heroku上的主机信令服务器

时间:2017-07-28 09:53:58

标签: node.js webrtc

我使用webrtc,nodejs,web-socket构建了一个信令服务器。 我能够在本地运行服务器,但我想在heroku上托管服务器。 O已经遍布互联网,但我找不到任何相关的东西。 如何在Heroku上托管此服务器?

这是我的代码

var WebSocketServer = require('ws').Server;

var wss = new WebSocketServer({port: 9090});

var users = {};

wss.on('connection', function(connection) {

  console.log("User connected");

  //when server gets a message from a connected user
  connection.on('message', function(message) {

    var data;

    //accepting only JSON messages
    try {
      data = JSON.parse(message);
    } catch (e) {
      console.log("Invalid JSON");
      data = {};
    }

    //switching type of the user message
    switch (data.type) {
      //when a user tries to login
      case "login":
      console.log("User logged", data.name);

      //if anyone is logged in with this username then refuse
      if(users[data.name]) {
        sendTo(connection, {
          type: "login",
          success: false
        });
      } else {
        //save user connection on the server
        users[data.name] = connection;
        connection.name = data.name;

        sendTo(connection, {
          type: "login",
          success: true
        });
      }

      break;

      case "offer":
      //for ex. UserA wants to call UserB
      console.log("Sending offer to: ", data.name);

      //if UserB exists then send him offer details
      var conn = users[data.name];

      if(conn != null) {
        //setting that UserA connected with UserB
        connection.otherName = data.name;

        sendTo(conn, {
          type: "offer",
          offer: data.offer,
          name: connection.name
        });
      }

      break;

      case "answer":
      console.log("Sending answer to: ", data.name);
      //for ex. UserB answers UserA
      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 from", data.name);
      var conn = users[data.name];
      conn.otherName = null;

      //notify the other user so he can disconnect his peer connection
      if(conn != null) {
        sendTo(conn, {
          type: "leave"
        });
      }

      break;

      default:
      sendTo(connection, {
        type: "error",
        message: "Command not found: " + data.type
      });

      break;
    }

  });

  //when user exits, for example closes a browser window
  //this may help if we are still in "offer","answer" or "candidate" state
  connection.on("close", function() {

    if(connection.name) {
      delete users[connection.name];

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

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

  });

  connection.send("Hello world");
});

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

以上是server.js文件的代码示例。

任何帮助将不胜感激?

1 个答案:

答案 0 :(得分:0)

这是:

Step1:在github上推送你的代码。 在package.json中进行make更改之前。

Package.json是在任何云上运行服务器的重要文件。主要是启动脚本,依赖项和节点版本很重要。 在您的代码中也使用process.env.PORT而不是任何硬编码端口号。

Step2:通过cli创建一个heroku项目。 现在推动你在heroku上的分支。

我知道这是非常简短的步骤。但我可以帮助你更多。如果你能做到那么好,否则我可以帮你详细。