node.js - 如何在 Angular 8 中使用 socket.io-client 来获得确认
问题描述
服务器端是nodejs socket.io,我已经连接到Angular socket.io-client。我在发出事件时尝试确认。如果有人知道任何文档或教程我该怎么做,请帮助
解决方案
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'
});
});
推荐阅读
- php - 如何使用 PHP 创建搜索按钮
- typescript - 在 router.ts 中访问 Vuex 存储模块的状态
- html - 如何在 HTML/Bootstrap 中将标题文本添加到 img alt
- python - 如何使用 GAE 收听 Google 表格,然后运行一组 python 代码?
- node.js - 如何从 axios 获取 utf-8 中的值在 node.js 中接收 iso-8859-1
- matlab - 常量属性中方法的函数句柄
- docker - docker 在 dockerfile 和 docker-compose.yml 中使用通用配置
- azureservicebus - 将 MassTransit 与 Azure 服务总线结合使用 - 设置配对命名空间
- selenium - 当我们在 Selenium 中使用 TestNG 时,我们不应该使用异常处理吗?
- redux - 在工作的 redux 配置上添加 redux-persist