Answer the question
In order to leave comments, you need to log in
How to pass the id of the WebSocket client?
Help, please, I can not understand something.
server:
var WebSocketServer = require('ws').Server,
wss = new WebSocketServer({ port: 3000 }),
CLIENTS=[];
wss.on('connection', function(ws){
console.log(CLIENTS.push(ws));
ws.on('message', function(message){
for (var i=0; i<CLIENTS.length; i++) {
console.log(message);
CLIENTS[i].send(message);
}
});
})
var ws = new WebSocket('ws://localhost:3000');
ws.onopen = function(){
console.log('open');
}
ws.onmessage = function(event){
var data = JSON.parse(event.data);
console.log(data);
}
$('#but').click(function(){
var sms = $sms.val(),
data = {
id: 'id',
sms: sms
};
ws.send(JSON.stringify(data));
})
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question