英文:
Idiomatic way to timeout websocket.Dial in go?
问题
我有以下代码:
_, err := websocket.Dial("wss://remote-server", "", "http://localhost")
if (err == nil) {
fmt.Println("成功!")
} else {
fmt.Println("失败")
}
当remote-server
宕机时,它需要60秒才能超时。我找到了websocket.SetDeadline
(http://godoc.org/code.google.com/p/go.net/websocket#Conn.SetDeadline),但是我还没有连接可以应用它。我可以将Config
传递给DialConfig
,但是我看不到在那里指定超时时间。
我找到了https://code.google.com/p/go-wiki/wiki/Timeouts,这是我必须这样做吗?我如何正确地将错误代码传递回goroutine?
英文:
I have code like the following:
_, err := websocket.Dial("wss://remote-server", "", "http://localhost")
if (err == nil) {
fmt.Println("Worked!")
} else {
fmt.Println("Fail")
}
When remote-server
is down, it takes 60s to timeout. I found websocket.SetDeadline
(http://godoc.org/code.google.com/p/go.net/websocket#Conn.SetDeadline), but I don't have a connection yet to apply it to. I can pass a Config
to DialConfig
, but I can't see where to specify a timeout doing that.
I found https://code.google.com/p/go-wiki/wiki/Timeouts, is this how I have to do it? How do I correctly pass the error code back from the goroutine?
答案1
得分: 2
我还没有使用过go websocket包,但根据文档,我可以推断出应该使用net.DialTimeout(...)与websocket.NewClient(...)结合使用。
DialConfig的源代码使用了相同的方法,但限制在net.Dial上。
英文:
I havent used the go websocket package yet but from what I can infer from the documentation one should probably use net.DialTimeout(...) coupled with websocket.NewClient(...)
The source of DialConfig uses the same method but limited to net.Dial.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论