node.js - 为什么重新连接成功时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);
});
如我所见,服务器不会收到离线消息,但是当客户端重新连接成功时,服务器会收到所有离线消息。
解决方案
发射时socket.io 代码的基础:
if (this.connected) {
this.packet(packet);
} else {
this.sendBuffer.push(packet);
}
所以,offline messages A
将被推送到sendBuffer,然后offline messages B
将被推送到sendBuffer,然后当socket再次连接时,sendBuffer将被发送到服务器。
推荐阅读
- php - 获取全局或常量变量的配置值
- linux - Bash 命令删除多个嵌套目录
- typescript - 如何使用 importValue() 从现有 Cloudformation 堆栈中检索数组
- angular - Angular BehaviourSubject 有时只被触发
- python - Pandas:使用大文件的行和列的条件优化迭代
- reactjs - Webpack 5 别名无法正确解析
- tensorflow - fit() 按预期工作,但随后在 evaluate() 模型偶然执行
- java - 上传文件时http状态403禁止
- javascript - 如何将字符串文字联合类型与 TypeScript 中的字符串变量进行比较
- hangfire - 使用 LocalRequestsOnlyAuthorizationFilter 在 Hangfire 上获取 401