Discuss / JavaScript / ws 在是使用了3.0以上的版本的一些问题

ws 在是使用了3.0以上的版本的一些问题

Topic source

Vincent_13

#1 Created at ... [Delete] [Delete and Lock User]

1、server.clients

{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);
    // ...
});

卧槽,这个地方我卡了半个小时,原来是这样

这才是有用的评论

12341234Zzz

#4 Created at ... [Delete] [Delete and Lock User]

let users = Array.from(this.wss.clients).map((client) => { return client.user; }

12341234Zzz

#5 Created at ... [Delete] [Delete and Lock User]

froeach无返回的


  • 1

Reply