2

我正在尝试开发一个简单的聊天应用程序。这是我的chat.js文件。

var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs');
app.listen(8124);
function handler (req, res) {
fs.readFile(__dirname + '/chat.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading chat.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
    socket.on('addme',function(username) {
        socket.username = username;
        socket.emit('chat', 'SERVER', 'You have connected');
        socket.broadcast.emit('chat', 'SERVER', username + ' is on deck');
    });
    socket.on('sendchat', function(data) {
        io.sockets.emit('chat', socket.username, data);
    });
    socket.on('disconnect', function() {
        io.sockets.emit('chat', 'SERVER', socket.username + ' has left the building');
    });
});

还有我的chat.html文件。

<head>
<meta charset="utf-8">
<title>bi-directional communication</title>
<script src="/socket.io/socket.io.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(document).ready(function(e) {
    var socket = io.connect('http://localhost:8124/');
    $('#submit').click(function(e) {
        e.preventDefault();
        v = $('#uname').val();
        $('#username').html('');
        socket.emit('addme', v);
    });

    socket.on('chat',function(username, data) {
        var p = document.createElement('p');
        p.innerHTML = username + ': ' + data;
        document.getElementById('output').appendChild(p);
    });
    window.addEventListener('load',function() {
        document.getElementById('sendtext').addEventListener('click',
        function() {
            var text = document.getElementById('data').value;
            socket.emit('sendchat', text);
        }, false);
    }, false);

});
</script>
</head>
<body>
<div id="output"></div>
<div id="username">
  <input type="text" name="uname" id="uname">
  <input type="submit" name="submit" id="submit" value="Submit">
</div>
<div id="send">
  <input type="text" id="data" size="100" /><br />
<input type="button" id="sendtext" value="Send Text" />
</div>
</body>
</html>

我通过在 node.js 命令提示符中键入node chat.jshttp://localhost:8124/然后在我的浏览器地址栏中键入来测试代码。问题是,虽然这在 IE9 上完美运行,但在 Firefox 和 Chrome 上却没有任何反应。

当我在 chrome 或 firefox 上运行它时,我在 Node.js 命令提示符上收到以下内容。

info  - socket.io started
   debug - served static content /socket.io.js
   debug - client authorized
   info  - handshake authorized 0yJjYvt7o36BTX6T_hXA
   debug - setting request GET /socket.io/1/websocket/0yJjYvt7o36BTX6T_hXA
   debug - set heartbeat interval for client 0yJjYvt7o36BTX6T_hXA
   debug - client authorized for
   debug - websocket writing 1::
   debug - setting request GET /socket.io/1/xhr-polling/0yJjYvt7o36BTX6T_hXA?t=1
341573194770
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared heartbeat interval for client 0yJjYvt7o36BTX6T_hXA
   debug - setting request GET /socket.io/1/jsonp-polling/0yJjYvt7o36BTX6T_hXA?t
=1341573204771&i=0
   debug - setting poll timeout
   debug - discarding transport
   debug - clearing poll timeout
   debug - clearing poll timeout
   debug - jsonppolling writing io.j[0]("8::");
   debug - set close timeout for client 0yJjYvt7o36BTX6T_hXA
   debug - jsonppolling closed due to exceeded duration
   debug - setting request GET /socket.io/1/jsonp-polling/0yJjYvt7o36BTX6T_hXA?t
=1341573224817&i=0
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared close timeout for client 0yJjYvt7o36BTX6T_hXA
   debug - clearing poll timeout
   debug - jsonppolling writing io.j[0]("8::");
   debug - set close timeout for client 0yJjYvt7o36BTX6T_hXA
   debug - jsonppolling closed due to exceeded duration
   debug - setting request GET /socket.io/1/jsonp-polling/0yJjYvt7o36BTX6T_hXA?t
=1341573244856&i=0

这样下去。请帮忙。

4

3 回答 3

5

谢谢你 ebohlman 的时间,但我解决了这个问题。在我的chat.js 中,我添加了以下行。

io.configure('development', function(){
  io.set('transports', ['xhr-polling']);
});

现在我的chat.js 看起来像。

var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs');

app.listen(8124);

io.configure('development', function(){
  io.set('transports', ['xhr-polling']);
});

function handler (req, res) {
fs.readFile(__dirname + '/chat.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading chat.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
    socket.on('addme',function(username) {
        socket.username = username;
        socket.emit('chat', 'SERVER', 'You have connected');
        socket.broadcast.emit('chat', 'SERVER', username + ' is on deck');
    });
    socket.on('sendchat', function(data) {
        io.sockets.emit('chat', socket.username, data);
    });
    socket.on('disconnect', function() {
        io.sockets.emit('chat', 'SERVER', socket.username + ' has left the building');
    });
});

但我仍然不知道可能导致错误的原因。知道的请解释一下!

于 2012-07-06T11:47:46.853 回答
1

我在尝试发布到 AppFog 时遇到了同样的问题。这是由 AppFog 尚不支持 websockets 引起的,因此您的修复通过强制 socket.io 使用长轮询/AJAX 来工作。AppFog 正在根据他们的网站支持 websockets,但没有给出日期。

不幸的是,我发现的大多数云 PaaS 提供商还不支持 websocket。我现在正在测试的一个值得注意的例外是 Nodejitsu: http ://nodejitsu.com/

于 2013-02-25T15:08:12.167 回答
0

我认为这将有助于了解这个问题,以及它是如何解决的,我搜索了很多,最后我得到了

socket.io 中的“xhr-polling”配置有什么作用?

谢谢你

于 2015-01-23T04:35:44.657 回答