首页 > 解决方案 > socket.io client receives everything except for one emitter

问题描述

I have got client and server socket.io code. Every message is being received except for one handler: when i send the following to the client:

socket.on('loginb', function(login) {
console.log(1);
var a = bedrijven.filter(function(e) {
    return e.bedrijfsnaam == login.bnaam && e.pwd == login.pwd;
});
if (a[0] == null||a.length!=1||a==undefined) {
    socket.emit('wow', false);
} else {
    console.log(2,a);
    socket.emit('wow', a[0]);
}
});

everything until "412" will be logged, but the message won't be received. This is how the listener looks at the client side:

socket.on('wow', function(login) {
            console.log(26);
 });

26 is never getting logged.

The strange thing is: the emitter and the listener are on the same place as other emitters and listeners i have, and work the same. However, this does not work while the rest does. I also tried replacing socket.emit('wow', a[0]); by socket.emit('wow', 'string'); but no difference.

标签: javascriptnode.jssocket.io

解决方案


I found the answer thanks to @jfriend00's comment. Indeed, the problem was that the client did not receive the socket message because the page refreshed after the submit. This was prevented by adding a return statement at the end of the submit function.


推荐阅读