doucuodan0897 2017-12-28 12:55
浏览 235
已采纳

在Go中连接到安全WebSocket时遇到问题

I'm having some troubles doing this exact connection in go-lang.

Thus far, I've done:

u := url.URL{Scheme: "wss", Host: "streamer.cryptocompare.com"}

var dialer *websocket.Dialer
conn, _, err := dialer.Dial(u.String(), nil)
l.Log(err)

msg, err := json.Marshal(Message{"SubAdd", map[string]interface{}{"subs": []string{"5~CCCAGG~BTC~USD"}}})
l.Log(err)

err = conn.WriteMessage(websocket.TextMessage, msg)
fmt.Println(err.Error())
l.Log(err)

I'm still learning about websockets, and how to use them.

Edit: This is the error, I'm getting:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x718937]

goroutine 22 [running]:
github.com/gorilla/websocket.(*Conn).WriteMessage(0x0, 0x1, 0xc420248020, 0x36, 0x40, 0x0, 0x0)
    /home/avl/go/src/github.com/gorilla/websocket/conn.go:734 +0x37
main.SetupSockets(0xc420058370)
    /home/avl/Dropbox/Development/mixed/server/crypto.go:55 +0x378
created by main.main
    /home/avl/Dropbox/Development/mixed/server/main.go:21 +0x9a
exit status 2

Edit 2: Line 55 in crypto.go corresponds to this msg, err := json.Marshal(Message{"SubAdd", map[string]interface{}{"subs": []string{"5~CCCAGG~BTC~USD"}}})

  • 写回答

1条回答 默认 最新

  • du2986 2017-12-28 13:46
    关注

    Here you get error while conn, _, err := dialer.Dial(u.String(), nil)

    unexpected EOF

    You have to handle this error, it occurs because of response of the server:

    https://github.com/gorilla/websocket/blob/master/client.go#L282

    resp, err := http.ReadResponse(conn.br, req)
    if err != nil {
        return nil, nil, err
    }
    

    After that error WriteMessage method doesn't make sense.

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

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死