Js 如何区分客户端

我尝试在 node.js 中使用套接字,我成功了,但是我不知道如何在我的代码中区分客户端。 关于插座的部分是这样的:

var WebSocketServer = require('ws').Server,
wss = new WebSocketServer({port: 8080});
wss.on('connection', function(ws) {
ws.on('message', function(message) {
console.log('received: %s', message);
ws.send(message);
});
ws.send('something');
});

这段代码在我的客户端 js 中运行良好。

但是我希望向某个特定用户或服务器上打开套接字的所有用户发送一条消息。

在我的例子中,我作为客户端发送一条消息,然后收到一个响应,但是其他用户什么也没有显示。

例如,我希望 user1通过 webSocket 向服务器发送一条消息,然后我向已经打开套接字的 user2发送一个通知。

113384 次浏览

This code snippet in Worlize server really helped me a lot. Even though you're using ws, the code should be easily adaptable. I've selected the important parts here:

// initialization
var connections = {};
var connectionIDCounter = 0;


// when handling a new connection
connection.id = connectionIDCounter ++;
connections[connection.id] = connection;
// in your case you would rewrite these 2 lines as
ws.id = connectionIDCounter ++;
connections[ws.id] = ws;


// when a connection is closed
delete connections[connection.id];
// in your case you would rewrite this line as
delete connections[ws.id];

Now you can easily create a broadcast() and sendToConnectionId() function as shown in the linked code.

Hope that helps.

You can simply assign users ID to an array CLIENTS[], this will contain all users. You can directly send message to all users as given below:

var WebSocketServer = require('ws').Server,
wss = new WebSocketServer({port: 8080}),
CLIENTS=[];


wss.on('connection', function(ws) {
CLIENTS.push(ws);
ws.on('message', function(message) {
console.log('received: %s', message);
sendAll(message);
});
ws.send("NEW USER JOINED");
});


function sendAll (message) {
for (var i=0; i<CLIENTS.length; i++) {
CLIENTS[i].send("Message: " + message);
}
}

I'm using fd from the ws object. It should be unique per client.

var clientID = ws._socket._handle.fd;

I get a different number when I open a new browser tab.

The first ws had 11, the next had 12.

It depends which websocket you are using. For example, the fastest one, found here: https://github.com/websockets/ws is able to do a broadcast via this method:

var WebSocketServer = require('ws').Server,
wss = new WebSocketServer({host:'xxxx',port:xxxx}),
users = [];
wss.broadcast = function broadcast(data) {
wss.clients.forEach(function each(client) {
client.send(data);
});
};

Then later in your code you can use wss.broadcast(message) to send to all. For sending a PM to an individual user I do the following:

(1) In my message that I send to the server I include a username (2) Then, in onMessage I save the websocket in the array with that username, then retrieve it by username later:

wss.on('connection', function(ws) {


ws.on('message', function(message) {


users[message.userName] = ws;

(3) To send to a particular user you can then do users[userName].send(message);

By clients if you mean the open connections, then you can use ws.upgradeReq.headers['sec-websocket-key'] as the identifier. And keep all socket objects in an array.

But if you want to identify your user then you'll need to add user specific data to socket object.

You can check the connection object. It has built-in identification for every connected client; you can find it here:

let id=ws._ultron.id;
console.log(id);

In nodejs you can directly modify the ws client and add custom attributes for each client separately. Also you have a global variable wss.clients that can be used anywhere. Please try the following code with at least two clients connected:

var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({
server: httpsServer
});




wss.getUniqueID = function () {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
return s4() + s4() + '-' + s4();
};


wss.on('connection', function connection(ws, req) {
ws.id = wss.getUniqueID();


wss.clients.forEach(function each(client) {
console.log('Client.ID: ' + client.id);
});
});

You can also pass parameters directly in the client connection URL:

https://myhost:8080?myCustomParam=1111&myCustomID=2222

In the connection function you can get these parameters and assign them directly to your ws client:

wss.on('connection', function connection(ws, req) {


const parameters = url.parse(req.url, true);


ws.uid = wss.getUniqueID();
ws.chatRoom = {uid: parameters.query.myCustomID};
ws.hereMyCustomParameter = parameters.query.myCustomParam;
}

One possible solution here could be appending the deviceId in front of the user id, so we get to separate multiple users with same user id but on different devices.

ws://xxxxxxx:9000/userID/<<deviceId>>

you can use request header 'sec-websocket-key'

wss.on('connection', (ws, req) => {
ws.id = req.headers['sec-websocket-key'];


//statements...
});

If someone here is maybe using koa-websocket library, server instance of WebSocket is attached to ctx along side the request. That makes it really easy to manipulate the wss.clients Set (set of sessions in ws). For example pass parameters through URL and add it to Websocket instance something like this:

const wss = ctx.app.ws.server
const { userId } = ctx.request.query


try{


ctx.websocket.uid = userId


}catch(err){
console.log(err)
}

Use a global counter variable and assign its value for every new connection:

const wss = new WebSocket.Server({server});
let count_clients = 0;
wss.on('connection', function connection(ws){
ws.id=count_clients++;
console.log(`new connection, ws.id=${ws.id}, ${ws._socket.remoteAddress}:${ws._socket.remotePort} #clients=${wss.clients.size}`);
ws.on('close', req => {console.log(`disconnected, ws.id=${ws.id}, ${ws._socket.remoteAddress}:${ws._socket.remotePort} #clients=${wss.clients.size}`);});
...

Here is what I did:

* on connect, server generate an unique id (e.g uuid) for the connection,
* save it in memory, (e.g as key of map),
* send back to client in response,
*
*
* client save the id, on each request will also send the id as part of request data,
* then server identify the client by id, on receive further request,
*
* server maintain client, e.g cleanup on close/error,
*

I've impl the idea, it works well to identify the client.
And, I also achieved group/topic broadcast based on the idea, which need the server to maintain extra info.