diff --git a/clients/client.go b/client.go similarity index 91% rename from clients/client.go rename to client.go index 314c993..31969e0 100644 --- a/clients/client.go +++ b/client.go @@ -1,4 +1,4 @@ -package clients +package overflow_gateway_websocket import ( "encoding/json" @@ -8,7 +8,7 @@ import ( "sync" "time" - "git.loafle.net/overflow/overflow_gateway_websocket/clients/protocol" + "git.loafle.net/overflow/overflow_gateway_websocket/protocol" "git.loafle.net/overflow/overflow_gateway_websocket/websocket" "github.com/valyala/fasthttp" @@ -22,14 +22,14 @@ type Client interface { type client struct { id string - o *Options + o *ClientOptions fastHTTPCtx *fasthttp.RequestCtx conn *websocket.Conn messageType int writeMTX sync.Mutex } -func New(id string, o *Options, ctx *fasthttp.RequestCtx, conn *websocket.Conn) Client { +func NewClient(id string, o *ClientOptions, ctx *fasthttp.RequestCtx, conn *websocket.Conn) Client { c := &client{ id: id, o: o, diff --git a/clients/options.go b/client_options.go similarity index 59% rename from clients/options.go rename to client_options.go index f1a33fa..f4988a4 100644 --- a/clients/options.go +++ b/client_options.go @@ -1,4 +1,4 @@ -package clients +package overflow_gateway_websocket import ( "time" @@ -7,10 +7,9 @@ import ( ) 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) + 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 ( @@ -26,31 +25,25 @@ const ( 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 { +// ClientOptions is configuration of the websocket server +type ClientOptions struct { OnRequest OnRequestFunc OnNotify OnNotifyFunc OnDisconnected OnDisconnectedFunc - MaxMessageSize int64 - ReadBufferSize int - WriteBufferSize int - WriteTimeout time.Duration - ReadTimeout time.Duration - PongTimeout time.Duration - PingTimeout time.Duration - PingPeriod time.Duration - BinaryMessage bool + 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 *Options) Validate() *Options { +func (o *ClientOptions) Validate() *ClientOptions { if o.WriteTimeout < 0 { o.WriteTimeout = DefaultWriteTimeout } @@ -71,14 +64,6 @@ func (o *Options) Validate() *Options { 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) { return nil, nil diff --git a/clients/protocol/server_error.go b/protocol/server_error.go similarity index 100% rename from clients/protocol/server_error.go rename to protocol/server_error.go diff --git a/clients/protocol/server_request.go b/protocol/server_request.go similarity index 100% rename from clients/protocol/server_request.go rename to protocol/server_request.go diff --git a/clients/protocol/server_response.go b/protocol/server_response.go similarity index 100% rename from clients/protocol/server_response.go rename to protocol/server_response.go diff --git a/server.go b/server.go index 7b7d9b5..e739eb2 100644 --- a/server.go +++ b/server.go @@ -3,7 +3,6 @@ package overflow_gateway_websocket import ( "log" - "git.loafle.net/overflow/overflow_gateway_websocket/clients" "git.loafle.net/overflow/overflow_gateway_websocket/websocket" "github.com/valyala/fasthttp" ) @@ -12,21 +11,21 @@ type () type Server interface { ListenAndServe(addr string) error - HandleClient(pattern string, o *clients.Options) + HandleClient(pattern string, o *ClientOptions) } type server struct { - _option *Options + _option *ServerOptions _upgrader *websocket.Upgrader - _handlers map[string]*clients.Options - _clients map[string]clients.Client + _handlers map[string]*ClientOptions + _clients map[string]Client } -func NewServer(o *Options) Server { +func NewServer(o *ServerOptions) Server { s := &server{ _option: o.Validate(), - _handlers: make(map[string]*clients.Options, 1), - _clients: make(map[string]clients.Client, 100), + _handlers: make(map[string]*ClientOptions, 1), + _clients: make(map[string]Client, 100), } s._upgrader = &websocket.Upgrader{ @@ -45,7 +44,7 @@ func (s *server) onPush(cb OnPushFunc) { } -func (s *server) onDisconnected(c clients.Client) { +func (s *server) onDisconnected(c Client) { delete(s._clients, c.ID()) s._option.OnDisconnected(c) @@ -66,13 +65,14 @@ func (s *server) onConnection(ctx *fasthttp.RequestCtx) { } cid := s._option.IDGenerator(ctx) - c := clients.New(cid, co, ctx, conn) + c := NewClient(cid, co, ctx, conn) s._clients[cid] = c s._option.OnConnection(path, c) }) } -func (s *server) HandleClient(pattern string, o *clients.Options) { +func (s *server) HandleClient(pattern string, o *ClientOptions) { + s._handlers[pattern] = o.Validate() } diff --git a/options.go b/server_options.go similarity index 78% rename from options.go rename to server_options.go index fc4be06..39b0d70 100644 --- a/options.go +++ b/server_options.go @@ -3,14 +3,13 @@ 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) + OnConnectionFunc func(path string, c Client) + OnDisconnectedFunc func(c Client) OnPushFunc func() ) @@ -20,7 +19,8 @@ const ( // DefaultReadBufferSize is default value of Read Buffer Size DefaultReadBufferSize = 4096 // DefaultWriteBufferSize is default value of Write Buffer Size - DefaultWriteBufferSize = 4096 + DefaultWriteBufferSize = 4096 + // DefaultEnableCompression is default value of support compression DefaultEnableCompression = false ) @@ -29,8 +29,8 @@ var ( DefaultIDGenerator = func(ctx *fasthttp.RequestCtx) string { return uuid.NewV4().String() } ) -// Options is configuration of the websocket server -type Options struct { +// ServerOptions is configuration of the websocket server +type ServerOptions struct { OnConnection OnConnectionFunc OnDisconnected OnDisconnectedFunc OnCheckOrigin func(ctx *fasthttp.RequestCtx) bool @@ -44,7 +44,7 @@ type Options struct { } // Validate validates the configuration -func (o *Options) Validate() *Options { +func (o *ServerOptions) Validate() *ServerOptions { if o.ReadBufferSize <= 0 { o.ReadBufferSize = DefaultReadBufferSize } @@ -54,12 +54,12 @@ func (o *Options) Validate() *Options { } if o.OnConnection == nil { - o.OnConnection = func(path string, c clients.Client) { + o.OnConnection = func(path string, c Client) { } } if o.OnDisconnected == nil { - o.OnDisconnected = func(c clients.Client) { + o.OnDisconnected = func(c Client) { } }