R
Railway14mo ago
ferret

websocket: bad handshake with gorilla websockets

i have a websocket server running on railway with a custom domain, and when going to the websocket route in the browser, i get the expected error:
Bad Request
websocket: the client is not using the websocket protocol: 'upgrade' token not found in 'Connection' header
Bad Request
websocket: the client is not using the websocket protocol: 'upgrade' token not found in 'Connection' header
so i think the server is exposed correctly. however, when connecting using my cli app with the gorilla websockets package, i get this error:
2023/09/19 18:30:04 websocket: bad handshake
exit status 1
2023/09/19 18:30:04 websocket: bad handshake
exit status 1
i'm not entirely sure why. code for connecting:
package client

import (
"fmt"
"net/url"
"os"
"os/signal"

"github.com/gorilla/websocket"
)

type StartRequest struct {
MetaApiKey string `json:"META_API_KEY"`
RiskPercent float64 `json:"RISK_PERCENT"`
AccountId string `json:"ACCOUNT_ID"`
BrokerSuffix string `json:"BROKER_SUFFIX"`
}

func Connect() (*websocket.Conn, chan struct{}, error) {
interrupt := make(chan os.Signal, 1)

signal.Notify(interrupt, os.Interrupt)

u := url.URL{Scheme: "ws", Host: os.Getenv("SERVER_HOST"), Path: "/websocket"}

fmt.Println(u.String())

c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)

if err != nil {
return &websocket.Conn{}, make(chan struct{}, 0), err
}

done := make(chan struct{}, 1)

go func() {
<-done

c.Close()
}()

return c, done, nil
}
package client

import (
"fmt"
"net/url"
"os"
"os/signal"

"github.com/gorilla/websocket"
)

type StartRequest struct {
MetaApiKey string `json:"META_API_KEY"`
RiskPercent float64 `json:"RISK_PERCENT"`
AccountId string `json:"ACCOUNT_ID"`
BrokerSuffix string `json:"BROKER_SUFFIX"`
}

func Connect() (*websocket.Conn, chan struct{}, error) {
interrupt := make(chan os.Signal, 1)

signal.Notify(interrupt, os.Interrupt)

u := url.URL{Scheme: "ws", Host: os.Getenv("SERVER_HOST"), Path: "/websocket"}

fmt.Println(u.String())

c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)

if err != nil {
return &websocket.Conn{}, make(chan struct{}, 0), err
}

done := make(chan struct{}, 1)

go func() {
<-done

c.Close()
}()

return c, done, nil
}
15 Replies
Percy
Percy14mo ago
Project ID: 8c9a3492-54aa-4b38-8d6f-03e1be90910a
ferret
ferret14mo ago
8c9a3492-54aa-4b38-8d6f-03e1be90910a
Brody
Brody14mo ago
have you tried connecting the the websocket server with postman or insomnia?
ferret
ferret14mo ago
i haven't
Brody
Brody14mo ago
give that a shot
ferret
ferret14mo ago
i didn't even know they had websocket support
Brody
Brody14mo ago
yep both do!
ferret
ferret14mo ago
that's cool
Brody
Brody14mo ago
let me know how that goes
ferret
ferret14mo ago
No description
Brody
Brody14mo ago
server works there just something wrong with the client code
ferret
ferret14mo ago
yeah
Brody
Brody14mo ago
Scheme: "ws" you need to use wss since railway is https only
ferret
ferret14mo ago
oh okay that did the trick thank you
Brody
Brody14mo ago
no problem!
Want results from more Discord servers?
Add your server