Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Client is not using the websocket protocol: 'upgrade' token not found in 'Connection' header

I am using Gorilla Websocket package to implement a websocket.

conn, err := upgrader.Upgrade(w, r, nil)

    if err != nil {
        // handle error
        fmt.Println(err)
    }
    defer conn.Close()

I am see the below error

websocket: the client is not using the websocket protocol: 'upgrade' token not found in 'Connection' header

I printed on the header of my request, and I am seeing the below

Sec-Fetch-User ?1
Sec-Fetch-Dest document
Referer http://localhost:4747/home
Cookie myGoLiveCookie=369d99fa-901d-4b23-a64b-4731247de304
Sec-Ch-Ua "Google Chrome";v="87", " Not;A Brand";v="99", "Chromium";v="87"
Sec-Ch-Ua-Mobile ?0
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36
Sec-Fetch-Site same-origin
Accept-Encoding gzip, deflate, br
Upgrade-Insecure-Requests 1
Accept text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Mode navigate
Accept-Language en-GB,en-US;q=0.9,en;q=0.8
Connection keep-alive

There is no Upgrade websocket or Connection Upgrade as per expected

I believe I am facing the exact same issue as this one.

like image 924
Ewan Sou Avatar asked Dec 15 '20 15:12

Ewan Sou


People also ask

How do I upgrade a WebSocket?

Upgrading to a WebSocket connectionThe WebSocket() constructor does all the work of creating an initial HTTP/1.1 connection then handling the handshaking and upgrade process for you. Note: You can also use the "wss://" URL scheme to open a secure WebSocket connection.

How do I enable WebSocket connection?

- In Control Panel, click Programs and Features, and then click Turn Windows features on or off. Expand Internet Information Services, expand World Wide Web Services, expand Application Development Features, and then select WebSocket Protocol. Click OK. Click Close.

What is upgrade in WebSocket?

The Upgrader. Upgrade method upgrades the HTTP server connection to the WebSocket protocol as described in the WebSocket RFC. A summary of the process is this: The client sends an HTTP request requesting that the server upgrade the connection used for the HTTP request to the WebSocket protocol.


2 Answers

The browser js request ws connection method is wrong, the correct ws request code var ws = new WebSocket("ws://localhost:4747/ws");.

A correct ws request header, each header in it is necessary, but the value is different.

GET /chat HTTP/1.1
Host: example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13
Origin: http://example.com
like image 51
eudore Avatar answered Oct 11 '22 21:10

eudore


If you are deploying the WS server in a VM and want this header to be there by default, update your Nginx config file like:

location /ws/ {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_set_header Upgrade websocket;
        proxy_set_header Connection Upgrade;
        proxy_pass         "http://127.0.0.1:8089";
    }

proxy_set_header Upgrade websocket; proxy_set_header Connection Upgrade; will do the job for you even if the client has not passed the headers.

like image 26
KIRAN KUMAR B Avatar answered Oct 11 '22 22:10

KIRAN KUMAR B