做一些socket.io工作。 好像我的发射回调没有被解雇。 我做得对吗?
this.logger.debug("joining room " + roomId); callback = function() { return this.logger.debug('finished emitting join for socket'); }; // this.config.ioTypes.join resolves to "j" this.socket.emit(this.config.ioTypes.join, { user: this.config.user, room: roomId }, callback);我的日志记录看起来是正确的(请注意,上面的代码不包括生成这些日志的所有内容):
DEBUG: joining room 1245 <-- first line from code above DEBUG: join event for socket etQIXGAm59DYkCPk1_gj DEBUG: data: { user: 1, room: '1245' } DEBUG: received join event for 1, data: { user: 1, room: '1245' }这就结束了。 看起来socket.io没有调用我的回调。 但文档很清楚:
有时,您可能希望在客户端确认消息接收时收到回调。
为此,只需将函数作为.send或.emit的最后一个参数.emit 。 更重要的是,当您使用.emit ,确认由您完成,这意味着您还可以传递数据:
我在这里做错了吗?
Doing some socket.io work. Seems like my emit callback is not being fired. Am I doing it right?
this.logger.debug("joining room " + roomId); callback = function() { return this.logger.debug('finished emitting join for socket'); }; // this.config.ioTypes.join resolves to "j" this.socket.emit(this.config.ioTypes.join, { user: this.config.user, room: roomId }, callback);My logging looks correct (note that the code above doesn't include everything that would generate these logs):
DEBUG: joining room 1245 <-- first line from code above DEBUG: join event for socket etQIXGAm59DYkCPk1_gj DEBUG: data: { user: 1, room: '1245' } DEBUG: received join event for 1, data: { user: 1, room: '1245' }And that's the end. It looks like socket.io is not calling my callback. But the docs are pretty clear:
Sometimes, you might want to get a callback when the client confirmed the message reception.
To do this, simply pass a function as the last parameter of .send or .emit. What's more, when you use .emit, the acknowledgement is done by you, which means you can also pass data along:
Am I doing something wrong here?
最满意答案
我自己搞清楚了。 要离开这里,因为这个文档不是很清楚。
我使用util.inspect(arguments)来查看服务器端的处理程序(coffeescript)的内容:
@logger.debug "onJoin args: " + util.inspect(arguments)结果导致: ... '2': [Function: ack]看起来像我需要的东西。 所以我把它添加到on处理程序的签名中。 然后我添加了callback() if callback? 到处理程序的末尾。 到目前为止看起来不错
Figured it out on my own. Gonna leave this here because the docs aren't very clear on this one.
I used util.inspect(arguments) to see what was coming in to my on handler on the server side (coffeescript):
@logger.debug "onJoin args: " + util.inspect(arguments)Which resulted in: ... '2': [Function: ack] which looked like what I needed. So I added that to the signature of the on handler. Then I added callback() if callback? to the end of the handler. Looks good so far.
更多推荐
发布评论