Close connection after app stops

This commit is contained in:
cyp0633 2022-11-25 10:05:59 +08:00
parent f6a8773d9b
commit 4cbe33f334
Signed by: cyp0633
GPG Key ID: CF90D09FB1FDCE45
2 changed files with 24 additions and 2 deletions

5
app.go
View File

@ -48,6 +48,7 @@ func (a *App) beforeClose(ctx context.Context) (prevent bool) {
func (a *App) shutdown(ctx context.Context) {
// Perform your teardown here
// 在此处做一些资源释放的操作
internal.DoExit()
}
func (a *App) FetchMessages() []internal.Message {
@ -55,6 +56,6 @@ func (a *App) FetchMessages() []internal.Message {
return internal.Messages
}
func(a *App) FetchClients() []string {
func (a *App) FetchClients() []string {
return internal.Clients
}
}

View File

@ -102,3 +102,24 @@ func DoPull() {
})
}
}
var exitRegex = regexp.MustCompile(`^OK`)
func DoExit() {
c := *conn
str := "EXIT"
_, err := c.Write([]byte(str))
if err != nil {
log.Println(err)
}
buf := make([]byte, 1024)
n, err := c.Read(buf)
str = string(buf[:n])
if err != nil || !exitRegex.MatchString(str) {
log.Println(err)
}
c.Close()
conn = nil
}