4

As socket.io said, it supports WebSocket, so, I use HTML5 standard web socket api to access socket.io server, but I always get below error:

WebSocket connection to 'ws://localhost:8080/' failed: Connection closed before receiving a handshake response

Then, I tried to use socket.io client in js to access socket.io server, it works, and by chrome network monitoring, i found it using web socket protocol correctly.

Did anyone ever try W3C Websocket api to access socket.io server and met similar issue? or any ideas or clues for my problem? appreciated!

Test code is here: https://github.com/piginzoo/socketiotest

2
  • Now I use socket.io 0.9 to adapt the AndroidASync project, it now only support socket.io 0.9. Commented Aug 5, 2014 at 5:06
  • Had exact same problem with Chrome and IE-11. The latter gives a more specific error message: "WebSocket Error: Network Error 12152, The server returned an invalid or unrecognized response" It does look like socket.io is not really compatible with Websocket API as implemented by either Chrome or IE-11 Commented Jul 31, 2016 at 21:12

3 Answers 3

2

Note: Socket.IO is not a WebSocket implementation. Although Socket.IO indeed uses WebSocket as a transport when possible, it adds some metadata to each packet: the packet type, the namespace and the ack id when a message acknowledgement is needed. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a WebSocket server (like ws://echo.websocket.org) either.
And I find this in socketio github issues:
Socket.IO is not a WebSocket server. Please see ws instead. https://github.com/socketio/socket.io/issues/3022

Sign up to request clarification or add additional context in comments.

Comments

1

The error message you got was because you used the wrong url. The error message "Connection closed before receiving a handshake response" actually told you this (not receiving a response)

It should be 'ws://localhost:8080/socket.io/?EIO=3&transport=websocket' Check here https://socket.io/docs/client-api/#With-custom-path & https://socket.io/docs/internals/ for details

e.g.

"EIO=3"               # the current version of the Engine.IO protocol

BUT as other answer said and actually that was what socket.io readme said

Although Socket.IO indeed uses WebSocket as a transport when possible, it adds some metadata to each packet: the packet type, the namespace and the ack id when a message acknowledgement is needed. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a WebSocket server.

I have tested that to verify both ws & browser native Websocket can't not connect to a Socket.IO server. They are both immediately disconnected from Socket.IO server after they connects, with Socket.IO server send out the error message socket id xxx has disconnected with reason parse error

The ws author also mentioned here https://github.com/websockets/ws/issues/1390

using plain WebSocket to talk with a Socket.IO server does not work seamlessly.

If you want to use browser native Websocket you can use ws can server.

Comments

0

We had exactly the same problem. Ended up with ws websockets. Not sure may be there is better solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.