问题描述
我遇到了这个确切的问题:https://github.com/rethinkdb/rethinkdb/issues/6503
I'm having this exact problem:https://github.com/rethinkdb/rethinkdb/issues/6503
我第一次连接时,它 console.logs 1 次.如果我刷新它 console.log 2 次.如果我再次刷新,它会控制台记录 3 次.等等.继续添加一个console.log/run 到每次重新加载.与 socket.emit 相同.每次重新加载时它会不断增加 1 个.
First time I connect, it console.logs 1 time.If I refresh it console.log 2 times.If I refresh again, it console logs 3 times. and so on.Keep adding one more console.log / run to each reload. The same with socket.emit. It keeps adding 1 extra on each reload.
在上面的链接中,他使用以下代码描述了如何修复它:
In the link above he describes how to fix it, with this code:
options.socket.on('close', () => {
conn.close({ noreplyWait: false });
});
我很想测试这个,但不知道怎么做?任何人都可以帮助我朝着正确的方向前进吗?
I would love to test this, but have no idea how to? Can anyone help me in the right direction?
我的服务器代码如下所示:
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
I also found someone who fixes it this way: https://gist.github.com/jamilservicos/e7c09e3701a7ba5ff817096ef8426d94
对于一件我认为不会有问题的小东西,它就像是很多代码.我在上面的代码中做错了什么吗?
It just seams like a lot of code for a small thing I wouldn't think should be a problem. Am I doing anything wrong in my code above?
知道如何解决问题或如何从第一个链接测试解决方案吗?
Any idea how to fix the issue or how I can test the solution from the first link?
推荐答案
我终于解决了!
我补充说:
io.removeAllListeners();
右下:
io.on('connection', function(socket){
所以现在看起来像这样:
So now it looks like this:
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 或发出.
And it works and I don't get multiple messages / console.logs or emits anymore.
这篇关于Socket.io changefeed 在刷新/重新加载时多次发出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!