本文主要是介绍Send feedback in socket.on() in socket.io,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
http://www.csser.com/board/4f5ed68d417a7f6a6e000059
发送、接收需要确认的数据
在服务端与终端发送消息的过程中,如需要对方接收到消息后立刻得到确认,则只需在 .send
或 .emit
最后一个参数传入回调函数就可以了。
服务端代码
var io = require('socket.io').listen(80);io.sockets.on('connection', function (socket) {socket.on('ferret', function (name, fn) {fn('woot');}); });客户端代码
<script>var socket = io.connect(); // TIP: .connect with no args does auto-discoverysocket.on('connect', function () { // TIP: you can avoid listening on `connect` and listen on events directly too!socket.emit('ferret', 'tobi', function (data) {console.log(data); // data will be 'woot'});}); </script>http://stackoverflow.com/questions/6756241/socket-io-client-side-emit-callback-never-fires
var io = require('socket.io').listen(80);io.sockets.on('connection', function (socket) {socket.on("getSomeData", function(name,fn) {fn({data: "some random data"});});
});
Client
<script src="http://localhost/socket.io/socket.io.js"></script>
<script>var socket = io.connect('http://localhost');socket.emit("getSomeData", function(data) {console.log(data);});
</script>
这篇关于Send feedback in socket.on() in socket.io的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!