websocket_fasthttp/client/socket_builders.go
crusader a5e92f2b11 ing
2017-12-08 16:41:43 +09:00

105 lines
2.2 KiB
Go

package client
import (
"crypto/tls"
"net"
"net/http"
"net/url"
"time"
"git.loafle.net/commons_go/logging"
cuc "git.loafle.net/commons_go/util/context"
cwf "git.loafle.net/commons_go/websocket_fasthttp"
)
type SocketBuilders struct {
SocketHandler SocketHandler
URL string
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)
}
func (sb *SocketBuilders) GetSocketHandler() SocketHandler {
return sb.SocketHandler
}
func (sb *SocketBuilders) GetHandshakeTimeout() time.Duration {
return sb.HandshakeTimeout
}
func (sb *SocketBuilders) GetURL() string {
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 nil == sb.SocketHandler {
logging.Logger().Panic("Websocket Fasthttp: SocketHandler must be specified")
}
sb.SocketHandler.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
}
}