【问题标题】:node.js socket.io emit success callbacknode.js socket.io 发出成功回调
【发布时间】:2018-11-02 22:08:50
【问题描述】:
socketIO
   .to("gCDtw1Ko0_z4wBzmAAAA")
   .emit('notification', data);

我正在编写自己的即时通知代码。但是我需要知道通知是否传达给了用户,我找不到任何接口。

【问题讨论】:

    标签: android node.js socket.io


    【解决方案1】:

    您可以将函数作为.emit 的最后一个参数传递。下面是一个例子:

    服务器(app.js):

    var io = require('socket.io')(80);
    
    io.on('connection', function (socket) {
      socket.on('notification', function (word, fn) {
        fn(' says ' + word);
      });
    });
    

    客户端(index.html):

    <script>
      var socket = io(); 
      socket.on('connect', function () { 
        socket.emit('notification', 'hello', function (data) { 
          console.log(data); // if data is 'says hello', then successful
        });
      });
    </script>
    

    【讨论】:

    • 不幸的是我做不到。服务器部分的 node.js 客户端将是 android
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    • 2012-11-13
    • 2014-05-04
    • 2017-09-10
    • 2016-10-19
    • 2014-01-18
    相关资源
    最近更新 更多