2

我试图让 flashsocket 与 socket.io 一起工作,但它没有,总是在 xhr-polling 后备中。

如果有人可以提供帮助,我看不出我做错了什么。

在服务器端

var app = express.createServer(),
io = require('socket.io').listen(app, {
    flashPolicyServer: true,
    transports: ['flashsocket', 'htmlfile', 'xhr-polling', 'jsonp-polling']
});
app.listen(80);

在客户端

...
<script src="/socket.io/socket.io.js"></script>
...
            socket = io.connect();

            socket.on('connect', function(evt) {
                console.log(socket.socket.transport.name);

                onOpen(timeDifference(new Date(), earlierDate), socket.socket.transport.name);
                earlierDate = new Date();
                socket.on('disconnect', function(evt) {
                    onClose(evt);
                });
                socket.on('echo', function(msg) {
                    onEcho(msg);
                });
                socket.on('error', function(evt) {
                    onError(evt);
                });
            });

之后,我检查了我的浏览器 chrome 是否启用了 flash。我还检查了端口 843 和 10843 是否正在侦听和响应:

<cross-domain-policy>
    <allow-access-from domain="*" to-ports="*"/>
</cross-domain-policy>

在服务器日志上,仅获取:

debug - served static content /socket.io.js
debug - client authorized
info  - handshake authorized 14328044138726156
debug - setting request GET /socket.io/1/xhr-polling/14328044138726156?t=1333755740295
debug - setting poll timeout
debug - client authorized for 
debug - clearing poll timeout
debug - xhr-polling writing 1::
debug - set close timeout for client 14328044138726156
debug - setting request GET /socket.io/1/xhr-polling/14328044138726156?t=1333755740299
debug - setting poll timeout
debug - clearing poll timeout
debug - xhr-polling writing 5:::{"name":"echo","args":["transport type : xhr-polling; and socket.id : 14328044138726156"]}
debug - set close timeout for client 14328044138726156
debug - discarding transport
debug - cleared close timeout for client 14328044138726156
debug - setting request GET /socket.io/1/xhr-polling/14328044138726156?t=1333755740303
debug - setting poll timeout
debug - discarding transport
debug - cleared close timeout for client 14328044138726156
debug - clearing poll timeout
debug - xhr-polling writing 8::
debug - set close timeout for client 14328044138726156`

谢谢你的帮助

4

1 回答 1

6

事实上,它有效!

感谢XHR,您带领我进行更多的分析和测试,以便我自己找到。

它可以工作,但与预期的不同:在浏览器上启用 websocket 时,您不能改用 flashsocket。

因此,即使您使用以下方式设置服务器:transports: ['flashsocket', 'htmlfile', 'xhr-polling', 'jsonp-polling']您的 google chrome 也永远不会使用 flashsocket,因为它启用了 websocket 并且它回退到 xhr-polling。但是没有启用 websocket 的 Internet Explorer 将使用 flashsocket。

而且我不需要在没有 websocket 的情况下设置 socket.io,所以这种行为对我有用。

此外,我认为这种行为很好,因为它可以防止在不需要时加载沉重的 .swf 文件。

杰罗姆。

于 2012-04-12T16:54:38.063 回答