新增:广播消息;

优化:订阅服务消息发送使用读锁

git-svn-id: svn://47.119.165.148/zhub@76 e63fbceb-bcc3-4977-ac22-735b83d8d0f4
This commit is contained in:
lxy
2021-01-21 11:05:49 +00:00
parent 03a7118598
commit 3c66a41de2
4 changed files with 66 additions and 39 deletions

View File

@@ -17,7 +17,7 @@ var (
)
type ZSub struct {
sync.Mutex
sync.RWMutex
topics map[string]*ZTopic
timers map[string]*ZTimer
}
@@ -99,9 +99,9 @@ accept topic message
1、send message to topic's chan
2、feedback send success to sender, and sending message to topic's subscripts
*/
func (s *ZSub) publish(topic string, msg string) {
s.Lock()
defer s.Unlock()
func (s *ZSub) publish(topic, msg string) {
s.RLock()
defer s.RUnlock()
ztopic := s.topics[topic] //ZTopic
if ztopic == nil {
return
@@ -110,6 +110,25 @@ func (s *ZSub) publish(topic string, msg string) {
ztopic.mcount++
}
/*
send broadcast message
*/
func (s *ZSub) broadcast(topic, msg string) {
s.RLock()
defer s.RUnlock()
ztopic := s.topics[topic] //ZTopic
if ztopic == nil {
return
}
for _, group := range ztopic.groups {
for _, conn := range group.conns {
conn.send("message", topic, msg)
}
}
}
func (s *ZSub) close(c *ZConn) {
// sub
for _, topic := range c.topics {