本文整理匯總了Golang中github.com/gorilla/websocket.Dialer.HandshakeTimeout方法的典型用法代碼示例。如果您正苦於以下問題:Golang Dialer.HandshakeTimeout方法的具體用法?Golang Dialer.HandshakeTimeout怎麽用?Golang Dialer.HandshakeTimeout使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/gorilla/websocket.Dialer
的用法示例。
在下文中一共展示了Dialer.HandshakeTimeout方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ConnectWebsocketSession
func ConnectWebsocketSession(opts *DialOptions) (*WebsocketSession, error) {
dialURL, err := url.Parse(opts.BaseURL)
if err != nil {
return nil, err
}
// will be used to set the origin header
originalScheme := dialURL.Scheme
if err := replaceSchemeWithWS(dialURL); err != nil {
return nil, err
}
if err := addMissingPortAndSlash(dialURL); err != nil {
return nil, err
}
serverID := threeDigits()
sessionID := randomStringLength(20)
// Add server_id and session_id to the path.
dialURL.Path += serverID + "/" + sessionID + "/websocket"
requestHeader := http.Header{}
requestHeader.Add("Origin", originalScheme+"://"+dialURL.Host)
ws := websocket.Dialer{
ReadBufferSize: opts.ReadBufferSize,
WriteBufferSize: opts.WriteBufferSize,
}
// if the user passed a timeout, us a dial with a timeout
if opts.Timeout != 0 {
ws.NetDial = func(network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, opts.Timeout)
}
// this is used as Deadline inside gorillas dialer method
ws.HandshakeTimeout = opts.Timeout
}
conn, _, err := ws.Dial(dialURL.String(), requestHeader)
if err != nil {
return nil, err
}
session := NewWebsocketSession(conn)
session.id = sessionID
return session, nil
}