我有一个使用socket.io的node.js服务器。我正在返回一个值为id
的对象。该值为二进制。目前我正在让socket.io处理这个因为它声明它可以处理二进制值。请求的服务器日志如下所示:
socket.io:socket sending ack [null,[{"id":{"type":"Buffer","data":[187,147,6,248,235,251,17,230,158,219,131,162,166,89,203,237]},"name":"Test project"}]] +7ms
socket.io:client writing packet {"id":0,"type":6,"data":[null,[{"id":{"type":"Buffer","data":[187,147,6,248,235,251,17,230,158,219,131,162,166,89,203,237]},"name":"Test project"}]],"nsp":"/"} +0ms
socket.io-parser encoding packet {"id":0,"type":6,"data":[null,[{"id":{"type":"Buffer","data":[187,147,6,248,235,251,17,230,158,219,131,162,166,89,203,237]},"name":"Test project"}]],"nsp":"/"} +8ms
socket.io-parser encoded {"id":0,"type":6,"data":[null,[{"id":{"_placeholder":true,"num":0},"name":"Test project"}]],"nsp":"/","attachments":1} as 61-0[null,[{"id":{"_placeholder":true,"num":0},"name":"Test project"}]] +0ms
engine:socket sending packet "message" (61-0[null,[{"id":{"_placeholder":true,"num":0},"name":"Test project"}]]) +8ms
engine:socket flushing buffer to transport +0ms
engine:ws writing "461-0[null,[{"id":{"_placeholder":true,"num":0},"name":"Test project"}]]" +0ms
engine:socket sending packet "message" (�������ۃ��Y��) +0ms
engine:socket flushing buffer to transport +1ms
engine:ws writing "�������ۃ��Y��" +0ms
如果我正确理解了socket.io doco,它会注入占位符并在另一个请求中发送二进制文件。所以这对我来说没问题。
我有一个HTML / Javascript测试客户端,我用它来测试查询,它的原始请求如下所示:
socket.emit('listProjects', function (data) {
writeToScreen('<span style="color: blue;">Ack: ' + data + '</span>');
});
在控制台中生成日志显示:
[Log] engine.io-client:socket socket receive: type "message", data "61-0[null,[{"id":{"_placeholder":true,"num":0},"name":"Test project"}]]" +6ms (socket.io.js, line 1)
[Log] socket.io-parser decoded 61-0[null,[{"id":{"_placeholder":true,"num":0},"name":"Test project"}]] as {"type":6,"attachments":1,"nsp":"/","id":0,"data":[null,[{"id":{"_placeholder":true,"num":0},"name":"Test project"}]]} +6ms (socket.io.js, line 1)
[Log] engine.io-client:socket socket receive: type "message", data "ArrayBuffer" +1ms (socket.io.js, line 1)
[Log] socket.io-client:socket calling ack 0 with [null,[{"id":{},"name":"Test project"}]] +0ms (socket.io.js, line 1)
[Log] Data: null (Tester.html, line 55)
基本上emit
的ack参数返回null。我原本以为它会被设置为控制台中显示的数据。即使它具有空id
值。
任何人都知道我可能做错了什么?
答案 0 :(得分:0)
好的,这是我的坏事。在服务器上,我使用Promise并使用标准function(err, data)
调用进行响应。所以我的排放应该是:
socket.emit('listProjects', function (err, data) {
writeToScreen('<span style="color: blue;">Ack: ' + data + '</span>');
});