2

I am doing a chat room using socket.io. When a client is offline(just turn off the wifi), I still send a message(named offline messages A) to the server through the message will not send success. After a while, the client will receive the disconnect event, ignore that, I still send messages(named offline messages B) to the server. After doing above, I turn on the wifi, and the client will reconnect success. Ok, the problem is that the server will receive all the offline messages(A, B) after the client reconnects success. I can not figure this out, how it goes from offline to reconnect success of the socket.io.

I know that the offline messages B is stored in the sendBuffer as described in the question. And the client will resend the messages in the sendBuffer when it reconnects success, but why the offline messages A still can send to the server success? I have coded to show this problem

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

As I see, the server will not receive the offline message, but the server receives all the offline messages when the client reconnects success.

lovegnep
  • 21
  • 4
  • Possible duplicate of [socket.io stop re-emitting event after x seconds/first failed attempt to get a response](https://stackoverflow.com/questions/32131629/socket-io-stop-re-emitting-event-after-x-seconds-first-failed-attempt-to-get-a-r) – Ouroborus Feb 13 '19 at 02:46
  • there are some differences with that questions, mine is more brief, and the answer in that question can not solve my question – lovegnep Feb 13 '19 at 03:44

1 Answers1

0

Base of socket.io code on emit:

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

so, offline messages A will be pushed to sendBuffer, then offline messages B will be pushed to sendBuffer, then when socket connect again, sendBuffer will be send to server.

Milad Aghamohammadi
  • 1,866
  • 1
  • 16
  • 29