websocket_fasthttp/client/socket_builders.go

105 lines
2.2 KiB
Go
Raw Permalink Normal View History

2017-11-30 03:15:05 +00:00
package client
import (
"crypto/tls"
"net"
"net/http"
"net/url"
"time"
2017-12-08 07:41:43 +00:00
"git.loafle.net/commons_go/logging"
2017-11-30 03:15:05 +00:00
cuc "git.loafle.net/commons_go/util/context"
cwf "git.loafle.net/commons_go/websocket_fasthttp"
)
type SocketBuilders struct {
2017-12-08 07:41:43 +00:00
SocketHandler SocketHandler
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) 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)
}
2017-12-08 07:41:43 +00:00
func (sb *SocketBuilders) GetSocketHandler() SocketHandler {
return sb.SocketHandler
}
2017-11-30 03:15:05 +00:00
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() {
2017-12-08 07:41:43 +00:00
if nil == sb.SocketHandler {
2018-03-21 11:11:48 +00:00
logging.Logger().Panicf("Websocket Fasthttp: SocketHandler must be specified")
2017-12-08 07:41:43 +00:00
}
sb.SocketHandler.Validate()
2017-11-30 03:15:05 +00:00
if 0 >= sb.HandshakeTimeout {
sb.HandshakeTimeout = cwf.DefaultHandshakeTimeout
}
if 0 >= sb.ReadBufferSize {
sb.ReadBufferSize = cwf.DefaultReadBufferSize
}
if 0 >= sb.WriteBufferSize {
sb.WriteBufferSize = cwf.DefaultWriteBufferSize
}
}