2

node.js + socket.io を使用して簡単なチャット アプリケーションを作成しました。実行しようとすると、期待どおりに実行されません。クライアントから継続的にリクエストが送信されます。トランスポートの設定を構成['jsonp-polling', 'xhr-polling']しました:両方(クライアント/サーバー)側で。を設定['close timeout': 10]してみましたが、何も変わりませんでした。ここで何が起こっているのかわかりません。

ログファイル(最後)にあるように、接続が壊れていると思います。

以下は私のコードです:

サーバー側: app.js ~~~~~~~~~~~~~~~~~~~~

var sio = require('socket.io');
var io = sio.listen(app)

io.configure(function () {
    io.enable('browser client minification'); 
    io.enable('browser client etag');         
    io.enable('browser client gzip');         
    io.set('log level', 3);
});

io.on('connection', function (client) {
    client.on('message', function (msg) {
        socket.broadcast(msg);
    });
    client.on('disconnect', function () {
    });
});

クライアント側: index.html ~~~~~~~~~~~~~~~~~~~~~~~

<!DOCTYPE html>
<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
        <script src="http://cdn.socket.io/stable/socket.io.js"></script>
        <script>
            $(document).ready(function () {
                var socket = new io.Socket("localhost", {port: 8080});

                socket.on('connect', function () {
                    socket.send('A client connected.');
                });
                socket.on('message', function (message) {
                    $('div#messages').append($('<p>'), message);
                });
                socket.on('disconnect', function () {
                    console.log('disconnected');
                });
                socket.connect();

                $('input').keydown(function (event) {
                    if(event.keyCode === 13) {
                        socket.send($('input').val());
                        $('input').val('');
                    }
                });
            });
        </script>
    </head>
    <body>
        <input type="text" style="width: 300px;" />
        <div id="messages" style="border:solid 1px #000;">&nbsp;</div>
    </body>
</html>

ログファイルは次のとおりです。

debug: client authorized
info: handshake authorized U1Y79P8WLaMz_UWoiKTc
debug: setting request GET /socket.io/1/xhr-polling/U1Y79P8WLaMz_UWoiKTc?t=1345273505128
debug: setting poll timeout
debug: client authorized for 
debug: clearing poll timeout
debug: xhr-polling writing 1::
debug: set close timeout for client U1Y79P8WLaMz_UWoiKTc
debug: setting request GET /socket.io/1/xhr-polling/U1Y79P8WLaMz_UWoiKTc?t=1345273505194
debug: setting poll timeout
debug: discarding transport
debug: cleared close timeout for client U1Y79P8WLaMz_UWoiKTc
debug: setting request GET /socket.io/1/xhr-polling/ufw4UqC2pCvWqqB5iKOQ?t=1345273505438
debug: setting poll timeout
debug: clearing poll timeout
debug: xhr-polling writing 7:::1+0
debug: set close timeout for client ufw4UqC2pCvWqqB5iKOQ
warn: client not handshaken client should reconnect
info: transport end (error)
debug: cleared close timeout for client ufw4UqC2pCvWqqB5iKOQ
debug: discarding transport
debug: client authorized
info: handshake authorized Uslh5g1YKNE8rCJDiKTd
debug: client authorized
info: handshake authorized -XHkL7Zk9KzA7WEFiKTe
debug: client authorized
info: handshake authorized _OOsM2nZOVgS93R-iKTf
debug: client authorized
info: handshake authorized IZ0vUej2iX_-TRSJiKTg
debug: setting request GET /socket.io/1/xhr-polling/IZ0vUej2iX_-TRSJiKTg?t=1345273505717
debug: setting poll timeout
debug: client authorized for 
debug: clearing poll timeout
debug: xhr-polling writing 1::
debug: set close timeout for client IZ0vUej2iX_-TRSJiKTg

この問題をデバッグする方法や、実際に問題が何であるかを誰かが提案できますか?

4

2 に答える 2

1
io.on('connection', function (client) {

する必要がありますio.sockets.on(...

client.on('message', function (msg) {
    socket.broadcast(msg);

する必要がありますclient.broadcast(msg);socketどこにも定義されていません)

于 2012-08-18T18:11:06.773 に答える
0

要件に応じて socket.io 設定を構成できます。https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO

私も同じ問題に遭遇し、以下の行を追加すると問題が解決することがわかりました

io.set('transports',['xhr-polling']);
于 2013-10-14T09:21:22.477 に答える