我有公共服务器接受http请求并通过套接字将它们传输到远程(NAT后面)套接字客户端。
服务器代码:
var express = require('express');
var app = express();
app.post("/", function(req, res){
console.log('Emit to client');
socketServer.sockets.sockets[client_id].emit('command', req.body);
});
var appSrv = app.listen(8080, function() {});
...
...
var socketServer = require('socket.io').listen(appSrv);
...
...
client.on('response', function (data) {
console.log(`Response From client...`);
});
客户代码:
var socket = require('socket.io-client')('http://exampleIP:port', {
reconnection: true,
reconnectionDelay: 1000,
reconnectionDelayMax : 5000,
reconnectionAttempts: 99999
});
socket.on('command', function(data){
console.log('Command from server');
...
...
console.log('Client respond with result');
socket.emit('response', ...)
}
我观察到的是:
如果我一个接一个地发布到服务器2命令......它们是同步链接的。第二个不会在第一个返回之前发送给客户端。
例如:
13:10:30 POST / command1 that take 10 sec.
13:10:31 Post / command2 that take 50 ms.
Result from server logs is:
13:10:30 Emit to client (command1)
13:10:31 Emit to client (command2)
13:10:41 Response From client... (command1)
13:10:41 Response From client... (command2)
Result from client logs is:
13:10:30 Command from server (command1)
13:10:41 Client respond with result (command1)
**13:10:41 Command from server (command2)**
13:10:41 Client respond with (command2)
服务器到客户端的第二次发出在首次执行之前甚至没有到客户端并从客户端发送到服务器。只有这时套接字服务器发送秒发出...我不会理解为什么会这样......他们就像服务器端的承诺链一样。
我搜索的结果是:
13:10:30 POST / command1 that take 10 sec.
13:10:31 Post / command2 that take 50 ms.
Result from server logs is:
13:10:30 Emit to client (command1)
13:10:31 Emit to client (command2)
13:10:31 Response From client... (command2)
13:10:41 Response From client... (command2)
Result from client logs is:
13:10:30 Command from server (command1)
**13:10:31 Command from server (command2)**
13:10:31 Client respond with result (command2)
13:10:41 Client respond with (command1)
我使用pm2 withouth nginx或apache来管理服务器进程
其他一切正常: 事件连接,断开连接,重新连接。
服务器是:DigitalOcean实例 客户是:Pasrberry Pi
我如何解决这个问题,或者改变一些进一步调试的线索?