简体   繁体   English

用socket.io发送确认,说我的函数是未定义的

[英]Sending acknowledgements with socket.io, says my function is undefined

I am trying to use acknowledgements with socket.io as defined in http://socket.io/#how-to-use "Sending and getting data (acknowledgements)". 我正在尝试使用在http://socket.io/#how-to-use “发送和获取数据(确认)”中定义的socket.io的确认。

My client code looks like 我的客户端代码看起来像

sio.emit('ferret', 'tobi', function (data) {
  console.log(data);
});

And my server code is: 我的服务器代码是:

socket.on('ferret', function (name, fn) {
  fn('woot');
});

But I'm getting an error that's crashing my node server: 但是我收到的错误是我的节点服务器崩溃了:

fn('woot');
23:13:48 web.1  |         ^
23:13:48 web.1  | TypeError: undefined is not a function

Which I find strange because if I add: 我发现这很奇怪,因为如果我添加:

console.log(name);
console.log(fn);

, name will log Tobi, but for what ever reason fn is logging undefined. ,name会记录Tobi,但是因为fn记录未定义的原因。

Any information would be awesome!! 任何信息都会很棒!!

In case anyone was having this problem, make sure that on the client, the emit has an acknowledgement function as the last parameter. 如果有人遇到此问题,请确保在客户端上,emit具有确认功能作为最后一个参数。 If you write the server side code and restart the server again, it will give an error unless you have the client side page refreshed with the new code. 如果您编写服务器端代码并​​再次重新启动服务器,除非您使用新代码刷新客户端页面,否则它将给出错误。 Although I had written the code on both client and server, I kept getting the error on my server because I had not refreshed my client side page. 虽然我已经在客户端和服务器上编写了代码,但我仍然在服务器上收到错误,因为我没有刷新客户端页面。

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

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