2017-07-28 94 views
0

我已经使用webrtc,nodejs,web-socket构建了一个信令服务器。 我能够在本地运行服务器,但我想在heroku上托管服务器。我已经看遍了所有的互联网,但我找不到任何相关的东西。 如何在Heroku上托管此服务器?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文件的代码示例。

任何帮助将不胜感激?

回答

0

这里是:

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

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

第2步:通过cli创建一个heroku项目。 现在把你推上heroku。

我知道这是非常简单的步骤。但我可以帮助你更多。如果你能够做到这一点很好,否则我可以帮你详细。