84 lines
1.9 KiB
Go
84 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()
|
|
OnErrorFunc func(ctx *fasthttp.RequestCtx, status int, reason error)
|
|
OnCheckOriginFunc func(ctx *fasthttp.RequestCtx) bool
|
|
IDGeneratorFunc func(ctx *fasthttp.RequestCtx) string
|
|
)
|
|
|
|
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
|
|
)
|
|
|
|
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 OnCheckOriginFunc
|
|
OnError OnErrorFunc
|
|
IDGenerator IDGeneratorFunc
|
|
|
|
ReadBufferSize int
|
|
WriteBufferSize int
|
|
HandshakeTimeout time.Duration
|
|
}
|
|
|
|
// 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
|
|
}
|