This commit is contained in:
crusader 2017-12-01 17:02:47 +09:00
parent b19cb58c3e
commit abb270181b
3 changed files with 11 additions and 10 deletions

View File

@ -139,7 +139,9 @@ func (s *server) handleRequest(ctx *fasthttp.RequestCtx) {
} }
var responseHeader *fasthttp.ResponseHeader var responseHeader *fasthttp.ResponseHeader
var socketID string 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")) s.handleError(ctx, http.StatusNotAcceptable, fmt.Errorf("Server: Handshake err"))
return return
} }
@ -150,8 +152,6 @@ func (s *server) handleRequest(ctx *fasthttp.RequestCtx) {
return return
} }
socketCTX := socketHandler.SocketContext(s.ctx)
soc := newSocket(socketHandler, socketCTX, conn, socketID) soc := newSocket(socketHandler, socketCTX, conn, socketID)
logging.Logger().Debug(fmt.Sprintf("Server: Client[%s] is connected.", soc.RemoteAddr())) logging.Logger().Debug(fmt.Sprintf("Server: Client[%s] is connected.", soc.RemoteAddr()))

View File

@ -7,6 +7,8 @@ import (
) )
type SocketHandler interface { type SocketHandler interface {
SocketContext(serverCTX ServerContext) SocketContext
// Init invoked when server is stated // Init invoked when server is stated
// If you override ths method, must call // If you override ths method, must call
// //
@ -21,8 +23,7 @@ type SocketHandler interface {
Init(serverCTX ServerContext) error Init(serverCTX ServerContext) error
// Handshake do handshake client and server // Handshake do handshake client and server
// id is identity of client socket. if id is "", disallow connection // id is identity of client socket. if id is "", disallow connection
Handshake(serverCTX ServerContext, ctx *fasthttp.RequestCtx) (id string, extensionsHeader *fasthttp.ResponseHeader) Handshake(socketCTX SocketContext, ctx *fasthttp.RequestCtx) (id string, extensionsHeader *fasthttp.ResponseHeader)
SocketContext(serverCTX ServerContext) SocketContext
// OnConnect invoked when client is connected // OnConnect invoked when client is connected
// If you override ths method, must call // If you override ths method, must call
// //

View File

@ -29,20 +29,20 @@ type SocketHandlers struct {
sockets map[string]Socket sockets map[string]Socket
} }
func (sh *SocketHandlers) SocketContext(serverCTX ServerContext) SocketContext {
return newSocketContext(serverCTX)
}
func (sh *SocketHandlers) Init(serverCTX ServerContext) error { func (sh *SocketHandlers) Init(serverCTX ServerContext) error {
sh.sockets = make(map[string]Socket) sh.sockets = make(map[string]Socket)
return nil 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 return "", nil
} }
func (sh *SocketHandlers) SocketContext(serverCTX ServerContext) SocketContext {
return newSocketContext(serverCTX)
}
func (sh *SocketHandlers) OnConnect(soc Socket) { func (sh *SocketHandlers) OnConnect(soc Socket) {
// no op // no op
} }