{Set}
A set that stores all connected clients. Please note that this property is only added when the clientTracking is truthy.
server.clients 是一个Set 对象
function onConnect() {
let user = this.user;
let msg = createMessage('join', user, `${user.name} joined.`);
this.wss.broadcast(msg);
// build user list:
// *** 这里请使用 forEach 遍历
let users = this.wss.clients.map(function (client) {
return client.user;
});
this.send(createMessage('list', user, users));
}
2、Event: 'connection'
socket {WebSocket}
request {http.IncomingMessage}
Emitted when the handshake is complete. request is the http GET request sent by the client. Useful for parsing authority headers, cookie headers, and other information.
ws升级到 3.0后 upgradeReq was removed from WebSocket.
wss.on('connection', function (ws) {
let location = url.parse(ws.upgradeReq.url, true);
// ...
});
// ***
wss.on('connection', function (ws, req) {
ws.upgradeReq = req;
let location = url.parse(ws.upgradeReq.url, true);
// ...
});
Vincent_13
1、server.clients
{Set} A set that stores all connected clients. Please note that this property is only added when the clientTracking is truthy.
2、Event: 'connection'
socket {WebSocket} request {http.IncomingMessage} Emitted when the handshake is complete. request is the http GET request sent by the client. Useful for parsing authority headers, cookie headers, and other information.