2017-07-28 97 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。

我知道這是非常簡單的步驟。但我可以幫助你更多。如果你能夠做到這一點很好,否則我可以幫你詳細。