socket.io strahlt mehrere Male

Habe ich ein sehr einfaches Beispiel. Diese Frage wurde gebeten, die zuvor mehrfach im stack-überlauf selbst aber ich konnte nicht die richtige Antwort, so werde ich mit diesem einfachen Beispiel.

Server:

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 });
  });
});

Client:

<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>

Wenn ich auf die test-mir-button für die erste Zeit
socket.emit('Ergebnis', { hello: 'world' });
es emittiert wird, eine Zeit. Und in der Konsole bekomme ich diese ausgegeben:

console.log('The data is '+data)

Aber, wenn ich auf einmal wieder bekomme ich diese dreimal gedruckt:

console.log('The data is '+data)
console.log('The data is '+data)
console.log('The data is '+data)

Wenn ich auf für ich zum Dritten mal gedruckt werden sechs mal:

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) 

So ist es die Multiplikation und gehen.

Kann jemand bitte gib mir die Einsicht, wie dieses problem zu lösen.
Ihre Hilfe wird sehr geschätzt. Danke!

InformationsquelleAutor sdg | 2015-08-28
Schreibe einen Kommentar