Please kindly assist. Thanks in advance.
PS: I edit the view to show if it is connected or not. Below is the code.
var wsFactory = { tryCount: 3,
connect : function(url){
var ctx = this,
ws = new WebSocket(url);
return new Promise(function(v,x){
ws.onerror = e => { console.log('WS connection attempt ${4-ctx.tryCount} -> Unsuccessful');
e.target.readyState === 3 && --ctx.tryCount;
if (ctx.tryCount > 0) setTimeout(() => v(ctx.connect(url)), 1000);
else x(new Error("3 unsuccessfull connection attempts"));
};
ws.onopen = e => {
console.log('WS connection Status: ${e.target.readyState}');
console.log("here: "+client.user_id);
conn.send(JSON.stringify(client));
$('#messages').append('<font color="green">Successfully connected as user ' + client.user_id + '</font><br>');
v(ws);
};
ws.onmessage = m => {
console.log(m.data);
var data = JSON.parse(e.data);
if (data.message) {
$('#messages').append(data.user_id + ' : ' + data.message + '<br>');
}
if (data.type === 'token') {
$('#token').html('JWT Token : ' + data.token);
}
};
});
}
};
wsFactory.connect("ws://blablabla.com:8282")
.then(ws => ws.send("Hey..! This is my first socket message"))
.catch(console.log);