Node.js如何从Redis适配器获取socketio套接字数组

时间:2019-04-28 12:58:22

标签: node.js redis socket.io

我当前在我的socketio设置中使用redis适配器,我希望能够从redis适配器中获取一组套接字,以便我可以检查会话,并且仅将数据发送到我尝试过的正确帐户ID中以下代码,但返回空数组有帮助吗?

NET.getDashboardSockets = () => {
    return new Promise((resolve, reject) => {
        const sockets = [];
        NET._io.of("/dashboard").adapter.clients((err, clients) => {
            if (clients.length > 0 && err == null) {
                for (let i = 0; i < clients.length; i++) {
                    const client = clients[i];
                    const socketData = NET._io.of("/dashboard").to(client)
                    if (socketData != null) {
                        sockets.push(socketData);
                    }
                }
            }
            resolve(sockets);
        });
    })
}

NET.getDashboardSocketsWithId = (Account) => {
    return new Promise((resolve, reject) => {
        const retsockets = [];

        NET.getDashboardSockets().then(sockets => {
            for (let i = 0; i < sockets.length; i++) {
                const socket = sockets[i];
                if (socket.handshake != null) {
                    const acctId = socket.handshake.session.accountId;
                    if (acctId != null) {
                        if (acctId == Account.getId()) {
                            retsockets.push(socket);
                        }
                    }
                }
            }

            resolve(retsockets);
        })
    })
}

0 个答案:

没有答案