简体   繁体   中英

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)".

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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