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

3

13

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.

Curkell answered 15/12, 2020 at 15:46 Comment(1)
As the error messages states and the request headers confirm, the client did not make a websocket upgrade request. Edit the question to describe how the client made the request. Include code if possibe.Germann
B
10

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.

Butt answered 21/11, 2021 at 8:56 Comment(0)
L
3

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
Louielouis answered 16/12, 2020 at 1:12 Comment(0)
B
0

the error has arrived because you called /ws/:roomId manually, this endpoint is hit internal from the frontend. you can go to the index.html file and print console.log(roomId) after this endpoint is hit from frontend, and that will conclude the error!

Bearberry answered 17/3, 2021 at 6:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.