From abb270181b8b27feb68eb0aa91e5ff88ba7f3781 Mon Sep 17 00:00:00 2001 From: crusader Date: Fri, 1 Dec 2017 17:02:47 +0900 Subject: [PATCH] ing --- server.go | 6 +++--- socket_handler.go | 5 +++-- socket_handlers.go | 10 +++++----- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/server.go b/server.go index cc38ad0..1151dfc 100644 --- a/server.go +++ b/server.go @@ -139,7 +139,9 @@ func (s *server) handleRequest(ctx *fasthttp.RequestCtx) { } var responseHeader *fasthttp.ResponseHeader var socketID string - if socketID, responseHeader = socketHandler.Handshake(s.ctx, ctx); "" == socketID { + socketCTX := socketHandler.SocketContext(s.ctx) + + if socketID, responseHeader = socketHandler.Handshake(socketCTX, ctx); "" == socketID { s.handleError(ctx, http.StatusNotAcceptable, fmt.Errorf("Server: Handshake err")) return } @@ -150,8 +152,6 @@ func (s *server) handleRequest(ctx *fasthttp.RequestCtx) { return } - socketCTX := socketHandler.SocketContext(s.ctx) - soc := newSocket(socketHandler, socketCTX, conn, socketID) logging.Logger().Debug(fmt.Sprintf("Server: Client[%s] is connected.", soc.RemoteAddr())) diff --git a/socket_handler.go b/socket_handler.go index 26cfc58..a03c910 100644 --- a/socket_handler.go +++ b/socket_handler.go @@ -7,6 +7,8 @@ import ( ) type SocketHandler interface { + SocketContext(serverCTX ServerContext) SocketContext + // Init invoked when server is stated // If you override ths method, must call // @@ -21,8 +23,7 @@ type SocketHandler interface { Init(serverCTX ServerContext) error // Handshake do handshake client and server // id is identity of client socket. if id is "", disallow connection - Handshake(serverCTX ServerContext, ctx *fasthttp.RequestCtx) (id string, extensionsHeader *fasthttp.ResponseHeader) - SocketContext(serverCTX ServerContext) SocketContext + Handshake(socketCTX SocketContext, ctx *fasthttp.RequestCtx) (id string, extensionsHeader *fasthttp.ResponseHeader) // OnConnect invoked when client is connected // If you override ths method, must call // diff --git a/socket_handlers.go b/socket_handlers.go index 40c7af8..63598d2 100644 --- a/socket_handlers.go +++ b/socket_handlers.go @@ -29,20 +29,20 @@ type SocketHandlers struct { sockets map[string]Socket } +func (sh *SocketHandlers) SocketContext(serverCTX ServerContext) SocketContext { + return newSocketContext(serverCTX) +} + func (sh *SocketHandlers) Init(serverCTX ServerContext) error { sh.sockets = make(map[string]Socket) return nil } -func (sh *SocketHandlers) Handshake(serverCTX ServerContext, ctx *fasthttp.RequestCtx) (id string, extensionsHeader *fasthttp.ResponseHeader) { +func (sh *SocketHandlers) Handshake(socketCTX SocketContext, ctx *fasthttp.RequestCtx) (id string, extensionsHeader *fasthttp.ResponseHeader) { return "", nil } -func (sh *SocketHandlers) SocketContext(serverCTX ServerContext) SocketContext { - return newSocketContext(serverCTX) -} - func (sh *SocketHandlers) OnConnect(soc Socket) { // no op }