简体   繁体   中英

socket.io: client-side emit callback never fires

Messing around with socket.io just for proof of concept, everthing is working great so far except I can't get my emit callback to work on the client side. I've got to be missing something stupid here, but documentation is not killer at the moment. The server picks up the "getSomeData" event just fine, no errors anywhere.

From what I could tell in the client socket.io source, it checks if the last argument to emit is a function and always uses it as a callback, but debugging any deeper than that was problematic for me.

I feel like I have to be missing some core concept here..Is this not what this.send(..) is supposed to do? I could find only 1 useage in the example apps, and none where the client side code for that event emission was available.

Update : just to be clear, I am in fact intentionally emitting the event client side. The purpose of this was to see if socket.io could be used to allow clients to pull data on request in addition to receiving pushes.

server:

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
    socket.on("getSomeData", function() {
        this.send({data: "some random data"});
    });
});

client: (console.log never happens)

<script type="text/javascript" src="http://localhost/socket.io/socket.io.js"></script>
<script type="text/javascript">
  var socket = io.connect('http://localhost');
  socket.emit("getSomeData", function(data) {
      console.log(data);
  });
</script>

It looks like you have some logic switched around, try...

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
    socket.emit("getSomeData",{data: "some random data"});
});

and client...

<script src="http://localhost/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost');
  socket.on("getSomeData", function(data) {
      console.log(data);
  });
</script>

EDIT:

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
    socket.on("getSomeData", function(name,fn) {
        fn({data: "some random data"});
    });
});

Client

<script src="http://localhost/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost');
  socket.emit("getSomeData", function(data) {
      console.log(data);
  });
</script>

In Liam William's answer, I found it was necessary to send the callback (or acknowledgement function) as the third argument on the client emit:

Client:

<script src="http://localhost/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.emit("getSomeData", null, function(data) {
    console.log(data);
});

You can have the callback, but you have to do it a bit differently:


Server: (app.js)

var io = require('socket.io')(80);

io.on('connection', function (socket) {
// please note that server will take 2 data entries as function parameter below
  socket.on('ferret', function (name, fn) {
    fn('woot');
  });
});

Client (index.html)

var socket = io(); // TIP: io() with no args does auto-discovery
socket.on('connect', function () {
socket.emit('ferret', 'tobi', function (data) {
    console.log(data); // data will be 'woot'
});
});

Actaully, socket io also mentions this one; sending-and-getting-data-(acknowledgements)

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