package overflow_gateway_websocket import ( "time" uuid "github.com/satori/go.uuid" "github.com/valyala/fasthttp" ) type ( OnConnectionFunc func(soc Socket) OnDisconnectedFunc func(soc Socket) OnCheckOriginFunc func(ctx *fasthttp.RequestCtx) bool ) const ( // DefaultHandshakeTimeout is default value of websocket handshake Timeout DefaultHandshakeTimeout = 0 // DefaultReadBufferSize is default value of Read Buffer Size DefaultReadBufferSize = 4096 // DefaultWriteBufferSize is default value of Write Buffer Size DefaultWriteBufferSize = 4096 // DefaultEnableCompression is default value of support compression DefaultEnableCompression = false ) var ( // DefaultIDGenerator returns the UUID of the client DefaultIDGenerator = func(ctx *fasthttp.RequestCtx) string { return uuid.NewV4().String() } ) // ServerOptions is configuration of the websocket server type ServerOptions struct { OnConnection OnConnectionFunc OnDisconnected OnDisconnectedFunc OnCheckOrigin OnCheckOriginFunc OnError func(ctx *fasthttp.RequestCtx, status int, reason error) IDGenerator func(ctx *fasthttp.RequestCtx) string HandshakeTimeout time.Duration ReadBufferSize int WriteBufferSize int EnableCompression bool } // Validate validates the configuration func (o *ServerOptions) Validate() *ServerOptions { if o.ReadBufferSize <= 0 { o.ReadBufferSize = DefaultReadBufferSize } if o.WriteBufferSize <= 0 { o.WriteBufferSize = DefaultWriteBufferSize } if o.OnConnection == nil { o.OnConnection = func(soc Socket) { } } if o.OnDisconnected == nil { o.OnDisconnected = func(soc Socket) { } } if o.OnError == nil { o.OnError = func(ctx *fasthttp.RequestCtx, status int, reason error) { } } if o.OnCheckOrigin == nil { o.OnCheckOrigin = func(ctx *fasthttp.RequestCtx) bool { return true } } if o.IDGenerator == nil { o.IDGenerator = DefaultIDGenerator } return o }