本文整理匯總了Golang中github.com/gorilla/websocket.Dialer.NetDial方法的典型用法代碼示例。如果您正苦於以下問題:Golang Dialer.NetDial方法的具體用法?Golang Dialer.NetDial怎麽用?Golang Dialer.NetDial使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/gorilla/websocket.Dialer
的用法示例。
在下文中一共展示了Dialer.NetDial方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ConnectWebsocketSession
// ConnectWebsocketSession dials the remote specified in the opts and
// creates new websocket session.
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 := utils.RandomString(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 custom HTTP client and its transport
// is of *http.Transport type - we're using its Dial field
// for connecting to remote host
if t, ok := opts.Client().Transport.(*http.Transport); ok {
ws.NetDial = t.Dial
}
// if the user passed a timeout, use a dial with a timeout
if opts.Timeout != 0 && ws.NetDial == nil {
// If ws.NetDial is non-nil then gorilla does not
// use ws.HandshakeTimeout for the deadlines.
//
// Instead we're going to set it ourselves.
ws.NetDial = (&net.Dialer{
Timeout: opts.Timeout,
Deadline: time.Now().Add(opts.Timeout),
}).Dial
}
conn, _, err := ws.Dial(dialURL.String(), requestHeader)
if err != nil {
return nil, err
}
session := NewWebsocketSession(conn)
session.id = sessionID
return session, nil
}
示例2: 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
}