diff --git a/server.go b/server.go index b2ec746..81d5cdf 100644 --- a/server.go +++ b/server.go @@ -124,13 +124,13 @@ func handleServer(s *server) { var socketID string socketHandler := s.sh.GetSocketHandler() + socketCTX := socketHandler.SocketContext(s.ctx) - if socketID = socketHandler.Handshake(s.ctx, conn); "" == socketID { + if socketID = socketHandler.Handshake(socketCTX, conn); "" == socketID { logging.Logger().Error(fmt.Sprintf("Server: Handshake err addr[%s] %v", conn.RemoteAddr(), conn)) conn.Close() continue } - 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 312836c..cd5c348 100644 --- a/socket_handler.go +++ b/socket_handler.go @@ -21,7 +21,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, conn net.Conn) (id string) + Handshake(socketCTX SocketContext, conn net.Conn) (id string) // OnConnect invoked when client is connected // If you override ths method, must call // diff --git a/socket_handlers.go b/socket_handlers.go index ddfe153..931c951 100644 --- a/socket_handlers.go +++ b/socket_handlers.go @@ -24,20 +24,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, conn net.Conn) (id string) { +func (sh *SocketHandlers) Handshake(socketCTX SocketContext, conn net.Conn) (id string) { return "" } -func (sh *SocketHandlers) SocketContext(serverCTX ServerContext) SocketContext { - return newSocketContext(serverCTX) -} - func (sh *SocketHandlers) OnConnect(soc Socket) { // no op } @@ -57,6 +57,7 @@ func (sh *SocketHandlers) Destroy() { func (sh *SocketHandlers) GetSocket(id string) Socket { return sh.sockets[id] } + func (sh *SocketHandlers) GetSockets() map[string]Socket { return sh.sockets }