83 lines
1.9 KiB
Go
83 lines
1.9 KiB
Go
package overflow_gateway_websocket
|
|
|
|
import (
|
|
"time"
|
|
|
|
"git.loafle.net/overflow/overflow_gateway_websocket/clients"
|
|
uuid "github.com/satori/go.uuid"
|
|
"github.com/valyala/fasthttp"
|
|
)
|
|
|
|
type (
|
|
OnConnectionFunc func(path string, c clients.Client)
|
|
OnDisconnectedFunc func(c clients.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 = false
|
|
)
|
|
|
|
var (
|
|
// DefaultIDGenerator returns the UUID of the client
|
|
DefaultIDGenerator = func(ctx *fasthttp.RequestCtx) string { return uuid.NewV4().String() }
|
|
)
|
|
|
|
// Options is configuration of the websocket server
|
|
type Options 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 *Options) Validate() *Options {
|
|
if o.ReadBufferSize <= 0 {
|
|
o.ReadBufferSize = DefaultReadBufferSize
|
|
}
|
|
|
|
if o.WriteBufferSize <= 0 {
|
|
o.WriteBufferSize = DefaultWriteBufferSize
|
|
}
|
|
|
|
if o.OnConnection == nil {
|
|
o.OnConnection = func(path string, c clients.Client) {
|
|
}
|
|
}
|
|
|
|
if o.OnDisconnected == nil {
|
|
o.OnDisconnected = func(c clients.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
|
|
}
|