问题描述
我有一个非常基本的例子.这个问题之前在堆栈溢出本身中被问过多次,但我无法得到正确的答案,所以我将使用这个基本示例.
I have got a very basic example. This question has been asked previously multiple times in stack overflow itself but I could not get the right answer so I am going with this basic example.
服务器:
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(3000);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
socket.on('chat', function (data) {
var op = false;
if( data.id == '1234' ){
op = 'yes';
}else{
op = 'no';
}
socket.emit('result', { hello: op });
});
});
客户:
<html>
<body>
<button onclick="check()">Test Me :-)</button>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:3000');
var check = function(){
var data = { id : '234'};
socket.emit('chat', data);
socket.on('result', function(data){
console.log('The data is '+data)
})
}
</script>
</body>
</html>
当我第一次点击 test me 按钮时socket.emit('result', { hello: 'world' });它被发射一次.在控制台中我得到了这个打印:
When I click the test me button for the first timesocket.emit('result', { hello: 'world' });it is emitted one time. And in the console I am getting this printed:
console.log('The data is '+data)
但是当我再次点击时,我会打印三次:
But when I click once again I get this printed thrice:
console.log('The data is '+data)
console.log('The data is '+data)
console.log('The data is '+data)
当我第三次点击时,我被打印了六次:
When I click for the third time I get printed six times:
console.log('The data is '+data)
console.log('The data is '+data)
console.log('The data is '+data)
console.log('The data is '+data)
console.log('The data is '+data)
console.log('The data is '+data)
就像这样,它在不断增长.
Like this it is multiplying and going.
谁能告诉我如何解决这个问题.非常感谢您的帮助.谢谢!
Can anyone please give me the insight on how to solve this problem.Your help is greatly appreciated. Thanks!
推荐答案
我认为您正在添加越来越多的侦听器来结果"您进行的每次检查.
I think you are adding more and more listeners to 'result' each call you make to check.
第一次点击 -> 从 call 1 listener 调用 1 console.log
First time click -> call 1 console.log from call 1 listener
第二次点击 -> call 1 console.log from call 1 listener + call 2 console.log from call 1 listener + call 2 console.log from call 2 listener
Second time click -> call 1 console.log from call 1 listener + call 2 console.log from call 1 listener + call 2 console.log from call 2 listener
第三次 -> 之前的日志 + 从调用 1 监听器调用 3 console.log + 从调用 2 监听器调用 3 console.log 和从调用 3 监听器调用 3 console.log.
Third time -> The previous logs + call 3 console.log from call 1 listener + call 3 console.log from call 2 listener and call 3 console.log from call 3 listener.
尝试将 'result' 的侦听器置于函数之外:
Try putting the listener for 'result' out of the function:
<html>
<body>
<button onclick="check()">Test Me :-)</button>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:3000');
socket.on('result', function(data){
console.log('The data is '+data)
})
var check = function(){
var data = { id : '234'};
socket.emit('chat', data);
}
</script>
</body>
</html>
这篇关于socket.io 发出多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!