From 5b5e541cc00bd28a829286e611c36052ee440e64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Wed, 1 Sep 2021 21:11:07 +0200 Subject: [PATCH] websocket peer add event. --- internal/types/websocket.go | 2 +- internal/websocket/manager.go | 5 +---- internal/websocket/peer.go | 32 ++++++++++++++++++++------------ 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/internal/types/websocket.go b/internal/types/websocket.go index 2eea24aa..6bdf0a3c 100644 --- a/internal/types/websocket.go +++ b/internal/types/websocket.go @@ -15,7 +15,7 @@ type WebSocketHandler func(Session, WebSocketMessage) bool type CheckOrigin func(r *http.Request) bool type WebSocketPeer interface { - Send(v interface{}) error + Send(event string, payload interface{}) Destroy() } diff --git a/internal/websocket/manager.go b/internal/websocket/manager.go index 6fddf0cf..5686e2de 100644 --- a/internal/websocket/manager.go +++ b/internal/websocket/manager.go @@ -307,13 +307,10 @@ func (manager *WebSocketManagerCtx) handle(connection *websocket.Conn, session t case raw := <-bytes: data := types.WebSocketMessage{} if err := json.Unmarshal(raw, &data); err != nil { - logger.Error().Err(err).Msg("message parsing has failed") + logger.Error().Err(err).Msg("message unmarshalling has failed") break } - // TODO: Switch to payload based messages. - data.Payload = raw - logger.Debug(). Str("address", connection.RemoteAddr().String()). Str("event", data.Event). diff --git a/internal/websocket/peer.go b/internal/websocket/peer.go index 7c6f5517..c182b5d8 100644 --- a/internal/websocket/peer.go +++ b/internal/websocket/peer.go @@ -19,25 +19,35 @@ type WebSocketPeerCtx struct { connection *websocket.Conn } -func (peer *WebSocketPeerCtx) Send(v interface{}) error { +func (peer *WebSocketPeerCtx) Send(event string, payload interface{}) { peer.mu.Lock() defer peer.mu.Unlock() if peer.connection == nil { - return nil + return } - raw, err := json.Marshal(v) + raw, err := json.Marshal(payload) if err != nil { - return err + peer.logger.Error().Err(err).Str("event", event).Msg("message marshalling has failed") + return + } + + err = peer.connection.WriteJSON(types.WebSocketMessage{ + Event: event, + Payload: raw, + }) + + if err != nil { + peer.logger.Error().Err(err).Str("event", event).Msg("send message error") + return } peer.logger.Debug(). Str("address", peer.connection.RemoteAddr().String()). - Str("raw", string(raw)). + Str("event", event). + Str("payload", string(raw)). Msg("sending message to client") - - return peer.connection.WriteMessage(websocket.TextMessage, raw) } func (peer *WebSocketPeerCtx) Destroy() { @@ -48,13 +58,11 @@ func (peer *WebSocketPeerCtx) Destroy() { return } - if err := peer.Send( + peer.Send( + event.SYSTEM_DISCONNECT, message.SystemDisconnect{ - Event: event.SYSTEM_DISCONNECT, Message: "connection destroyed", - }); err != nil { - peer.logger.Warn().Err(err).Msg("failed to send disconnect event") - } + }) if err := peer.connection.Close(); err != nil { peer.logger.Warn().Err(err).Msg("peer connection destroyed with an error")