Ws: Multiple connections, but single websocket.on("message") event emitter

Created on 19 Oct 2016  路  5Comments  路  Source: websockets/ws

Hi

I have the same issue . I am trying to implement a web chat using websocket and Er-lang. The whole set up of mine worked well . But now a facing an issue like the connection is getting replaced by latest connection. I'm not able to distinguish between the user. I have implemented in node js.
My code is below

This is the server side code
wss.on('connection', function (ws) {
function init(user,pass,loginStatus,id) {
//To initiate the xmpp connection
wsURI = "ws://52.77.212.110:5223/xmpp";
//create a new WebSocket object.
wsss = new webSocket(wsURI, [ 'xmpp' ]);

    wsss.onopen = function(evt) {
        if(loginStatus===true){
            login(user, pass,loginStatus);
        }
        else{
            wsss.close(3000,"Deliberate Connection");
        }
    };
    wsss.onclose = function(evt) {
        var code=evt.code;
        console.log('connection closed');
        if(code!==3000){
            if(loginStatus===true){
                console.log('connection started');
                login(user, pass,loginStatus);
            } 
        }

    };
    wsss.onmessage = function(evt) {
        processResponse(evt.data, user, domain,id);
    };
    wsss.onerror = function(evt) {
        console.log('Error Occured');
        console.log(evt);
    };
    this.wsss = wsss;
    // 
}

}

The client side code

var url = "ws://127.0.0.1:8087/";
socket = new WebSocket(url);

socket.onopen = function () {
var user=document.getElementById("user").value;
var secretCode=document.getElementById("secret-code").value;
setCookie("username", user, 365);
setCookie("user_id", getRandomInt(0, 10000), 365);
var bool=true;
//To set the username in session
var message = {};
message.user = user.trim();
message.pass = secretCode.trim();
message.id = getCookie("user_id");
message.type = "login";
message.connection=bool;
var msg=JSON.stringify(message);
socket.send(encodeURIComponent(msg));
};

Most helpful comment

Hi,

I have got solution for it. Thanks for your response.

All 5 comments

Can you please clarify and create an example to reproduce the issue?
The above code is kinda messy and hard to read.

Thanks.

Closing due to inactivity.

Hi,

I have got solution for it. Thanks for your response.

What is a solution?

What is a solution? Please!!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sherikapotein picture sherikapotein  路  3Comments

dcflow picture dcflow  路  4Comments

NodePing picture NodePing  路  5Comments

pmcalabrese picture pmcalabrese  路  4Comments

HanHtoonAung picture HanHtoonAung  路  3Comments