广播服务上线
This commit is contained in:
parent
77ed185e08
commit
96be85e615
@ -52,6 +52,8 @@ func NewNatsService(param *InitNatsServiceParams) (*NatsService, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
s.registry.WatchServices()
|
s.registry.WatchServices()
|
||||||
|
// 广播服务上线
|
||||||
|
s.publishUpdateService()
|
||||||
|
|
||||||
s.nats = nat.NewNats(param.ServiceName, param.NatsAddress...)
|
s.nats = nat.NewNats(param.ServiceName, param.NatsAddress...)
|
||||||
if err = s.nats.Connect(); err != nil {
|
if err = s.nats.Connect(); err != nil {
|
||||||
@ -65,6 +67,11 @@ func NewNatsService(param *InitNatsServiceParams) (*NatsService, error) {
|
|||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *NatsService) publishUpdateService() {
|
||||||
|
jsonData, _ := json.Marshal(n.node)
|
||||||
|
_ = n.nats.Publish(updateTopic, jsonData)
|
||||||
|
}
|
||||||
|
|
||||||
func (n *NatsService) subscribeUpdateService() error {
|
func (n *NatsService) subscribeUpdateService() error {
|
||||||
return n.nats.SubscribeCb(updateTopic, func(m *nats.Msg) {
|
return n.nats.SubscribeCb(updateTopic, func(m *nats.Msg) {
|
||||||
var node = &etcd.ServiceNode{}
|
var node = &etcd.ServiceNode{}
|
||||||
@ -83,6 +90,11 @@ func (n *NatsService) subscribeUpdateService() error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 订阅回调,极少用。调用方自行保证并发性问题
|
||||||
|
func (n *NatsService) SubscribeCb(topic string, cb func(m *nats.Msg)) error {
|
||||||
|
return n.nats.SubscribeCb(topic, cb)
|
||||||
|
}
|
||||||
|
|
||||||
func (n *NatsService) Subscribe(topic string) error {
|
func (n *NatsService) Subscribe(topic string) error {
|
||||||
return n.nats.Subscribe(topic, n.msg)
|
return n.nats.Subscribe(topic, n.msg)
|
||||||
}
|
}
|
||||||
|
@ -83,3 +83,7 @@ func (s *WsServer) FindConnByUserId(userId int64) (IConn, bool) {
|
|||||||
func (s *WsServer) FindConnByConnId(connId uint32) (IConn, bool) {
|
func (s *WsServer) FindConnByConnId(connId uint32) (IConn, bool) {
|
||||||
return wsMgr.Get(connId)
|
return wsMgr.Get(connId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *WsServer) Rang(cb func(conn IConn) bool) {
|
||||||
|
wsMgr.Rang(cb)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user