websocket_fasthttp/client/socket_builders.go

97 lines
2.0 KiB
Go
Raw Normal View History

2017-11-30 03:15:05 +00:00
package client
import (
"crypto/tls"
"net"
"net/http"
"net/url"
"time"
cuc "git.loafle.net/commons_go/util/context"
cwf "git.loafle.net/commons_go/websocket_fasthttp"
)
type SocketBuilders struct {
2017-12-01 04:00:40 +00:00
URL string
2017-11-30 03:15:05 +00:00
RequestCookie http.CookieJar
RequestHeader http.Header
SubProtocols []string
EnableCompression bool
HandshakeTimeout time.Duration
TLSConfig *tls.Config
ReadBufferSize int
WriteBufferSize int
}
func (sb *SocketBuilders) SocketContext(parent cuc.Context) SocketContext {
return newSocketContext(parent)
}
func (sb *SocketBuilders) SocketHandler() SocketHandler {
return NewSocketHandler()
}
func (sb *SocketBuilders) UseProxy(req *http.Request) (*url.URL, error) {
return http.ProxyFromEnvironment(req)
}
func (sb *SocketBuilders) Dial(network, addr string) (net.Conn, error) {
var deadline time.Time
if 0 != sb.HandshakeTimeout {
deadline = time.Now().Add(sb.HandshakeTimeout)
}
netDialer := &net.Dialer{Deadline: deadline}
return netDialer.Dial(network, addr)
}
func (sb *SocketBuilders) GetHandshakeTimeout() time.Duration {
return sb.HandshakeTimeout
}
2017-12-01 04:00:40 +00:00
func (sb *SocketBuilders) GetURL() string {
2017-11-30 03:15:05 +00:00
return sb.URL
}
func (sb *SocketBuilders) GetRequestCookie() http.CookieJar {
return sb.RequestCookie
}
func (sb *SocketBuilders) GetRequestHeader() http.Header {
return sb.RequestHeader
}
func (sb *SocketBuilders) GetSubProtocols() []string {
return sb.SubProtocols
}
func (sb *SocketBuilders) IsEnableCompression() bool {
return sb.EnableCompression
}
func (sb *SocketBuilders) GetTLSConfig() *tls.Config {
return sb.TLSConfig
}
func (sb *SocketBuilders) GetReadBufferSize() int {
return sb.ReadBufferSize
}
func (sb *SocketBuilders) GetWriteBufferSize() int {
return sb.WriteBufferSize
}
func (sb *SocketBuilders) Validate() {
if 0 >= sb.HandshakeTimeout {
sb.HandshakeTimeout = cwf.DefaultHandshakeTimeout
}
if 0 >= sb.ReadBufferSize {
sb.ReadBufferSize = cwf.DefaultReadBufferSize
}
if 0 >= sb.WriteBufferSize {
sb.WriteBufferSize = cwf.DefaultWriteBufferSize
}
}