Create websocket using golang in aws

huangapple go评论95阅读模式
英文:

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:

Load Balancing 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?

huangapple
  • 本文由 发表于 2017年2月4日 05:35:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/42033358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定