deprecated_overflow_gateway.../socket_options.go
crusader 03e1946210 ing
2017-08-25 17:13:21 +09:00

78 lines
1.6 KiB
Go

package overflow_gateway_websocket
import (
"log"
"time"
"github.com/valyala/fasthttp"
)
type (
OnErrorFunc func(ctx *fasthttp.RequestCtx, status int, reason error)
)
const (
// DefaultWriteTimeout is default value of Write Timeout
DefaultWriteTimeout = 0
// DefaultReadTimeout is default value of Read Timeout
DefaultReadTimeout = 0
// DefaultPongTimeout is default value of Pong Timeout
DefaultPongTimeout = 60 * time.Second
// DefaultPingTimeout is default value of Ping Timeout
DefaultPingTimeout = 10 * time.Second
// DefaultPingPeriod is default value of Ping Period
DefaultPingPeriod = (DefaultPongTimeout * 9) / 10
// DefaultMaxMessageSize is default value of Max Message Size
DefaultMaxMessageSize = 1024
)
// SocketOptions is configuration of the websocket server
type SocketOptions struct {
onDisconnected OnDisconnectedFunc
Handler MessageHandler
MaxMessageSize int64
WriteTimeout time.Duration
ReadTimeout time.Duration
PongTimeout time.Duration
PingTimeout time.Duration
PingPeriod time.Duration
BinaryMessage bool
}
// Validate validates the configuration
func (o *SocketOptions) Validate() *SocketOptions {
if nil == o.Handler {
log.Fatalf("Message Handler must specified.\n")
return nil
}
if o.WriteTimeout < 0 {
o.WriteTimeout = DefaultWriteTimeout
}
if o.ReadTimeout < 0 {
o.ReadTimeout = DefaultReadTimeout
}
if o.PongTimeout < 0 {
o.PongTimeout = DefaultPongTimeout
}
if o.PingPeriod <= 0 {
o.PingPeriod = DefaultPingPeriod
}
if o.MaxMessageSize <= 0 {
o.MaxMessageSize = DefaultMaxMessageSize
}
if o.onDisconnected == nil {
o.onDisconnected = func(soc Socket) {
}
}
return o
}