最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - node.js and socket.io. transport type configuration for websocket? - Stack Overflow

programmeradmin0浏览0评论

This question concerns socket.io versions < 0.9.x. Newer versions have different transports and methods of setting transports.

I test node js and socket.io in two week. when I began I get the problem from socket.send(message) function in client. I can't send any message to the server. But I still can receive messages from the server. I solved this problem when I found the configure transport of server side:

socket.set('transports',[
   'xhr-polling'
  , 'jsonp-polling'
]);

Everything good. Now I can send messages to the server as well. But I still have a question why I have to configure transport. Default socket.io use websocket transport setting like this:

socket.set('transports', [
    'websocket'
  , 'flashsocket'
  , 'htmlfile'
  , 'xhr-polling'
  , 'jsonp-polling'
]);

so it uses websocket at first, not xhr-polling. But the server cannot receive any messages sent from the client when using socket.send(msg) even socket.emit(...).

So the problem is: what is not supporting websocket here? browser or node.js ... I'm sorry but I searched so many pages from google and I haven't found an answer for this.

I use node.js version 0.8.16, socket.io version 0.9.13 and newest browsers: chrome, firefox, opera

I want to use websocket not xhr-polling.

This question concerns socket.io versions < 0.9.x. Newer versions have different transports and methods of setting transports.

I test node js and socket.io in two week. when I began I get the problem from socket.send(message) function in client. I can't send any message to the server. But I still can receive messages from the server. I solved this problem when I found the configure transport of server side:

socket.set('transports',[
   'xhr-polling'
  , 'jsonp-polling'
]);

Everything good. Now I can send messages to the server as well. But I still have a question why I have to configure transport. Default socket.io use websocket transport setting like this:

socket.set('transports', [
    'websocket'
  , 'flashsocket'
  , 'htmlfile'
  , 'xhr-polling'
  , 'jsonp-polling'
]);

so it uses websocket at first, not xhr-polling. But the server cannot receive any messages sent from the client when using socket.send(msg) even socket.emit(...).

So the problem is: what is not supporting websocket here? browser or node.js ... I'm sorry but I searched so many pages from google and I haven't found an answer for this.

I use node.js version 0.8.16, socket.io version 0.9.13 and newest browsers: chrome, firefox, opera

I want to use websocket not xhr-polling.

Share Improve this question edited Sep 23, 2015 at 16:43 SuperBiasedMan 9,96910 gold badges52 silver badges76 bronze badges asked Jan 15, 2013 at 8:43 Rai BlazeRai Blaze 931 gold badge2 silver badges7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

That's odd because even if websockets are not supported by your server configuration, socket.io will select the next best available method (in your case xhr-polling). Actually, you shouldn't even need to set those transports as socket.io will try to use 'websocket' as a primary method by default. This may indicate some other problem, possibly with your code?

What is not supporting websockets is definitely not the browsers you're using nor node.js of course. This will depend on your server setup.

First check:

  1. The port you're listening to is open in your firewall
  2. Your webserver supports websockets. If you're using Apache and proxing your request to an internal IP:PORT, websocket will not work unless you install something like apache-websocket or pywebsocket

What finally solved my issue was to disable Apache listening on port 80 and having node.js listening on that port. Here's the answer on SO that helped me: https://stackoverflow./a/7640966/2347777

发布评论

评论列表(0)

  1. 暂无评论