javascript - WebSocket connection established, onopen never runs -


i'm trying learn websockets , i've created websocket server in node , working on browser implementation. have tested server works , responds how want using chrome extension called smart websocket client.

the console in browser says button pressed! when press button , connection lost! (1000) when end node process never has said connection established!.

edit: client code running on site secured https , serves hsts header while server code (currently, won't continue be) running on localhost on normal http, if it's concern.

server code:

const websock = require('./node_modules/ws'); const hashmap = require('./node_modules/hashmap'); const jsonparse = require('./node_modules/jsonparse'); const randomstring = require('./node_modules/randomstring');  class session {     constructor(server) {         this.server = server;         this.clients = [];     } }  var connections = new hashmap(); const json = new jsonparse();  const wss = new websock.server({ port: 36245 });  process.on('sigint',function () {     console.log("recieved sigint, stopping gracefully...");     wss.clients.foreach(function (ws) {         console.log("-ended connection "+ws.upgradereq.socket.remoteaddress+" (1001)");         ws.closereasoncode = 1001;         ws.close();     });     process.exit(1); });  wss.on('connection',function connection(ws,conn) {    console.log("+recieved connection "+ws._socket.remoteaddress);    ws.upgradereq = conn;    ws.hashandshook = false;    ws.onmessage = function message(msg) {        var message;        try {            message = json.parse(msg.data);        } catch (ex) {            ws.send("{\"e\":\"invalid json.\"}");            return;        }        if (!ws.hashandshook) {            ws.hashandshook = true;            if (message.type === "client") {                //ensure code provided , has room                if (typeof message.code === 'undefined' || !connections.has(message.code)) {                    ws.send("{\"e\":\"invalid game code.\"}");                    ws.closereasoncode = 4001;                    ws.closedescription = "invalid game code.";                    console.log("-ended connection "+ws._socket.remoteaddress+ " (4001)");                    ws.close();                }                if (typeof message.name === 'undefined') {                    //todo error out, no player name provided                }                //attach client game session                ws.clienttype = "client";                ws.gamecode = message.code;                ws.playername =                connections.get(message.code).clients.add(ws);                ws.send("{\"joingame\":\"true\"}");            } else {                ws.send("{\"e\":\"invalid type provided on handshake message.\"}");                ws.closereasoncode = 4000;                ws.closedescription = "invalid type provided on handshake message.";                console.log("-ended connection "+ws._socket.remoteaddress+" (4000)");                ws.close();            }        }    };    ws.onclose = function close() {        console.log("-ended connection "+ws.upgradereq.socket.remoteaddress+" (client closed)");    } }); 

client code, run on press of button on page:

function dojoingame () {     console.log("button pressed!");     gamecode = document.getelementbyid('base-gamecode').value.touppercase();     playername = document.getelementbyid('base-playername').value;     var ws = new websocket("ws://localhost:36245");     ws.onopen = function (event) {         console.log("connection established!");         ws.send("{\"type\":\"client\",\"code\":\""+gamecode+"\",\"name\":\""+playername+"\"");     };     ws.onmessage = function (msg) {         let message = json.parse(msg.data);         if (message.joingame) { //if "client added session" message, set display: none; on codeentry div             document.getelementbyid('codeentry').style.display = "none";         }         //todo handle message     };     ws.onclose = function (evt) {         console.log("connection lost! ("+evt.code+":"+evt.reason+")");     }; } 

thank help!

problem fixed. attempting connect non secure websocket server secure origin , chrome & co. wasn't fan.


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -