R
Railway15mo 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
Percy15mo ago
Project ID: 8c9a3492-54aa-4b38-8d6f-03e1be90910a
ferret
ferretOP15mo ago
8c9a3492-54aa-4b38-8d6f-03e1be90910a
Brody
Brody15mo ago
have you tried connecting the the websocket server with postman or insomnia?
ferret
ferretOP15mo ago
i haven't
Brody
Brody15mo ago
give that a shot
ferret
ferretOP15mo ago
i didn't even know they had websocket support
Brody
Brody15mo ago
yep both do!
ferret
ferretOP15mo ago
that's cool
Brody
Brody15mo ago
let me know how that goes
ferret
ferretOP15mo ago
No description
Brody
Brody15mo ago
server works there just something wrong with the client code
ferret
ferretOP15mo ago
yeah
Brody
Brody15mo ago
Scheme: "ws" you need to use wss since railway is https only
ferret
ferretOP15mo ago
oh okay that did the trick thank you
Brody
Brody15mo ago
no problem!
Want results from more Discord servers?
Add your server