S
S
stas91732019-11-17 14:50:50
JavaScript
stas9173, 2019-11-17 14:50:50

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);
    }
  });
})

customer:
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));
})

I need to implement a private chat, and for this I have a session running in php. How to send a session to the server when starting the site, if possible?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2019-11-17
@stas9173

Go to node.js/socket.io/express

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question