From 8e13a6067741dd24676aa178dac6a28580742564 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Thu, 28 Jan 2021 15:56:50 +0100 Subject: [PATCH] send unicast & broadcast. --- internal/types/event/events.go | 5 ++++ internal/types/message/messages.go | 19 +++++++++++++++ internal/websocket/handler/handler.go | 12 ++++++++++ internal/websocket/handler/send.go | 33 +++++++++++++++++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 internal/websocket/handler/send.go diff --git a/internal/types/event/events.go b/internal/types/event/events.go index 36d48ede..5541571f 100644 --- a/internal/types/event/events.go +++ b/internal/types/event/events.go @@ -52,6 +52,11 @@ const ( BORADCAST_STATUS = "broadcast/status" ) +const ( + SEND_UNICAST = "send/unicast" + SEND_BROADCAST = "send/broadcast" +) + const ( FILE_CHOOSER_DIALOG_OPENED = "file_chooser_dialog/opened" FILE_CHOOSER_DIALOG_CLOSED = "file_chooser_dialog/closed" diff --git a/internal/types/message/messages.go b/internal/types/message/messages.go index fe31194c..01c20623 100644 --- a/internal/types/message/messages.go +++ b/internal/types/message/messages.go @@ -167,3 +167,22 @@ type BroadcastStatus struct { IsActive bool `json:"is_active"` URL string `json:"url,omitempty"` } + +///////////////////////////// +// Send (opaque comunication channel) +///////////////////////////// + +type SendUnicast struct { + Event string `json:"event,omitempty"` + Sender string `json:"sender"` + Receiver string `json:"receiver"` + Subject string `json:"subject"` + Body string `json:"body"` +} + +type SendBroadcast struct { + Event string `json:"event,omitempty"` + Sender string `json:"sender"` + Subject string `json:"subject"` + Body string `json:"body"` +} diff --git a/internal/websocket/handler/handler.go b/internal/websocket/handler/handler.go index 22e109bb..1601a42b 100644 --- a/internal/websocket/handler/handler.go +++ b/internal/websocket/handler/handler.go @@ -86,6 +86,18 @@ func (h *MessageHandlerCtx) Message(session types.Session, raw []byte) bool { err = utils.Unmarshal(payload, raw, func() error { return h.keyboardModifiers(session, payload) }) + + // Send Events + case event.SEND_UNICAST: + payload := &message.SendUnicast{} + err = utils.Unmarshal(payload, raw, func() error { + return h.sendUnicast(session, payload) + }) + case event.SEND_BROADCAST: + payload := &message.SendBroadcast{} + err = utils.Unmarshal(payload, raw, func() error { + return h.sendBroadcast(session, payload) + }) default: return false } diff --git a/internal/websocket/handler/send.go b/internal/websocket/handler/send.go new file mode 100644 index 00000000..ba757fb8 --- /dev/null +++ b/internal/websocket/handler/send.go @@ -0,0 +1,33 @@ +package handler + +import ( + "demodesk/neko/internal/types" + "demodesk/neko/internal/types/message" + "demodesk/neko/internal/types/event" +) + +func (h *MessageHandlerCtx) sendUnicast(session types.Session, payload *message.SendUnicast) error { + receiver, ok := h.sessions.Get(payload.Receiver) + if !ok { + h.logger.Debug().Str("id", session.ID()).Msg("receiver ID not found") + return nil + } + + return receiver.Send(message.SendUnicast{ + Event: event.SEND_UNICAST, + Sender: session.ID(), + Receiver: receiver.ID(), + Subject: payload.Subject, + Body: payload.Body, + }) +} + +func (h *MessageHandlerCtx) sendBroadcast(session types.Session, payload *message.SendBroadcast) error { + h.sessions.Broadcast(message.SendBroadcast{ + Event: event.SEND_BROADCAST, + Sender: session.ID(), + Subject: payload.Subject, + Body: payload.Body, + }, []string{ session.ID() }) + return nil +}