socket.io发出多次 [英] socket.io emits multiple times

查看:265
本文介绍了socket.io发出多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本的例子.之前在堆栈溢出本身中已经多次问过这个问题,但是我找不到正确的答案,所以我将继续这个基本示例.

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>

当我第一次单击测试我"按钮时 socket.emit('result',{hello:'world'}); 它发射一次.在控制台中,我得到了打印:

When I click the test me button for the first time socket.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.

第一次单击->从呼叫1侦听器呼叫1 console.log

First time click -> call 1 console.log from call 1 listener

第二次单击->从呼叫1侦听器呼叫1 console.log +从呼叫1侦听器呼叫2 console.log从呼叫2侦听器呼叫2 console.log

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.

尝试将结果"的侦听器置于函数之外:

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发出多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆