From 164c412c24437ed0625aa62364192097f3095788 Mon Sep 17 00:00:00 2001 From: 兔子 Date: Tue, 29 Dec 2020 14:09:48 +0800 Subject: [PATCH] improved --- client.go | 14 ++++++-------- server.go | 19 +++++++++++-------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/client.go b/client.go index a08ed40..ce3b3bf 100644 --- a/client.go +++ b/client.go @@ -24,8 +24,6 @@ type StarNotifyC struct { stopSign context.Context cancel context.CancelFunc defaultFunc func(CMsg) - // Stop 停止信 号 - Stop chan int // UseChannel 是否使用channel作为信息传递 UseChannel bool isUDP bool @@ -53,11 +51,10 @@ func (star *StarNotifyC) starinitc() { star.Queue.Encode = true star.FuncLists = make(map[string]func(CMsg)) star.UseChannel = false - star.Stop = make(chan int, 5) star.clientSign = make(map[string]chan string) star.Online = false star.lockPool = make(map[string]CMsg) - star.Queue.RestoreDuration(time.Second * 2) + star.Queue.RestoreDuration(time.Millisecond * 50) } // Notify 用于获取一个通知 @@ -167,6 +164,10 @@ func (star *StarNotifyC) Send(name string) error { return star.SendValue(name, "") } +func (star *StarNotifyC) Stoped() <-chan struct{} { + return star.stopSign.Done() +} + func (star *StarNotifyC) SendValueRaw(key string, msg interface{}) error { encodeData, err := encode(msg) if err != nil { @@ -273,7 +274,7 @@ func (star *StarNotifyC) cnotify() { } data, err := star.Queue.RestoreOne() if err != nil { - time.Sleep(time.Microsecond * 2) + time.Sleep(time.Millisecond * 500) continue } if string(data.Msg) == "b612ryzstop" { @@ -343,9 +344,6 @@ func (star *StarNotifyC) ClientStop() { star.Send("b612ryzstop") } star.cancel() - star.Stop <- 1 - star.Stop <- 1 - star.Stop <- 1 } // SetNotify 用于设置关键词的调用函数 diff --git a/server.go b/server.go index 1d6e12c..3bea8a7 100644 --- a/server.go +++ b/server.go @@ -54,8 +54,6 @@ type StarNotifyS struct { listener net.Listener isUDP bool Sync bool - // Stop 停止信 号 - Stop chan int // UDPConn UDP监听 UDPConn *net.UDPConn // Online 当前链接是否处于活跃状态 @@ -90,6 +88,9 @@ func (star *StarNotifyS) getName(conn string) string { } return "" } +func (star *StarNotifyS) Stoped() <-chan struct{} { + return star.stopSign.Done() +} // GetConnPool 获取所有Client端信息 func (star *StarNotifyS) GetConnPool() []SMsg { @@ -233,9 +234,8 @@ func (star *StarNotifyS) starinits() { star.FuncLists = make(map[string]func(SMsg) string) star.nickName = make(map[string]string) star.lockPool = make(map[string]SMsg) - star.Stop = make(chan int, 5) star.Online = false - star.Queue.RestoreDuration(time.Second * 2) + star.Queue.RestoreDuration(time.Millisecond * 50) } // NewNotifyS 开启一个新的Server端通知 @@ -443,10 +443,13 @@ func (star *StarNotifyS) notify() { } data, err := star.Queue.RestoreOne() if err != nil { - time.Sleep(time.Microsecond * 2) + time.Sleep(time.Millisecond * 500) continue } mode, key, value := star.analyseData(string(data.Msg)) + if mode == key && mode == value && mode == "" { + continue + } var rmsg SMsg if !star.isUDP { rmsg = SMsg{data.Conn.(net.Conn), key, value, nil, nil, mode, nil, star.setNickName, star.getName} @@ -507,13 +510,13 @@ func (star *StarNotifyS) notify() { func (star *StarNotifyS) analyseData(msg string) (mode, key, value string) { slice := strings.SplitN(msg, "||", 3) + if len(slice) < 3 { + return "", "", "" + } return slice[0], star.trim(slice[1]), slice[2] } // ServerStop 用于终止Server端运行 func (star *StarNotifyS) ServerStop() { star.cancel() - star.Stop <- 1 - star.Stop <- 1 - star.Stop <- 1 }