From a5299cdc0ae3b9f2ea61105f18d5d05079dc6a07 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 19 Aug 2021 16:17:00 +0200 Subject: [PATCH 1/2] broadcast messages via event bus --- server/internal/events/events.go | 1 + server/internal/ws/client.go | 5 +++-- server/internal/ws/server.go | 11 ++++++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/server/internal/events/events.go b/server/internal/events/events.go index d389fdb..62c05a7 100644 --- a/server/internal/events/events.go +++ b/server/internal/events/events.go @@ -24,6 +24,7 @@ func Unsubscribe(topic string, handler interface{}) error { func SubscribeAsync(topic string, fn interface{}, transactional bool) error { return bus.SubscribeAsync(topic, fn, transactional) } + func WaitAsync() { bus.WaitAsync() } diff --git a/server/internal/ws/client.go b/server/internal/ws/client.go index 8536313..fd3f070 100644 --- a/server/internal/ws/client.go +++ b/server/internal/ws/client.go @@ -11,6 +11,7 @@ import ( "go.uber.org/zap" + "github.com/ptr-geeks/ptrun/server/internal/events" "github.com/ptr-geeks/ptrun/server/internal/messages" ) @@ -94,13 +95,13 @@ func (c *clientImpl) ReadPump() { c.logger.Debugw("exiting client read pump", "id", c.id, "remoteAddr", c.addr) break } - + //tu je ta message bus // Everything seems fine, just unmarshal & forward c.logger.Debugw("recieved message", "id", c.id, "remoteAdr", c.addr) message := &messages.Message{} proto.Unmarshal(msg, message) - c.server.Broadcast(c.id, message) + events.Publish("server.broadcast", c.id, message) } } diff --git a/server/internal/ws/server.go b/server/internal/ws/server.go index 58b3b83..3290ede 100644 --- a/server/internal/ws/server.go +++ b/server/internal/ws/server.go @@ -7,6 +7,7 @@ import ( "go.uber.org/zap" "github.com/ptr-geeks/ptrun/server/internal/consts" + "github.com/ptr-geeks/ptrun/server/internal/events" "github.com/ptr-geeks/ptrun/server/internal/messages" ) @@ -58,7 +59,7 @@ func NewServer(logger *zap.Logger) Server { //Run is used for connecting and disconnetcing clients from the server func (s *serverImpl) Run() { s.logger.Debug("server started and listening for events") - + s.HandleBroadcast() for { select { case connect := <-s.connect: @@ -79,6 +80,7 @@ func (s *serverImpl) Run() { } case broadcast := <-s.broadcast: + s.logger.Debugw("Broadcasting", "id", broadcast.excludeClient) for clientID, client := range s.clients { if broadcast.excludeClient == clientID { continue @@ -116,3 +118,10 @@ func (s *serverImpl) Disconnect(client Client) { func (s *serverImpl) Broadcast(excludeClient int32, msg *messages.Message) { s.broadcast <- broadcastMessage{msg: msg, excludeClient: excludeClient} } + +func (s *serverImpl) HandleBroadcast() { + err := events.Subscribe("server.broadcast", s.Broadcast) + if err != nil { + s.logger.Warnw("cannot read from the client") + } +} From 36e26830d0050b456c38af941ec110fa092ca9b7 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 19 Aug 2021 16:35:13 +0200 Subject: [PATCH 2/2] handleBroadcast is private --- server/internal/ws/server.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/internal/ws/server.go b/server/internal/ws/server.go index 3290ede..dded0b3 100644 --- a/server/internal/ws/server.go +++ b/server/internal/ws/server.go @@ -59,7 +59,7 @@ func NewServer(logger *zap.Logger) Server { //Run is used for connecting and disconnetcing clients from the server func (s *serverImpl) Run() { s.logger.Debug("server started and listening for events") - s.HandleBroadcast() + s.handleBroadcast() for { select { case connect := <-s.connect: @@ -119,7 +119,7 @@ func (s *serverImpl) Broadcast(excludeClient int32, msg *messages.Message) { s.broadcast <- broadcastMessage{msg: msg, excludeClient: excludeClient} } -func (s *serverImpl) HandleBroadcast() { +func (s *serverImpl) handleBroadcast() { err := events.Subscribe("server.broadcast", s.Broadcast) if err != nil { s.logger.Warnw("cannot read from the client")