在Go语言中,设置WebSocket.Dial的超时时间有几种惯用的方法?

huangapple go评论88阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2013年4月9日 02:29:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/15886323.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定