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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6756241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:03:46  来源:igfitidea点击:

socket.io: client-side emit callback never fires

javascriptnode.jssocket.io

提问by jdc0589

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. 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.

只是为了概念证明而使用 socket.io,到目前为止一切都很好,除了我无法让我的发射回调在客户端工作。我必须在这里遗漏一些愚蠢的东西,但目前文档并不是杀手。服务器接收“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 源代码中可以看出,它检查发出的最后一个参数是否是一个函数,并始终将其用作回调,但是调试比这更深的对我来说是有问题的。

I feel like I have to be missing some core concept here..Is this not what this.send(..)is supposed to do? I could find only 1 useage in the example apps, and none where the client side code for that event emission was available.

我觉得我必须在这里遗漏一些核心概念..这不是this.send(..)应该做的吗?我在示例应用程序中只能找到 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>

回答by Lime

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>

回答by Thomas Luechtefeld

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

回答by Xin

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; sending-and-getting-data-(acknowledgements)

其实socket io也提到了这个;发送和获取数据-(确认)