Node.js和socket.io - 客户端没有握手客户端应该重新连接

时间:2012-02-28 16:55:44

标签: node.js socket.io

我有socket.io的问题。

此错误:警告 - 客户端未握手的客户端应重新连接

在控制台中我有一个错误列表:

debug - setting poll timeout
   debug - clearing poll timeout
   debug - xhr-polling writing 7:::1+0
   debug - set close timeout for client 17348980511779302969
   warn  - client not handshaken client should reconnect
   info  - transport end

我有这个简单的应用程序:

require.paths.unshift(__dirname + '/../../lib/');

var express = require('express');
var app = express.createServer();
var io = require('socket.io').listen(app);
var stylus = require('stylus');
var mysql = require('mysql');

app.use(express.bodyParser());
app.use(express.cookieParser());

app.use(stylus.middleware({ src: __dirname + '/public', compile: compile }))
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname);

app.set("view options", { layout: false });

app.register('.html', {
    compile: function (str, options) {
        return function (locals) {
            return str;
        };
    }
});

function compile(str, path) {
    return stylus(str)
      .set('filename', path)
      .use(nib());
};

app.get('/', function (req, res) {
res.render('index.html', { layout: false });
});


io.configure(function () {
    io.set('transports', ['websocket','flashsocket','xhr-polling']);
    io.set('polling duration', '10');
});

io.sockets.on('connection', function (socket) {

    console.log("Connect with ID: " + socket.id);   

    socket.emit("sendid", {id: socket.id});

    socket.on('disconnect', function () {
        console.log("Disconnect with ID: " + socket.id);
    });

});

app.listen(3000);

客户代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>Socket.IO - test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script src="/socket.io/socket.io.js" type="text/javascript"></script>
    <script type="text/javascript">

        var WEB_SOCKET_SWF_LOCATION = './WebSocketMain.swf';

    var socket = io.connect(null, {
            'connect timeout': 500,
            'reconnect': true,
            'reconnection delay': 500,
            'reopen delay': 500,
            'max reconnection attempts': 10
        });

    socket.on('sendid', function (data) {
            alert("Your socket ID is: " + data.id);
        });

    </script>
</head>
<body>
Test socket.io.
</body>
</html>

1 个答案:

答案 0 :(得分:4)

我已下载最新版本的socket.iosocket.io-client

我在客户端设置了连接socket.io的URL和端口。

 var socket = io.connect('http://domain.com:port', {
            'connect timeout': 500,
            'reconnect': true,
            'reconnection delay': 500,
            'reopen delay': 500,
            'max reconnection attempts': 10
        });