首页 > 解决方案 > 如何在 Angular 8 中使用 socket.io-client 来获得确认

问题描述

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

标签: node.jsangularsockets

解决方案


To do this, simply pass a function as the last parameter of .send or .emit. What’s more, when you use .emit, the acknowledgement is done by you, which means you can also pass data along: Source Link Your server.js

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

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

Your 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'
   });
 });

推荐阅读