How can I convert a localhost server to a global one?
Hello everyone! So I am new to node.js(I installed it yesterday), but experienced with JavaScript. So I made a webSocket server using the ws module. So my first program I am making with node.js is a simple chatroom. It works just fine on the same computer, but it does not work cross computers. My question is how can I make it work between pcs? So I basically just want to change localhost to a global server. This is all my code:ServerCode:var server = require('ws').Server; var s = new server({ port: 5001,host:"" }); var amountofplayers = 0; var playerwebsockets = []; var removingone = 0; var userlist = []; function filtercheckthing(number) { if (number == removingone) { return false; } return true; } function givePlayerList() { sendMessageToAllClients("PlayerList:33:098:" + userlist.toString()) } function sendMessageToAllClients(message) { console.log("There are now " + amountofplayers + " players."); for (var i = 0; i < playerwebsockets.length; i++) { playerwebsockets[i].send(message); } } s.on('connection',function(ws){ ws.on('message', function (message) { sendMessageToAllClients("PlayerList:33:098:" + userlist.toString()); console.log("Received:" + message + " from the ip:" + ws._socket.remoteAddress.toString().split(":")[3]); if (message.toString().includes("ChatOpening:")) { sendMessageToAllClients("ChatMes:2" + message.split("ChatOpening:3")[1]); }else if (message.toString().includes("A user just connected")) { amountofplayers++; playerwebsockets.push(ws); sendMessageToAllClients("PlayerList:33:098:" + userlist.toString()); sendMessageToAllClients("AMOP39:" + amountofplayers); setTimeout(givePlayerList, 1); }else if (message == "A user just left") { removingone = ws; playerwebsockets.filter(filtercheckthing); amountofplayers--; sendMessageToAllClients("AMOP39:" + amountofplayers); } else if (message.toString().includes("UserLeave:::")) { //user just left removingone = message.split(":::")[1]; userlist.filter(filtercheckthing); sendMessageToAllClients("PlayerList:33:098:" + userlist.toString()); console.log(playerwebsockets); } else if (message.toString().includes("UserJoin:::")) { //user just joined userlist.push(message.split(":::")[1]); sendMessageToAllClients("PlayerList:33:098:" + userlist.toString()); console.log(playerwebsockets); } ws.send("Server Just Received:"+message); }); }); ClientCode:
Servers are down right now
I know the code is confusing, non-professional, and not secure, but it is my first node.js program, and I didn't intend for it to be professional. Thank you all very much!
Submitted June 06, 2019 at 08:30PM by Typical_Standard
No comments:
Post a Comment