2017-07-13 12:01:28 +00:00
|
|
|
package server
|
2017-07-10 11:54:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2017-07-14 11:18:07 +00:00
|
|
|
"git.loafle.net/overflow/overflow_service_websocket/config"
|
2017-07-10 11:54:40 +00:00
|
|
|
uuid "github.com/satori/go.uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2017-07-12 12:36:12 +00:00
|
|
|
// DefaultPingTimeout is default value of Ping Timeout
|
|
|
|
DefaultPingTimeout = 10 * time.Second
|
2017-07-10 11:54:40 +00:00
|
|
|
// DefaultPingPeriod is default value of Ping Period
|
|
|
|
DefaultPingPeriod = (DefaultPongTimeout * 9) / 10
|
|
|
|
// DefaultMaxMessageSize is default value of Max Message Size
|
|
|
|
DefaultMaxMessageSize = 1024
|
|
|
|
// 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(*http.Request) string { return uuid.NewV4().String() }
|
|
|
|
)
|
|
|
|
|
2017-07-11 08:05:06 +00:00
|
|
|
// Options is configuration of the websocket server
|
|
|
|
type Options struct {
|
2017-07-13 12:01:28 +00:00
|
|
|
OnError func(res http.ResponseWriter, req *http.Request, status int, reason error)
|
|
|
|
OnCheckOrigin func(req *http.Request) bool
|
2017-07-10 11:54:40 +00:00
|
|
|
WriteTimeout time.Duration
|
|
|
|
ReadTimeout time.Duration
|
|
|
|
PongTimeout time.Duration
|
2017-07-12 12:36:12 +00:00
|
|
|
PingTimeout time.Duration
|
2017-07-10 11:54:40 +00:00
|
|
|
PingPeriod time.Duration
|
|
|
|
MaxMessageSize int64
|
2017-07-12 12:36:12 +00:00
|
|
|
BinaryMessage bool
|
2017-07-10 11:54:40 +00:00
|
|
|
ReadBufferSize int
|
|
|
|
WriteBufferSize int
|
|
|
|
IDGenerator func(*http.Request) string
|
|
|
|
}
|
|
|
|
|
2017-07-14 11:18:07 +00:00
|
|
|
func NewOptions(c *config.WebsocketConfig) *Options {
|
|
|
|
o := &Options{
|
|
|
|
WriteTimeout: time.Duration(c.WriteTimeout) * time.Second,
|
|
|
|
ReadTimeout: time.Duration(c.ReadTimeout) * time.Second,
|
|
|
|
PongTimeout: time.Duration(c.PongTimeout) * time.Second,
|
|
|
|
PingTimeout: time.Duration(c.PingTimeout) * time.Second,
|
|
|
|
MaxMessageSize: c.MaxMessageSize,
|
|
|
|
BinaryMessage: c.UseBinaryMessage,
|
|
|
|
ReadBufferSize: c.ReadBufferSize,
|
|
|
|
WriteBufferSize: c.WriteBufferSize,
|
|
|
|
}
|
|
|
|
o.Validate()
|
|
|
|
o.PongTimeout = o.PongTimeout * time.Second
|
|
|
|
o.PingTimeout = o.PingTimeout * time.Second
|
|
|
|
o.PingPeriod = (o.PingTimeout * 9) / 10
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
2017-07-10 11:54:40 +00:00
|
|
|
// Set is the func which makes the OptionSet an OptionSetter, this is used mostly
|
2017-07-11 08:05:06 +00:00
|
|
|
func (o *Options) Set(main *Options) {
|
2017-07-13 12:01:28 +00:00
|
|
|
main.OnError = o.OnError
|
|
|
|
main.OnCheckOrigin = o.OnCheckOrigin
|
2017-07-11 08:05:06 +00:00
|
|
|
main.WriteTimeout = o.WriteTimeout
|
|
|
|
main.ReadTimeout = o.ReadTimeout
|
|
|
|
main.PongTimeout = o.PongTimeout
|
2017-07-12 12:36:12 +00:00
|
|
|
main.PingTimeout = o.PingTimeout
|
2017-07-11 08:05:06 +00:00
|
|
|
main.PingPeriod = o.PingPeriod
|
|
|
|
main.MaxMessageSize = o.MaxMessageSize
|
2017-07-12 12:36:12 +00:00
|
|
|
main.BinaryMessage = o.BinaryMessage
|
2017-07-11 08:05:06 +00:00
|
|
|
main.ReadBufferSize = o.ReadBufferSize
|
|
|
|
main.WriteBufferSize = o.WriteBufferSize
|
|
|
|
main.IDGenerator = o.IDGenerator
|
2017-07-10 11:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates the configuration
|
2017-07-11 08:05:06 +00:00
|
|
|
func (o *Options) Validate() {
|
2017-07-10 11:54:40 +00:00
|
|
|
|
2017-07-11 08:05:06 +00:00
|
|
|
if o.WriteTimeout < 0 {
|
|
|
|
o.WriteTimeout = DefaultWriteTimeout
|
2017-07-10 11:54:40 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 08:05:06 +00:00
|
|
|
if o.ReadTimeout < 0 {
|
|
|
|
o.ReadTimeout = DefaultReadTimeout
|
2017-07-10 11:54:40 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 08:05:06 +00:00
|
|
|
if o.PongTimeout < 0 {
|
|
|
|
o.PongTimeout = DefaultPongTimeout
|
2017-07-10 11:54:40 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 08:05:06 +00:00
|
|
|
if o.PingPeriod <= 0 {
|
|
|
|
o.PingPeriod = DefaultPingPeriod
|
2017-07-10 11:54:40 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 08:05:06 +00:00
|
|
|
if o.MaxMessageSize <= 0 {
|
|
|
|
o.MaxMessageSize = DefaultMaxMessageSize
|
2017-07-10 11:54:40 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 08:05:06 +00:00
|
|
|
if o.ReadBufferSize <= 0 {
|
|
|
|
o.ReadBufferSize = DefaultReadBufferSize
|
2017-07-10 11:54:40 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 08:05:06 +00:00
|
|
|
if o.WriteBufferSize <= 0 {
|
|
|
|
o.WriteBufferSize = DefaultWriteBufferSize
|
2017-07-10 11:54:40 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 12:01:28 +00:00
|
|
|
if o.OnError == nil {
|
|
|
|
o.OnError = func(res http.ResponseWriter, req *http.Request, status int, reason error) {
|
2017-07-10 11:54:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-13 12:01:28 +00:00
|
|
|
if o.OnCheckOrigin == nil {
|
|
|
|
o.OnCheckOrigin = func(req *http.Request) bool {
|
2017-07-10 11:54:40 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-11 08:05:06 +00:00
|
|
|
if o.IDGenerator == nil {
|
|
|
|
o.IDGenerator = DefaultIDGenerator
|
2017-07-10 11:54:40 +00:00
|
|
|
}
|
|
|
|
}
|