From bed2ea7985345460cefbc427ec4fd7a3f9666230 Mon Sep 17 00:00:00 2001 From: crusader Date: Thu, 24 Aug 2017 20:29:22 +0900 Subject: [PATCH] ing --- clients/options.go | 74 ++++++++++++++++++++++++++++++++++++++----- options.go | 79 ++++++---------------------------------------- server.go | 32 ++++++++++--------- 3 files changed, 92 insertions(+), 93 deletions(-) diff --git a/clients/options.go b/clients/options.go index ffa01aa..f1a33fa 100644 --- a/clients/options.go +++ b/clients/options.go @@ -1,25 +1,83 @@ package clients -import "time" +import ( + "time" -const () + "github.com/valyala/fasthttp" +) + +type ( + OnDisconnectedFunc func(c Client) + 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) +) + +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 + // DefaultReadBufferSize is default value of Read Buffer Size + DefaultReadBufferSize = 4096 + // DefaultWriteBufferSize is default value of Write Buffer Size + DefaultWriteBufferSize = 4096 +) // Options is configuration of the websocket server type Options struct { - OnRequest func(c Client, method string, params interface{}) (interface{}, error) - OnNotify func(c Client, method string, params interface{}) error - OnDisconnected func(c Client) + OnRequest OnRequestFunc + OnNotify OnNotifyFunc + OnDisconnected OnDisconnectedFunc MaxMessageSize int64 - WriteTimeout time.Duration - ReadTimeout time.Duration - BinaryMessage bool ReadBufferSize int WriteBufferSize int + WriteTimeout time.Duration + ReadTimeout time.Duration + PongTimeout time.Duration + PingTimeout time.Duration + PingPeriod time.Duration + BinaryMessage bool } // Validate validates the configuration func (o *Options) Validate() *Options { + 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.ReadBufferSize <= 0 { + o.ReadBufferSize = DefaultReadBufferSize + } + + if o.WriteBufferSize <= 0 { + o.WriteBufferSize = DefaultWriteBufferSize + } if o.OnRequest == nil { o.OnRequest = func(c Client, method string, params interface{}) (interface{}, error) { diff --git a/options.go b/options.go index 5874a8b..bd4c505 100644 --- a/options.go +++ b/options.go @@ -9,10 +9,8 @@ import ( ) type ( - OnConnectionFunc func(c clients.Client) + OnConnectionFunc func(path string, c clients.Client) OnDisconnectedFunc func(c clients.Client) - OnRequestFunc func(c clients.Client, method string, params interface{}) (interface{}, error) - OnNotifyFunc func(c clients.Client, method string, params interface{}) error OnPushFunc func() OnErrorFunc func(ctx *fasthttp.RequestCtx, status int, reason error) OnCheckOriginFunc func(ctx *fasthttp.RequestCtx) bool @@ -22,18 +20,6 @@ type ( const ( // DefaultHandshakeTimeout is default value of websocket handshake Timeout DefaultHandshakeTimeout = 0 - // 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 // DefaultReadBufferSize is default value of Read Buffer Size DefaultReadBufferSize = 4096 // DefaultWriteBufferSize is default value of Write Buffer Size @@ -47,49 +33,19 @@ var ( // Options is configuration of the websocket server type Options struct { - OnConnection OnConnectionFunc - OnDisconnected OnDisconnectedFunc - OnRequest OnRequestFunc - OnNotify OnNotifyFunc - OnPush OnPushFunc - OnCheckOrigin OnCheckOriginFunc - OnError OnErrorFunc - IDGenerator IDGeneratorFunc - HandshakeTimeout time.Duration - WriteTimeout time.Duration - ReadTimeout time.Duration - PingTimeout time.Duration - PongTimeout time.Duration - PingPeriod time.Duration - MaxMessageSize int64 - BinaryMessage bool + 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.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.ReadBufferSize <= 0 { o.ReadBufferSize = DefaultReadBufferSize } @@ -99,7 +55,7 @@ func (o *Options) Validate() *Options { } if o.OnConnection == nil { - o.OnConnection = func(c clients.Client) { + o.OnConnection = func(path string, c clients.Client) { } } @@ -108,23 +64,6 @@ func (o *Options) Validate() *Options { } } - if o.OnRequest == nil { - o.OnRequest = func(c clients.Client, method string, params interface{}) (interface{}, error) { - return nil, nil - } - } - - if o.OnNotify == nil { - o.OnNotify = func(c clients.Client, method string, params interface{}) error { - return nil - } - } - - if o.OnPush == nil { - o.OnPush = func() { - } - } - if o.OnError == nil { o.OnError = func(ctx *fasthttp.RequestCtx, status int, reason error) { } diff --git a/server.go b/server.go index b5732c0..2376bef 100644 --- a/server.go +++ b/server.go @@ -15,10 +15,10 @@ type Server interface { } type server struct { - _option *Options - _clientPption *clients.Options - _upgrader *websocket.Upgrader - _clients map[string]clients.Client + _option *Options + _upgrader *websocket.Upgrader + _handlers map[string]*clients.Options + _clients map[string]clients.Client } func NewServer(o *Options) Server { @@ -32,15 +32,6 @@ func NewServer(o *Options) Server { WriteBufferSize: s._option.WriteBufferSize, HandshakeTimeout: s._option.HandshakeTimeout, } - s._clientPption = &clients.Options{ - OnRequest: s._option.OnRequest, - OnDisconnected: s.onDisconnected, - ReadTimeout: s._option.ReadTimeout, - WriteTimeout: s._option.WriteTimeout, - ReadBufferSize: s._option.ReadBufferSize, - WriteBufferSize: s._option.WriteBufferSize, - MaxMessageSize: s._option.MaxMessageSize, - } return s } @@ -61,13 +52,24 @@ func (s *server) onConnection(ctx *fasthttp.RequestCtx) { log.Print("upgrade:", err) return } + path := string(ctx.Path()) + co, ok := s._handlers[path] + if !ok { + log.Printf("Path[%s] is not exist.", path) + return + } + cid := s._option.IDGenerator(ctx) - c := clients.New(cid, s._clientPption, ctx, conn) + c := clients.New(cid, co, ctx, conn) s._clients[cid] = c - s._option.OnConnection(c) + s._option.OnConnection(path, c) }) } +func (s *server) HandleClient(pattern string, o *clients.Options) { + s._handlers[pattern] = o.Validate() +} + func (s *server) ListenAndServe(addr string) error { return fasthttp.ListenAndServe(addr, s.onConnection) }