Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to a WebSocket in Golang fails with bad handshake

I am trying to connect to a websocket in Golang using github.com/gorilla/websocket library but it's failing with a bad handshake.

Golang code :

    c, resp, err := websocket.DefaultDialer.Dial(WEBSOCKET_ENDPOINT, nil)
    if err != nil {
        fmt.Println(resp.Status)
        fmt.Println(err)
    }
    _ = c

Output :

200 OK
websocket: bad handshake

I also tried connecting to the same socket in python using socketio and it works.

Python code:

import socketio
sio = socketio.Client()
sio.connect(WEBSOCKET_ENDPOINT, transports = 'websocket')

@sio.event
def connect():
    print("I'm connected!")

Output :

I'm connected!
like image 330
user40061 Avatar asked Oct 16 '25 06:10

user40061


1 Answers

See What Socket.IO is not:

Although Socket.IO indeed uses WebSocket for transport when possible, it adds additional metadata to each packet. 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 plain WebSocket server either.

In Go you are using Gorilla Websockets which is a standard Websocket client. Your Python example appears to be using python-socketio which is an implementation of Socket.IO.

As per the above quote the two are not directly compatible. I suspect your server is using Socket.IO and, as per the above quote, "a WebSocket client will not be able to successfully connect".

like image 66
Brits Avatar answered Oct 17 '25 22:10

Brits



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!