I'm following a tutorial on how to build a game with WebSockets. However, I don't know how to test my code; it says that localhost doesn't work. Can you explain to me why it's not working? (code)
const http = require("http")
const websocketServer = require("websocket").server
const httpServer = http.createServer();
httpServer.listen(9090, () => console.log("Listrening... on 9090"))
// hashmap
const clients = {}
const wsServer = new websocketServer({
"httpServer": httpServer
})
wsServer.on("request", request => {
// connect
const connection = request.accept(null, request.origin);
connection.on("open", () => console.log("opened!"))
connection.on("closed", () => console.log("closed!"))
connection.on("message", message => {
const result = JSON.parse(message.utf8Data)
// i have recieved a message from the client
console.log(result)
})
// generate a new clientId
const clientId = guid();
clients[clientId] = {
"connection": connection
}
const payLoad = {
"method": "connect",
"clientId": clientid
}
// send back the client connect
connection.send(JSON.stringify(payLaod))
})
function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
// then to call it, plus stitch in '4' in the third group
const guid = () => (S4() + S4() + "-" + S4() + "-4" + S4().substr(0,3) + "-" + S4() + "-" + S4() + S4() + S4()).toLowerCase();
Thanks for your help! (sorry if this is a stupid question - I'm very new to game dev w/ javascript)
¡Adios!