I have a very simple configuration in a node server with socket.io installed (a little bit more complex but essentially like this one):
我在带有套接字的节点服务器中有一个非常简单的配置。io安装(稍微复杂一点,但本质上就像这个):
var main = require('express')();
server = require('http').createServer(main);
io = require('socket.io')(server);
io.use(function(socket, next) {
console.log("middleware!");
next();
});
io.on('connection', function (socket) {
console.log('connected...');
socket.on('pong', function (data) {
console.log(data.message);
});
setTimeout(function() {
console.log("Saying hello");
socket.emit('ping', { message: 'Hello from server ' + Date.now() });
}, 1000);
});
server.listen(2080, function onCreateServerMain() {
console.log('Server main is listening on port 2080';
console.log('************************************************************');
});
In the client:
在客户端:
var socketIoScript,
loadSocketTimeout,
trialsToLoadSocketIo = 0,
APP_CFG = {baseUrl : "http://192.168.1.13:2080"};
function loadSocketIo(socketIoIp) {
socketIoScript = document.createElement('script');
socketIoScript.setAttribute('src', socketIoIp);
socketIoScript.setAttribute('onload', 'onSocketLoaded();');
document.head.appendChild(socketIoScript);
}
window.onSocketLoaded = function onSocketLoaded() {
if (typeof(io.connect) === 'function') {
var mSocket,
mIoSocket;
$timeout.cancel(loadSocketTimeout);
mIoSocket = new io.Manager(APP_CFG.baseUrl);
mIoSocket.connect(function(socket) {
console.log('Connected!!');
});
mIoSocket.on('error', function onSocketError(e) {
console.log('WebSocket Error ' + error);
});
mIoSocket.on('ping', function onPingReceived(e) {
console.log('Server emitted ping: ' + e.data);
mSocket.emit('pong', 'hi server!');
});
}
}
~(function onLoadSocketTimeout() {
var nextTimeout;
if (trialsToLoadSocketIo < 10) {
nextTimeout = 5000;
} else if (trialsToLoadSocketIo > 60) {
nextTimeout = 60000;
} else {
nextTimeout = 1000 * trialsToLoadSocketIo;
}
if (socketIoScript) {
document.head.removeChild(socketIoScript);
}
loadSocketIo(APP_CFG.baseUrl + '/socket.io/socket.io.js#' + trialsToLoadSocketIo);
loadSocketTimeout = $timeout(onLoadSocketTimeout, nextTimeout);
trialsToLoadSocketIo += 1;
})();
(I'm doing like this because it's mobile app so it may have not connection). I'm testing it with Brackets and Chrome. Server and client are in the same machine. In the app the script is loaded fine and it connects to the server as I can it see in node log (edit: and this is all what I get in the node console):
(我这样做是因为它是移动应用,所以可能没有连接)。我正在用括号和Chrome测试它。服务器和客户端在同一台机器上。在app中,脚本加载良好,可以在节点日志中看到它连接到服务器(编辑:这是我在节点控制台得到的所有内容):
Server main is listening on port 2080
************************************************************
middleware!
connected...
Saying hello
Edit: in Chrome console I don't get any message, and any breakpoint stops at on
listeners. If I stop node, the console for the Chrome immediately starts logging that it has been disconnected:
编辑:在Chrome控制台,我没有收到任何消息,任何断点都停在监听器上。如果我停止node, Chrome的控制台立即开始记录它已经断开连接:
GET http://192.168.1.13:2080/socket.io/?EIO=3&transport=polling&t=1413066902601-6 net::ERR_CONNECTION_REFUSED
GET http://192.168.1.13:2080/socket.io/?EIO=3&transport=polling&t=1413066906606-7 net::ERR_CONNECTION_REFUSED
But I can't see any incoming message. In the app I don't receive any incoming message. Is there any reason why I could not communicate in this environment even if socket is successfully connected?
但是我看不到任何传入的消息。在应用程序中,我没有收到任何传入消息。在这个环境中,如果socket连接成功了,我还有什么原因不能交流吗?
EDIT
编辑
No app is receiving events sent from the other side. Logs from node show this, logs from Chrome are empty.
没有应用程序接收来自另一端的事件。来自node的日志显示,来自Chrome的日志是空的。
EDIT
编辑
In Chrome app I don't receive console.log("Connected!");
. But neither I receive ERR_CONNECTION_REFUSED
errors: I don't receive anything.
在Chrome应用程序中,我不接收console.log(“连接!”);但是我都没有收到err_connection_denied错误:我没有收到任何东西。
EDIT
编辑
I managed to get console.log("Connected!");
in the app by changing Manager options:
我设法找到了console.log("Connected!")在app中更换经理选项:
mIoSocket = new io.Manager(APP_CFG.baseUrl, { autoConnect: false });
As it was auto connecting and the events were attached after connection was made, "Connected" was never reached. But I'm still not receiving any event in any app.
由于它是自动连接的,并且在连接完成后事件被附加在一起,所以从来没有到达“Connected”。但我仍然没有收到任何应用程序中的任何事件。
1 个解决方案
#1
0
Ok, so a few things :
好,有几件事:
First, var mSocket
doesn't seem to be initialized, so it may be difficult for it to emit() anything (am I missing something?)
首先,var mSocket似乎没有初始化,所以它可能很难发出()任何东西(我是不是漏掉了什么?)
Second, when you do :
其次,当你这么做的时候:
socket.on('pong', function (data) {
console.log(data.message);
});
the server expects to receive an object containing a message
property, eg : data = {message:'hi server'}
In your case, you send a string, so data
is 'Hi server !'
and your log will say 'undefined'. You should change this bit to :
服务器期望接收一个包含消息属性的对象,例如:data = {message:'hi server'}在您的例子中,您发送了一个字符串,所以数据是'hi server !',您的日志将显示'undefined'。您应该将这部分更改为:
socket.on('pong', function (data) {
console.log(data);
});
and you have a similar problem the other way around, you send an object : { message: 'Hello from server ' + Date.now() }
, and are trying to log a data
property which does not exist. Change this bit to :
另一种类似的问题是,发送一个对象:{消息:'Hello from server ' + Date.now()},并尝试记录不存在的数据属性。将这个位改为:
console.log('Server emitted ping: ' + e.message);
And third , you have to listen for events on the socket, not the 'manager'
第三,您必须监听套接字上的事件,而不是“manager”
Client :
客户:
mIoSocket.connect(function(socket) {
console.log('Connected!!');
socket.emit('pong');
socket.on('error', function onSocketError(e) {
console.log('WebSocket Error ' + error);
});
socket.on('ping', function onPingReceived(e) {
console.log('Server emitted ping: ' + e.data);
socket.emit('pong', 'hi server!');
});
});
Server :
服务器:
io.on('connection', function (socket) {
console.log('connected...');
socket.on('pong', function (data) {
console.log(data);
});
setTimeout(function() {
console.log("Saying hello");
socket.emit('ping', { message: 'Hello from server ' + Date.now() });
}, 1000);
});
#1
0
Ok, so a few things :
好,有几件事:
First, var mSocket
doesn't seem to be initialized, so it may be difficult for it to emit() anything (am I missing something?)
首先,var mSocket似乎没有初始化,所以它可能很难发出()任何东西(我是不是漏掉了什么?)
Second, when you do :
其次,当你这么做的时候:
socket.on('pong', function (data) {
console.log(data.message);
});
the server expects to receive an object containing a message
property, eg : data = {message:'hi server'}
In your case, you send a string, so data
is 'Hi server !'
and your log will say 'undefined'. You should change this bit to :
服务器期望接收一个包含消息属性的对象,例如:data = {message:'hi server'}在您的例子中,您发送了一个字符串,所以数据是'hi server !',您的日志将显示'undefined'。您应该将这部分更改为:
socket.on('pong', function (data) {
console.log(data);
});
and you have a similar problem the other way around, you send an object : { message: 'Hello from server ' + Date.now() }
, and are trying to log a data
property which does not exist. Change this bit to :
另一种类似的问题是,发送一个对象:{消息:'Hello from server ' + Date.now()},并尝试记录不存在的数据属性。将这个位改为:
console.log('Server emitted ping: ' + e.message);
And third , you have to listen for events on the socket, not the 'manager'
第三,您必须监听套接字上的事件,而不是“manager”
Client :
客户:
mIoSocket.connect(function(socket) {
console.log('Connected!!');
socket.emit('pong');
socket.on('error', function onSocketError(e) {
console.log('WebSocket Error ' + error);
});
socket.on('ping', function onPingReceived(e) {
console.log('Server emitted ping: ' + e.data);
socket.emit('pong', 'hi server!');
});
});
Server :
服务器:
io.on('connection', function (socket) {
console.log('connected...');
socket.on('pong', function (data) {
console.log(data);
});
setTimeout(function() {
console.log("Saying hello");
socket.emit('ping', { message: 'Hello from server ' + Date.now() });
}, 1000);
});