nodejs socket.io程序仅适用于谷歌浏览器

时间:2013-05-01 11:02:02

标签: node.js cross-browser socket.io

解决

我刚在客户端页面中将innerText更改为innerHTML


我有一个代码要测试,它在我的谷歌浏览器上工作正常,但它不能在我的Firefox上运行!什么可能导致问题?!

服务器代码:

var http = require('http'),
    io;

var html = require('fs').readFileSync("./index.html").toString('utf-8');

var server = http.createServer(function (request, response) {
    'use strict';
    response.statusCode = 200;
    response.setHeader('Content-Type', 'text/html');
    response.end(html);
});

server.listen(8124);

io = require('socket.io').listen(server);

function push(socket) {
    'use strict';

    var i = 0;
    return function () {
        socket.send(String(i++ % 3600));
    };
}

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

    var interval = setInterval(push(socket), 1000);
    socket.on('disconnect', function () {
        clearInterval(interval);
    });
});

客户代码:

<!DOCTYPE html>
<title>Socket.IO Example</title>
<div id="number"></div>
<script src="/socket.io/socket.io.js"></script>
<script>
    var io = io || undefined,
        socket = io.connect(),
        numberElement = document.getElementById('number');

    socket.on('message', function (data) {
        numberElement.innerText = data;
    });
</script>

Firefox调试器

Error: The connection to ws://127.0.0.1:8124/socket.io/1/websocket/O_dTsMb1uMKPtpPdqSVG was interrupted while the page was loading.
Source File: http://127.0.0.1:8124/socket.io/socket.io.js
Line: 2371

和该代码的第2371行是:

this.websocket = new Socket(this.prepareUrl() + query);

我该如何解决问题?!

0 个答案:

没有答案