diff --git a/construct.go b/construct.go index 5d90367..5e5869b 100644 --- a/construct.go +++ b/construct.go @@ -60,7 +60,7 @@ var ( // Author : go_developer@163.com<白茶清欢> // // Date : 6:49 下午 2021/3/27 -func NewWebsocketServe(wsInstanceList ...abstract.IWebsocket) error { +func NewWebsocketServe(finishHook func(), wsInstanceList ...abstract.IWebsocket) error { if len(wsInstanceList) == 0 { return errors.WithStack(errors.New("register websocket server list is empty")) } @@ -70,7 +70,7 @@ func NewWebsocketServe(wsInstanceList ...abstract.IWebsocket) error { for _, wsInstance := range wsInstanceList { initServer(wsInstance) } - run() + run(finishHook) return nil } @@ -321,7 +321,7 @@ func dispatchCommand(ctx *context.WSContext, data []byte) error { } // 启动所有端口的监听 -func run() { +func run(finishHook func()) { for port, ginInstance := range ginRouterTable { go func(ginInstance *gin.Engine, port int) { if err := ginInstance.Run(fmt.Sprintf(":%d", port)); nil != err { @@ -331,7 +331,10 @@ func run() { } <-sigChan - // TODO : 增加后置hook + // 增加后置hook + if nil != finishHook { + finishHook() + } } // Stop 停止服务 diff --git a/example/server.go b/example/server.go index 246fe86..6e56755 100644 --- a/example/server.go +++ b/example/server.go @@ -23,7 +23,7 @@ import ( ) func main() { - websocket.NewWebsocketServe(&Example{}) + _ = websocket.NewWebsocketServe(nil, &Example{}) } type Example struct { @@ -31,7 +31,7 @@ type Example struct { func (e Example) Connect(ctx *context.WSContext) error { fmt.Println("建立连接成功") - message.Response(ctx, map[string]interface{}{"say": "hello world!", "cid": ctx.ConnectionID}) + _ = message.Response(ctx, map[string]interface{}{"say": "hello world!", "cid": ctx.ConnectionID}) return nil }