S
S
Sedbol2020-02-23 20:07:11
Node.js
Sedbol, 2020-02-23 20:07:11

Why is the sent message from the server not visible in the message event?

wss.on('connection', function(ws,req) {
    const user=getUser(req.url);
    ws.onclient=user['viewer_id'];
    wss.clients.forEach(function each(client) {
        if (client.readyState === WebSocket.OPEN ){
            client.send(JSON.stringify({doubleClient:user['viewer_id']}));
                    }
    });
    if(MD5(user['api_id']+"_"+user['viewer_id']+"_"+appkey)!==user['auth_key'] || !user['auth_key']){
        console.info('Conn End')
        ws.close()
    }else{
        console.info("OK")
    }
    ws.on('close', function (data) {
        console.info('exit')
    });
    ws.on('message', function (data) {
        console.info(data)
        try{
         const userSend=JSON.parse(data);
         if(userSend===data.doubleClient){
             console.info('double')
         }

         console.info(userSend)
            
        }catch (e) {
            console.info('Не JSON')
            return false
        }
    })
});


Why is the message event not coming on the server? Everything is great for the client. Is it not visible on the server?
How can I make the sent message visible on the server in the message event when the connection event occurs? And not just on the client. In general, this is the task. If a user with the given ID already has an open connection, then close it and leave only the last one connected. Example: A user is connected to a socket and works with it. If the same ID enters the server, close another connection (According to the system, opening two tabs in the browser, if you open the second one, the first one closes) you need to implement the same with a socket connection. If, for example, a user opened a tab in another browser and successfully logged in, then close the same first connection and leave the newly connected one.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Shohruh Shaimardonov, 2020-02-23
@Sedbol

Your question is much easier to search on Google: websocket single session management
Here is the actual article in which the author solves a similar problem for his project

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question