增加服务退出时, finish hook支持

This commit is contained in:
白茶清欢 2021-11-17 18:54:34 +08:00
parent 2101bda75c
commit 19cb3e3ddf
2 changed files with 9 additions and 6 deletions

View File

@ -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 停止服务

View File

@ -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
}