首页 > 解决方案 > 为什么重新连接成功时socket.io客户端会重新发送离线消息?

问题描述

我正在使用 socket.io 做一个聊天室。当客户端离线时(只是关闭wifi),我仍然通过消息向服务器发送消息(名为离线消息A)不会发送成功。一段时间后,客户端将收到断开连接事件,忽略它,我仍然向服务器发送消息(名为离线消息 B)。完成以上操作后,我打开wifi,客户端会重新连接成功。好的,问题是客户端重新连接成功后,服务器会收到所有的离线消息(A,B)。我无法弄清楚它是如何从离线到重新连接 socket.io 成功的。

我知道离线消息 B 存储在 sendBuffer 中,如问题中所述。并且客户端重连成功时会重新发送sendBuffer中的消息,但是为什么离线消息A仍然可以成功发送到服务器呢?我已经编码来显示这个问题

/*
* Client side
*/
onClickSend = (value) => {
    console.log('send message:', value)
    this.socket.emit('chatMessage', value);
}
connect = () => {
    if (!this.socket) {
        console.log('connecting...')
        this.socket = io(url, {
            query: {
                clientId: this.clientId
            },
            forceNew:true,
        })
    }
    this.socket.on('connect',()=>{
        console.log('connected');
    })

    this.socket.on('error',(err)=>{
        console.log('error happen:',err);
    })
    this.socket.on('disconnect',(reason)=>{
        console.log('disconnect event:', reason);
    })
    this.socket.on('reconnect',(e)=>{
        console.log('reconnect success:' , e);
    })
}

/*
* Server side
*/
const app = require('express')();
const http = require('http').createServer(app);
const io = require('socket.io')(http)
const PORT  = 40001;
io.on('connection', function(socket) {
    const clientId = socket.handshake.query.clientId;
    console.log('client come...', clientId)

    socket.on('connect', function(data) {
        console.log('user connects socket');
    });

    socket.on('disconnect', (reason)=>{
        console.log('event disconnect: ', reason, clientId)
        socket.removeAllListeners()
        socket = null
    })
    socket.on('error', (err)=>{
        console.log('error happen: ', err, clientId)
    })

    // the server will receive all the offline messages(message A, B)
    // send after the network is down when the client 
    // reconnect success.
    socket.on('chatMessage', (message)=>{
        console.log('receive message: ', message, clientId)
        io.emit('chatMessage', message)
    })

});


http.listen(PORT, function() {
    console.log('listening on *:', PORT);
});

如我所见,服务器不会收到离线消息,但是当客户端重新连接成功时,服务器会收到所有离线消息。

标签: node.jssocket.io

解决方案


发射时socket.io 代码的基础:

if (this.connected) {
    this.packet(packet);
} else {
    this.sendBuffer.push(packet);
}

所以,offline messages A将被推送到sendBuffer,然后offline messages B将被推送到sendBuffer,然后当socket再次连接时,sendBuffer将被发送到服务器。


推荐阅读