问题描述
var io = require('socket.io-client'),
socket = io.connect('targeturl', {
port: 8080
});
socket.on('connect', function () {
console.log("socket connected"); //this is printed.
});
socket.emit('list', {"user":'username',"token": 'mytoken'});
socket.on('message', function(data){
console.log(data);
});
socket.on('error', function(data){
console.log(data); //nothing is printed
});
我在命令行上从node.js运行时看到socket connected消息但是我没有看到列表调用的响应。
I see the message 'socket connected' when running this from node.js on the command line but I don't see a response from the 'list' call.
请注意,这是关于使用作为命令行应用程序。
Note that this is a specific question about using socket-io-client from Node.js as a command line application.
我有使用 curl
验证我可以到达REMOTE的套接字服务器,即我没有服务器代码,但我知道它使用的是socket.io。
I have verified using curl
that I can reach the socket server which is REMOTE i.e I do not have the server code but I know that it uses socket.io.
推荐答案
我不是这方面的专家,但看起来你正试图在建立连接之前发出消息。 (这个东西毕竟是异步的。)
I'm no expert on the matter, but it appears you're attempting to emit the message before the connection is established. (This stuff is asynchronous after all.)
尝试在'connect'的处理程序中移动 emit
调用'事件,确保事后发生。
Try moving the emit
call inside of the handler for the 'connect' event, to ensure that it happens afterward.
这篇关于为什么我没有看到socket.io-client对node.js的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!