简体   繁体   English

socket.io发出未触发的回调

[英]socket.io emit callback not firing

function MainController () {
 var self = this;

self.mainInfo = null;

self.socket = io.connect('http:/192.168.56.101:3000');

self.login = function () {
self.socket.emit("login", "Hello");
}


self.socket.on('welcome', function () {
console.log('welcome');
});

self.socket.on('player leave', function () {
console.log('leave');
});


self.login();
};

Above is front-end code. 上面是前端代码。 I can receive emit messages, google chrome logs incoming message 我可以接收发射消息,谷歌浏览器会记录传入消息

5:::{"name":"welcome"}

But console.log('welcome'); 但是console.log('welcome'); doesn't work. 不起作用。 callback's don't fire 回调不触发

错别字:(

 self.socket = io.connect('http://192.168.56.101:3000');

You need to put self.login() inside 'connect' event handler because at the time login() got called, it's not guaranteed that the connection is established. 您需要将self.login()放入'connect'事件处理程序中,因为在调用login()时,不能保证建立连接。

self.socket = io.connect('http://192.168.56.101:3000');

    :
    :

self.socket.on('connect', function() {
    self.login();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM