【问题标题】:How to use socket.io-client in angular 8 to get acknowledgement如何在 Angular 8 中使用 socket.io-client 来获得确认
【发布时间】:2019-11-18 14:32:19
【问题描述】:

服务器端是nodejs socket.io,我已经连接到Angular socket.io-client。我在发出事件时尝试确认。如果有人知道任何文档或教程我该怎么做,请帮助

【问题讨论】:

  • 请分享您的一些代码并彻底解释问题。

标签: node.js angular sockets


【解决方案1】:

为此,只需将函数作为 .send 或 .emit 的最后一个参数传递。更重要的是,当您使用 .emit 时,确认由您完成,这意味着您还可以传递数据: Source Link 你的 server.js

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

io.on('connection', function (socket) {
 socket.on('ferret', function (name, word, fn) {
    fn(name + ' says ' + word);
 });
});

你的 Client.js

 socket.emit('ferret', 'tobi', 'woot', function (data) { // args are sent in order to acknowledgement function
      console.log(data); // data will be 'tobi says woot'
   });
 });

【讨论】:

    猜你喜欢
    • 2018-04-20
    • 2016-12-23
    • 2018-08-21
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 2016-09-02
    • 2015-01-24
    • 2020-08-09
    相关资源
    最近更新 更多