socket.io连接上的多个握手

时间:2013-11-12 05:54:33

标签: node.js socket.io titanium titanium-mobile

我正在为移动应用程序编写服务器,并选择使用socket.io从服务器到应用程序的推送通知,但我在设置socket.io时遇到问题

目前在io.connect(服务器:端口)

上启动了无休止的握手
debug - client authorized
info  - handshake authorized HXtC1UPzcsEzO8X7x2B1
debug - client authorized
info  - handshake authorized aE7hMGDpp5InO1UWx2B2
debug - client authorized
info  - handshake authorized GBx_3n-8Lvbuo4QJx2B3
debug - client authorized
info  - handshake authorized tOEl0F5wNlh4xkn1x2B4
debug - client authorized
info  - handshake authorized K55Oit22yN9JDWxhx2B5
debug - client authorized
...

我正在使用socket.io-titanium作为应用程序的客户端,我无法分辨哪一端导致了这个连接问题。

客户端:

var io = require('socket.io-titanium');
var socket = io.connect('http://myserver.com:3000');
socket.on('connect', function (data){
  Ti.API.log("socket.io connected");
});
socket.on('update', function (data){
  Ti.API.log(data);
});

服务器:

index = fs.readFileSync(__dirname + '/index.html');
var updates = http.createServer(function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end(index);
});

var io = require('socket.io').listen(updates);

var clients = {};

io.sockets.on('connection', function(socket) {
    console.log("Server Connection Event");
    socket.on('i am client', console.log);
});

io.set("polling duration", 10);
io.set("close timeout", 30);
updates.listen(3000, function(){
 console.log("Updates listening on port 3000");
 console.log("");
});

我在一个非常长的流(100 +)

中间收到了一次“服务器连接事件”

我认为握手的想法是只需要授权和连接;每个用户建立一次,直到连接关闭。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

您是在本地笔记本电脑上还是通过互联网进行测试?

如果是在互联网上,则socket.io客户端可能会回退到较旧的基于轮询的协议。例如,如果您的ISP碰巧运行HTTP / 1.0代理,就会发生这种情况。这样的代理将拒绝在HTTP / 1.1中定义的连接升级,因此Web套接字将失败,并且您的客户端将回退到较旧的方法,如长轮询等。

如果您在本地笔记本电脑上运行时遇到此问题,则可能是其他问题。你可以打开Chrome然后打开开发者工具>网络然后选择Web套接字筛选器。现在导航到您的站点,看看您的Web Socket是否已正确建立。