我想创建一个等待服务器运行的websocket客户端.如果服务器关闭连接,则应重新连接.
我尝试的不起作用,我的代码退出时出现运行时错误:
panic: runtime error: invalid memory address or nil pointer dereference
func run() {
origin := "http://localhost:8080/"
url := "ws://localhost:8080/ws"
ws, err := websocket.Dial(url, "", origin)
if err != nil {
fmt.Println("Connection fails, is being re-connection")
main()
}
if _, err := ws.Write([]byte("something")); err != nil {
log.Fatal(err)
}
}
Run Code Online (Sandbox Code Playgroud)