socket.io: client-side emit callback never fires

后端 未结 3 1840
孤城傲影
孤城傲影 2020-12-28 21:07

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 m

3条回答
  •  悲&欢浪女
    2020-12-28 21:29

    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)

提交回复
热议问题