简体   繁体   English

socket.io:客户端发出回调永远不会触发

[英]socket.io: client-side emit callback never fires

Messing around with socket.io just for proof of concept, everthing is working great so far except I can't get my emit callback to work on the client side.只是为了概念证明而与 socket.io 混在一起,到目前为止一切都很好,除了我无法让我的发出回调在客户端工作。 I've got to be missing something stupid here, but documentation is not killer at the moment.我必须在这里遗漏一些愚蠢的东西,但目前文档并不是杀手。 The server picks up the "getSomeData" event just fine, no errors anywhere.服务器很好地接收“getSomeData”事件,任何地方都没有错误。

From what I could tell in the client socket.io source, it checks if the last argument to emit is a function and always uses it as a callback, but debugging any deeper than that was problematic for me.从我在客户端 socket.io 源代码中可以看出,它检查要发出的最后一个参数是否是 function 并始终将其用作回调,但比这更深的调试对我来说是个问题。

I feel like I have to be missing some core concept here..Is this not what this.send(..) is supposed to do?我觉得我必须在这里遗漏一些核心概念..这不是this.send(..)应该做的吗? I could find only 1 useage in the example apps, and none where the client side code for that event emission was available.我只能在示例应用程序中找到 1 个用法,而在该事件发射的客户端代码可用的地方却没有。

Update : just to be clear, I am in fact intentionally emitting the event client side.更新:为了清楚起见,我实际上是故意发出事件客户端。 The purpose of this was to see if socket.io could be used to allow clients to pull data on request in addition to receiving pushes.这样做的目的是查看 socket.io 是否可用于允许客户端在接收推送之外根据请求提取数据。

server:服务器:

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

io.sockets.on('connection', function (socket) {
    socket.on("getSomeData", function() {
        this.send({data: "some random data"});
    });
});

client: (console.log never happens)客户:(console.log 永远不会发生)

<script type="text/javascript" src="http://localhost/socket.io/socket.io.js"></script>
<script type="text/javascript">
  var socket = io.connect('http://localhost');
  socket.emit("getSomeData", function(data) {
      console.log(data);
  });
</script>

It looks like you have some logic switched around, try...看起来你有一些逻辑转换,试试......

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

io.sockets.on('connection', function (socket) {
    socket.emit("getSomeData",{data: "some random data"});
});

and client...和客户...

<script src="http://localhost/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost');
  socket.on("getSomeData", function(data) {
      console.log(data);
  });
</script>

EDIT:编辑:

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>

In Liam William's answer, I found it was necessary to send the callback (or acknowledgement function) as the third argument on the client emit:在 Liam William 的回答中,我发现有必要将回调(或确认函数)作为客户端发出的第三个参数发送:

Client:客户:

<script src="http://localhost/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.emit("getSomeData", null, function(data) {
    console.log(data);
});

You can have the callback, but you have to do it a bit differently:你可以有回调,但你必须做的有点不同:


Server: (app.js)服务器:(app.js)

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

io.on('connection', function (socket) {
// please note that server will take 2 data entries as function parameter below
  socket.on('ferret', function (name, fn) {
    fn('woot');
  });
});

Client (index.html)客户端 (index.html)

var socket = io(); // TIP: io() with no args does auto-discovery
socket.on('connect', function () {
socket.emit('ferret', 'tobi', function (data) {
    console.log(data); // data will be 'woot'
});
});

Actaully, socket io also mentions this one;实际上,socket io 也提到了这个; sending-and-getting-data-(acknowledgements)发送和获取数据(确认)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM