deprecated_overflow_gateway.../server_options.go
crusader 24f7b3d3d6 ing
2017-08-25 11:33:00 +09:00

83 lines
1.9 KiB
Go

package overflow_gateway_websocket
import (
"time"
uuid "github.com/satori/go.uuid"
"github.com/valyala/fasthttp"
)
type (
OnConnectionFunc func(path string, c Client)
OnDisconnectedFunc func(c Client)
OnPushFunc func()
)
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 func(ctx *fasthttp.RequestCtx) bool
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(path string, c Client) {
}
}
if o.OnDisconnected == nil {
o.OnDisconnected = func(c Client) {
}
}
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
}