ing
This commit is contained in:
parent
af159726d6
commit
4146e11294
26
server.go
26
server.go
@ -23,29 +23,31 @@ type Server interface {
|
||||
|
||||
type server struct {
|
||||
_ctx context.Context
|
||||
_option *ServerOptions
|
||||
_sh ServerHandler
|
||||
_upgrader *websocket.Upgrader
|
||||
_handlers map[string]*SocketOptions
|
||||
_sockets map[string]Socket
|
||||
_socketsCh chan socketsChannelAction
|
||||
}
|
||||
|
||||
func NewServer(ctx context.Context, o *ServerOptions) Server {
|
||||
func NewServer(ctx context.Context, sh ServerHandler) Server {
|
||||
sh.Validate()
|
||||
|
||||
s := &server{
|
||||
_ctx: ctx,
|
||||
_option: o.Validate(),
|
||||
_sh: sh,
|
||||
_handlers: make(map[string]*SocketOptions, 1),
|
||||
_sockets: make(map[string]Socket, 100),
|
||||
_socketsCh: make(chan socketsChannelAction),
|
||||
}
|
||||
|
||||
s._upgrader = &websocket.Upgrader{
|
||||
HandshakeTimeout: s._option.HandshakeTimeout,
|
||||
ReadBufferSize: s._option.ReadBufferSize,
|
||||
WriteBufferSize: s._option.WriteBufferSize,
|
||||
CheckOrigin: s._option.OnCheckOrigin,
|
||||
HandshakeTimeout: s._sh.GetHandshakeTimeout(),
|
||||
ReadBufferSize: s._sh.GetReadBufferSize(),
|
||||
WriteBufferSize: s._sh.GetWriteBufferSize(),
|
||||
CheckOrigin: s._sh.OnCheckOrigin,
|
||||
Error: s.onError,
|
||||
EnableCompression: s._option.EnableCompression,
|
||||
EnableCompression: s._sh.GetEnableCompression(),
|
||||
}
|
||||
|
||||
return s
|
||||
@ -71,12 +73,12 @@ func (s *server) onError(ctx *fasthttp.RequestCtx, status int, reason error) {
|
||||
ctx.Response.Header.Set("Sec-Websocket-Version", "13")
|
||||
ctx.Error(http.StatusText(status), status)
|
||||
|
||||
s._option.OnError(ctx, status, reason)
|
||||
s._sh.OnError(ctx, status, reason)
|
||||
}
|
||||
|
||||
func (s *server) onDisconnected(soc Socket) {
|
||||
s.removeSocket(soc)
|
||||
s._option.OnDisconnected(soc)
|
||||
s._sh.OnDisconnected(soc)
|
||||
}
|
||||
|
||||
func (s *server) onConnection(ctx *fasthttp.RequestCtx) {
|
||||
@ -92,10 +94,10 @@ func (s *server) onConnection(ctx *fasthttp.RequestCtx) {
|
||||
s.onError(ctx, fasthttp.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
id := s._option.IDGenerator(ctx)
|
||||
id := s._sh.OnIDGenerate(ctx)
|
||||
soc := NewSocket(s._ctx, id, path, co, conn)
|
||||
s.addSocket(soc)
|
||||
s._option.OnConnection(soc)
|
||||
s._sh.OnConnection(soc)
|
||||
|
||||
soc.run()
|
||||
})
|
||||
|
22
server_handler.go
Normal file
22
server_handler.go
Normal file
@ -0,0 +1,22 @@
|
||||
package overflow_gateway_websocket
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
type ServerHandler interface {
|
||||
GetHandshakeTimeout() time.Duration
|
||||
GetReadBufferSize() int
|
||||
GetWriteBufferSize() int
|
||||
GetEnableCompression() bool
|
||||
|
||||
OnConnection(soc Socket)
|
||||
OnDisconnected(soc Socket)
|
||||
OnCheckOrigin(ctx *fasthttp.RequestCtx) bool
|
||||
OnError(ctx *fasthttp.RequestCtx, status int, reason error)
|
||||
OnIDGenerate(ctx *fasthttp.RequestCtx) string
|
||||
|
||||
Validate()
|
||||
}
|
56
server_handlers.go
Normal file
56
server_handlers.go
Normal file
@ -0,0 +1,56 @@
|
||||
package overflow_gateway_websocket
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
uuid "github.com/satori/go.uuid"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
// ServerHandlers is implementation of the Server handler interface
|
||||
type ServerHandlers struct {
|
||||
HandshakeTimeout time.Duration
|
||||
ReadBufferSize int
|
||||
WriteBufferSize int
|
||||
EnableCompression bool
|
||||
}
|
||||
|
||||
func (sh *ServerHandlers) GetHandshakeTimeout() time.Duration {
|
||||
return sh.HandshakeTimeout
|
||||
}
|
||||
func (sh *ServerHandlers) GetReadBufferSize() int {
|
||||
return sh.ReadBufferSize
|
||||
}
|
||||
func (sh *ServerHandlers) GetWriteBufferSize() int {
|
||||
return sh.WriteBufferSize
|
||||
}
|
||||
func (sh *ServerHandlers) GetEnableCompression() bool {
|
||||
return sh.EnableCompression
|
||||
}
|
||||
|
||||
func (sh *ServerHandlers) OnConnection(soc Socket) {
|
||||
|
||||
}
|
||||
func (sh *ServerHandlers) OnDisconnected(soc Socket) {
|
||||
|
||||
}
|
||||
func (sh *ServerHandlers) OnCheckOrigin(ctx *fasthttp.RequestCtx) bool {
|
||||
return true
|
||||
}
|
||||
func (sh *ServerHandlers) OnError(ctx *fasthttp.RequestCtx, status int, reason error) {
|
||||
|
||||
}
|
||||
func (sh *ServerHandlers) OnIDGenerate(ctx *fasthttp.RequestCtx) string {
|
||||
return uuid.NewV4().String()
|
||||
}
|
||||
|
||||
// Validate validates the configuration
|
||||
func (sh *ServerHandlers) Validate() {
|
||||
if sh.ReadBufferSize <= 0 {
|
||||
sh.ReadBufferSize = DefaultReadBufferSize
|
||||
}
|
||||
|
||||
if sh.WriteBufferSize <= 0 {
|
||||
sh.WriteBufferSize = DefaultWriteBufferSize
|
||||
}
|
||||
}
|
56
server_options2.go
Normal file
56
server_options2.go
Normal file
@ -0,0 +1,56 @@
|
||||
package overflow_gateway_websocket
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
uuid "github.com/satori/go.uuid"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
// ServerOptions is configuration of the websocket server
|
||||
type ServerOptions2 struct {
|
||||
HandshakeTimeout time.Duration
|
||||
ReadBufferSize int
|
||||
WriteBufferSize int
|
||||
EnableCompression bool
|
||||
}
|
||||
|
||||
func (o *ServerOptions2) GetHandshakeTimeout() time.Duration {
|
||||
return o.HandshakeTimeout
|
||||
}
|
||||
func (o *ServerOptions2) GetReadBufferSize() int {
|
||||
return o.ReadBufferSize
|
||||
}
|
||||
func (o *ServerOptions2) GetWriteBufferSize() int {
|
||||
return o.WriteBufferSize
|
||||
}
|
||||
func (o *ServerOptions2) GetEnableCompression() bool {
|
||||
return o.EnableCompression
|
||||
}
|
||||
|
||||
func (o *ServerOptions2) OnConnection(soc Socket) {
|
||||
|
||||
}
|
||||
func (o *ServerOptions2) OnDisconnected(soc Socket) {
|
||||
|
||||
}
|
||||
func (o *ServerOptions2) OnCheckOrigin(ctx *fasthttp.RequestCtx) bool {
|
||||
return true
|
||||
}
|
||||
func (o *ServerOptions2) OnError(ctx *fasthttp.RequestCtx, status int, reason error) {
|
||||
|
||||
}
|
||||
func (o *ServerOptions2) OnIDGenerate(ctx *fasthttp.RequestCtx) string {
|
||||
return uuid.NewV4().String()
|
||||
}
|
||||
|
||||
// Validate validates the configuration
|
||||
func (o *ServerOptions2) Validate() {
|
||||
if o.ReadBufferSize <= 0 {
|
||||
o.ReadBufferSize = DefaultReadBufferSize
|
||||
}
|
||||
|
||||
if o.WriteBufferSize <= 0 {
|
||||
o.WriteBufferSize = DefaultWriteBufferSize
|
||||
}
|
||||
}
|
25
web_handler.go
Normal file
25
web_handler.go
Normal file
@ -0,0 +1,25 @@
|
||||
package overflow_gateway_websocket
|
||||
|
||||
import "context"
|
||||
|
||||
// ServerOptions is configuration of the websocket server
|
||||
type WebOptions struct {
|
||||
ServerOptions2
|
||||
}
|
||||
|
||||
func (wo *WebOptions) OnConnection(soc Socket) {
|
||||
|
||||
}
|
||||
|
||||
func NewOption() ServerHandler {
|
||||
wo := &WebOptions{}
|
||||
wo.EnableCompression = true
|
||||
wo.Validate()
|
||||
return wo
|
||||
}
|
||||
|
||||
func test() {
|
||||
sh := NewOption()
|
||||
ctx := context.Background()
|
||||
NewServer2(ctx, sh)
|
||||
}
|
@ -186,6 +186,7 @@ func (u *Upgrader) Upgrade(ctx *fasthttp.RequestCtx, responseHeader *fasthttp.Re
|
||||
}
|
||||
|
||||
h := &fasthttp.RequestHeader{}
|
||||
|
||||
//copy request headers in order to have access inside the Conn after
|
||||
ctx.Request.Header.CopyTo(h)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user