英文:
Create websocket using golang in aws
问题
我正在尝试在AWS上使用Golang和Gorilla创建Websockets(无需Docker),并使用负载均衡器。我已经将证书连接到负载均衡器。
我成功让代码在HTTP上工作,但是当我尝试在HTTPS上使用代码时,它不起作用。
我做错了什么?
从HTTP切换到HTTPS时,我将客户端请求更改为wss而不是ws,并将证书添加到负载均衡器。
以下是我的代码。
这是我的main.go:
http.HandleFunc("/wss", serveWs)
fmt.Printf("Listening on port %s\n\n", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
fmt.Printf(err.Error())
}
这是serveWs函数:
func serveWs(w http.ResponseWriter, r *http.Request) {
upgrader.CheckOrigin = func(r *http.Request) bool {
// 默认允许所有连接
return true
}
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println(err)
if _, ok := err.(websocket.HandshakeError); !ok {
log.Println(err)
}
return
}
var lastMod time.Time
if n, err := strconv.ParseInt(r.FormValue("lastMod"), 16, 64); err != nil {
lastMod = time.Unix(0, n)
}
go writer(ws, lastMod, w, r)
reader(ws)
}
这是请求:
var conn = new WebSocket("wss://www.weldpad.com/wss?")
当我从客户端发送请求时,我收到以下错误:
failed: Error during WebSocket handshake: Unexpected response code: 400
查看服务器日志,我看到以下内容。
"could not find upgrade header with token 'websocket'"
这是我的负载均衡器配置:
英文:
I am trying to create websockets using golang with gorilla on aws (without docker) with load balancer, I have a certificate connected to the load balancer.
I managed to make the code work with http, but when I try to use the code over https it does not work.
What am I doing wrong?
When moving from http to https I changed the client request to wss instead of ws, and added the certificate to the load balancer.
Here is my code.
This is my main.go
http.HandleFunc("/wss", serveWs)
fmt.Printf("Listening on port %s\n\n", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
fmt.Printf(err.Error())
}
This is the serveWs:
func serveWs(w http.ResponseWriter, r *http.Request) {
upgrader.CheckOrigin = func(r *http.Request) bool {
// allow all connections by default
return true
}
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println(err)
if _, ok := err.(websocket.HandshakeError); !ok {
log.Println(err)
}
return
}
var lastMod time.Time
if n, err := strconv.ParseInt(r.FormValue("lastMod"), 16, 64); err != nil {
lastMod = time.Unix(0, n)
}
go writer(ws, lastMod, w, r)
reader(ws)
}
This is the request:
var conn = new WebSocket("wss://https://www.weldpad.com/wss?"
When I send a request from the client i get the folloing error:
failed: Error during WebSocket handshake: Unexpected response code: 400
Looking at the server log, I see.
"could not find upgrade header with token 'websocket"
This is my load balancer configuration:
答案1
得分: 1
我认为应该是 wss://www.weldpad.com/wss?
而不是 wss://https://www.weldpad.com/wss?
。
英文:
I believe it should be wss://www.weldpad.com/wss?
not wss://https://www.weldpad.com/wss?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论