node.js - Host signalling server on heroku -
i have built signalling server using webrtc, nodejs, web-socket. able run server locally want host server on heroku. o have looked on internet cannot find relevant. how can host server on heroku?
here code
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 message connected user connection.on('message', function(message) { var data; //accepting json messages try { data = json.parse(message); } catch (e) { console.log("invalid json"); data = {}; } //switching type of user message switch (data.type) { //when user tries login case "login": console.log("user logged", data.name); //if logged in username refuse if(users[data.name]) { sendto(connection, { type: "login", success: false }); } else { //save user connection on server users[data.name] = connection; connection.name = data.name; sendto(connection, { type: "login", success: true }); } break; case "offer": //for ex. usera wants call userb console.log("sending offer to: ", data.name); //if userb exists send him offer details var conn = users[data.name]; if(conn != null) { //setting usera connected 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 other user can disconnect 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, example closes browser window //this may if still in "offer","answer" or "candidate" state connection.on("close", function() { if(connection.name) { delete users[connection.name]; if(connection.othername) { console.log("disconnecting ", 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)); }
above code sample of server.js file.
any appreciated?
here is:
step1: push code on github. before pushing make changes in package.json.
package.json important file run server on cloud. start script, dependencies , node version important. in code use process.env.port instead of hard coded port number.
step2: create heroku project through cli. push branch on heroku.
i know pretty brief steps. can more. if able good, otherwise can in detail.
Comments
Post a Comment