doushenyu2537 2017-02-03 21:35
浏览 75
已采纳

在AWS中使用Golang创建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

", 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条回答 默认 最新

  • doutany76678 2017-02-03 21:51
    关注

    I believe it should be wss://www.weldpad.com/wss? not wss://https://www.weldpad.com/wss?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?