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 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献