javascript - Is it possible to keep an id for WebSocket in js -
i'm trying create dialogue system using websockets on both client , server side. goal keep list of connections can iterate through them fast , ws value it's user id.
planning save websocket in array key id of it's owner don't know if can save additional info in websocket instance , if user can read or change data it?
what's safest , fastest way save data?
my current code is:
const websocketserver = require('ws').server; var wss = new websocketserver({ server: httpsserver }); connected = []; count = 0; wss.on('connection', function(ws) { ws.on('message', function incoming(message) { console.log('received: %s', message); data = json.parse(message); type=data[0]; if(type == 0){ connected[data[1][0]] = ws; ws.id = data[1][0]; console.log(ws.id); } else if (type == 1){ touser = connected[data[1][0]]; touser.send(data[1][1]); } }); ws.on('close', function (ws) { console.log(ws.id); connected.slice(ws.id, 1); }); }); i understand don't change ws variable when message cause log in close me undefined how think should work.
Comments
Post a Comment