Close connection after app stops
This commit is contained in:
parent
f6a8773d9b
commit
4cbe33f334
5
app.go
5
app.go
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue