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

在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?

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

报告相同问题?

悬赏问题

  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络