WebSocket closes when sending

So, I saw this wonderful blog post, Experimenting with Node.js. I decided to try and configure it myself using the author gist . This did not work.

Further debugging shows me that the websocket plugs in normally, but closes as soon as send is called. Here is the wire route (sorry for the weird distance):

GET /test HTTP/1.1 Host: 127.0.0.1:8000 Sec-WebSocket-Key2: 3 j 92 9 62" 7 0 8 8 Upgrade: WebSocket Connection: Upgrade Origin: http://127.0.0.1:3000 Sec-WebSocket-Key1: 96'5% S72.93?06 ......(bHTTP/1.1 101 WebSocket Protocol Handshake Upgrade: WebSocket Connection: Upgrade Sec-WebSocket-Origin: http://127.0.0.1:3000 Sec-WebSocket-Location: ws://127.0.0.1:8000/test .4.R....mh.....{.{"action":"move","x":450,"y":22,"w":1146,"h":551}. 

I tried this in both the beta version of Chrome and Firefox 4.0. Both of them exhibit the same behavior. If I go to the original blog site , it works great.

Another thing. If I log into the JS console in either FF or Chrome, and do the following:

 ws = new WebSocket('ws://localhost:8000/test') ws.send("foo") 

It immediately disconnects and does not send a message. The server shows a connection and a handshake, but never receives a message.

I found several questions that were similar, but were resolved without posting the fix or did not seem to apply in this situation. I can post the code from gist if this simplifies.

+6
javascript html5 websocket
source share
3 answers

The main chapter. Despite the fact that I installed the latest version of Node.js, I did not. I have a couple of cars with Node.js on them, I must have lost the track. I had Node.js v0.1.96. After switching to v0.1.102 everything works fine.

Sorry guys!: - D

+1
source share

CloseEvent has a "code" property that will give you information about why your connection was closed.

"Returns an unsigned short containing the closing code sent by the server. The following values ​​are valid status codes."

Various code values ​​are supported. Here are the most famous:

  • 1000: CLOSE_NORMAL
  • 1001: CLOSE_GOING_AWAY
  • 1002: CLOSE_PROTOCOL_ERROR
  • 1003: CLOSE_UNSUPPORTED
  • 1005: CLOSE_NO_STATUS

For more information, see the CloseEvent API documents for MDN .

+2
source share

If the disconnect problem occurs when sending from the browser, you need to wait for the onopen event to occur before sending:

 var conn = new WebSocket('ws://localhost:8000/test'); conn.onopen = function (e) { conn.send('foo'); } conn.onmessage = function (e) { console.log('got: ' + e.data); } 
0
source share

All Articles