2017-08-25 02:33:00 +00:00
|
|
|
package overflow_gateway_websocket
|
2017-08-24 10:42:40 +00:00
|
|
|
|
2017-08-24 11:29:22 +00:00
|
|
|
import (
|
|
|
|
"time"
|
2017-08-24 10:42:40 +00:00
|
|
|
|
2017-08-24 11:29:22 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2017-08-25 02:33:00 +00:00
|
|
|
OnRequestFunc func(c Client, method string, params interface{}) (interface{}, error)
|
|
|
|
OnNotifyFunc func(c Client, method string, params interface{}) error
|
|
|
|
OnErrorFunc func(ctx *fasthttp.RequestCtx, status int, reason error)
|
2017-08-24 11:29:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
)
|
2017-08-24 10:42:40 +00:00
|
|
|
|
2017-08-25 02:33:00 +00:00
|
|
|
// ClientOptions is configuration of the websocket server
|
|
|
|
type ClientOptions struct {
|
2017-08-24 11:29:22 +00:00
|
|
|
OnRequest OnRequestFunc
|
|
|
|
OnNotify OnNotifyFunc
|
|
|
|
OnDisconnected OnDisconnectedFunc
|
2017-08-24 10:42:40 +00:00
|
|
|
|
2017-08-25 02:33:00 +00:00
|
|
|
MaxMessageSize int64
|
|
|
|
WriteTimeout time.Duration
|
|
|
|
ReadTimeout time.Duration
|
|
|
|
PongTimeout time.Duration
|
|
|
|
PingTimeout time.Duration
|
|
|
|
PingPeriod time.Duration
|
|
|
|
BinaryMessage bool
|
2017-08-24 10:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates the configuration
|
2017-08-25 02:33:00 +00:00
|
|
|
func (o *ClientOptions) Validate() *ClientOptions {
|
2017-08-24 11:29:22 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-08-24 10:42:40 +00:00
|
|
|
if o.OnRequest == nil {
|
|
|
|
o.OnRequest = func(c Client, method string, params interface{}) (interface{}, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.OnNotify == nil {
|
|
|
|
o.OnNotify = func(c Client, method string, params interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.OnDisconnected == nil {
|
|
|
|
o.OnDisconnected = func(c Client) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return o
|
|
|
|
}
|