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只是为了概念验证,到目前为止,everthing工作得很好,除了我无法让我的emit回调在客户端工作。我必须在这里错过一些愚蠢的东西,但文档目前并不是杀手锏。服务器接收“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>
3 个解决方案
#1
20
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>
#2
5
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);
});
#3
0
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也提到了这个;送和获取-DATA-(确认)
#1
20
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>
#2
5
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);
});
#3
0
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也提到了这个;送和获取-DATA-(确认)