开发者

Chrome & Safari revert to xhr-polling rather than websockets with socket.io

开发者 https://www.devze.com 2023-03-11 02:16 出处:网络
I have a problem with websockets and socket.io. When I try to connect to my node server with socket.io it initially connects using websockets but when reverts to jsonp-polling shortly after.

I have a problem with websockets and socket.io. When I try to connect to my node server with socket.io it initially connects using websockets but when reverts to jsonp-polling shortly after.

This is the output from the node sever when I connect:

8 Jun 07:01:15 - Initializing client with transport "websocket"
8 Jun 07:01:19 - Initializing client with transport "jsonp-polling"
8 Jun 07:01:19 - Client 16630339180119336 connected

This happens in Chrome & Safari. I have updated to the latest socket.io version 0.6.17 and am running node 0.4.7.

I have tried deleteing my cookies and cache as suggested on github and SO, however the problem remains. Also, w开发者_如何学Gohen I try to force websockets it never fully connects with a session ID.

Does anyone have any ideas?


Websocket API is not supported by default in all the browsers at the moment (as per my knowledge) it should work on chromium though try testing it on chromium or firefox(after editing the default settings)and see if that still reverts to XHRPolling.

I am running it on a different IP as I need to run node on port 80 which causes conflict on my web server with Apache. Can websockets/flashsockets not be use cross-domain?

Now there might be 2 different reasons for the bug from here

  1. Web/Flash Sockets will not let u connect to the node.js client unless either u specify a differnt port like 81 or u specially specify apache to proxy the incoming request to Node. an easy solution could be writing the Node.js based HTTP server to just relay data from Apache (and setting Apache to run on a differnt port then 80)

    This link tells how to do that... in this process you can make Node.js do something like check if the request is from a websocket/httpbrowser if thats an http browser forward the request to Apache if not ie if thats from web/flash sockets then handle the socket accordingly. or as commented on the question. Specify APACHE to proxy to Node.js.

  2. Flashsockets require you to serve a crossdomain policy file on port 843 are you sure you are providing a cross domain file? (I think socket.io has inbuilt functionality to do that but still its always good to check.)

As told on the socket.io main website

In order to provide realtime connectivity on every browser, Socket.IO selects the most capable transport at runtime, without it affecting the API.

  • WebSocket
  • Adobe® Flash® Socket
  • AJAX long polling
  • AJAX multipart streaming
  • Forever Iframe
  • JSONP Polling

It's pretty clear that it will revert to AJAX Long Polling if websockets are disabled and Adobe Flash Socket fails to connect (this might be due to the unavailability of the policy file).

Here's a sample code for the cross domain file which you can include in your code and see if that makes your server run with websockets.

var net = require("net");

// Node.js 

var Policy = net.createServer(function(socket)
{
    socket.setEncoding('utf8');
    socket.on('connect',function(){
        console.log("Policy Request");
        socket.end("<?xml version=\"1.0\"?><!DOCTYPE cross-domain-policy SYSTEM \"/xml/dtds/cross-domain-policy.dtd\"><cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" secure=\"false\"/></cross-domain-policy>");
    });
});

Policy.listen(843); 
0

精彩评论

暂无评论...
验证码 换一张
取 消