Sending message to a specific connected users using webSocket?

后端 未结 5 2133
Happy的楠姐
Happy的楠姐 2020-11-29 15:43

I wrote a code for broadcasting a message to all users:

// websocket and http servers
var webSocketServer = require(\'websocket\').server;

...
...
v         


        
5条回答
  •  执笔经年
    2020-11-29 15:59

    For people using ws version 3 or above. If you want to use the answer provided by @ma11hew28, simply change this block as following.

    webSocketServer.on('connection', function (webSocket) {
      var userID = parseInt(webSocket.upgradeReq.url.substr(1), 10)
    
    webSocketServer.on('connection', function (webSocket, req) {
      var userID = parseInt(req.url.substr(1), 10)
    

    ws package has moved upgradeReq to request object and you can check the following link for further detail.

    Reference: https://github.com/websockets/ws/issues/1114

提交回复
热议问题