Socket.io changefeed 在刷新/重新加载时发出多个
Socket.io changefeed multiple emits on refresh / reload
我遇到了这个确切的问题:
https://github.com/rethinkdb/rethinkdb/issues/6503
我第一次连接,console.logs 1 次。
如果我刷新它 console.log 2 次。
如果我再次刷新,它会记录 3 次控制台日志。因此 on.Keep 每次重新加载时再添加一个 console.log / 运行。与 socket.emit 相同。它会在每次重新加载时不断增加 1。
在上面的 link 中,他描述了如何修复它,代码如下:
options.socket.on('close', () => {
conn.close({ noreplyWait: false });
});
我很想对此进行测试,但不知道如何进行?任何人都可以在正确的方向上帮助我吗?
我的 服务器 代码如下所示:
io.on('connection', function(socket){
console.log('a user connected: ' + socket.id);
r.table('Groups')
.filter(r.row("members")
.contains(req.user['id']))
.changes({ includeInitial: true })
.run()
.then(function(feed){
feed.each(function(err, item){
console.log(req.user['entra']);
//console.log(JSON.stringify(item, null, 2));
socket.emit('group',item.new_val);
})
})
});
和客户端代码:
var url = 'http://'+top.location.hostname+':4200';
var socket = io.connect(url);
socket.on('group', function(msg){
console.log(msg);
if (msg != null) {
$('span#groupName').html(msg['groupName']);
}else{
$('span#groupName').html('Not member of any group yet');
}
});
我也找到了这样修复的人:https://gist.github.com/jamilservicos/e7c09e3701a7ba5ff817096ef8426d94
它只是为一件我认为不成问题的小事拼接了很多代码。我在上面的代码中做错了什么吗?
知道如何解决问题或如何从第一个 link 开始测试解决方案吗?
我终于解决了!
我补充了:
io.removeAllListeners();
正下方:
io.on('connection', function(socket){
所以现在看起来像这样:
io.on('connection', function(socket){
io.removeAllListeners();
console.log('a user connected: ' + socket.id);
r.table('Groups')
.filter(r.row("members")
.contains(req.user['id']))
.changes({ includeInitial: true })
.run()
.then(function(feed){
feed.each(function(err, item){
console.log(req.user['entra']);
//console.log(JSON.stringify(item, null, 2));
socket.emit('group',item.new_val);
})
})
});
它有效,我不再收到多条消息/console.logs或发出消息。
我遇到了这个确切的问题: https://github.com/rethinkdb/rethinkdb/issues/6503
我第一次连接,console.logs 1 次。 如果我刷新它 console.log 2 次。 如果我再次刷新,它会记录 3 次控制台日志。因此 on.Keep 每次重新加载时再添加一个 console.log / 运行。与 socket.emit 相同。它会在每次重新加载时不断增加 1。
在上面的 link 中,他描述了如何修复它,代码如下:
options.socket.on('close', () => {
conn.close({ noreplyWait: false });
});
我很想对此进行测试,但不知道如何进行?任何人都可以在正确的方向上帮助我吗?
我的 服务器 代码如下所示:
io.on('connection', function(socket){
console.log('a user connected: ' + socket.id);
r.table('Groups')
.filter(r.row("members")
.contains(req.user['id']))
.changes({ includeInitial: true })
.run()
.then(function(feed){
feed.each(function(err, item){
console.log(req.user['entra']);
//console.log(JSON.stringify(item, null, 2));
socket.emit('group',item.new_val);
})
})
});
和客户端代码:
var url = 'http://'+top.location.hostname+':4200';
var socket = io.connect(url);
socket.on('group', function(msg){
console.log(msg);
if (msg != null) {
$('span#groupName').html(msg['groupName']);
}else{
$('span#groupName').html('Not member of any group yet');
}
});
我也找到了这样修复的人:https://gist.github.com/jamilservicos/e7c09e3701a7ba5ff817096ef8426d94
它只是为一件我认为不成问题的小事拼接了很多代码。我在上面的代码中做错了什么吗?
知道如何解决问题或如何从第一个 link 开始测试解决方案吗?
我终于解决了!
我补充了:
io.removeAllListeners();
正下方:
io.on('connection', function(socket){
所以现在看起来像这样:
io.on('connection', function(socket){
io.removeAllListeners();
console.log('a user connected: ' + socket.id);
r.table('Groups')
.filter(r.row("members")
.contains(req.user['id']))
.changes({ includeInitial: true })
.run()
.then(function(feed){
feed.each(function(err, item){
console.log(req.user['entra']);
//console.log(JSON.stringify(item, null, 2));
socket.emit('group',item.new_val);
})
})
});
它有效,我不再收到多条消息/console.logs或发出消息。