From 5f9779469114aa89b19b2c0490451ad6abc25cf1 Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Mon, 11 Apr 2022 16:53:47 +0200 Subject: [PATCH] Send directly to local session with disconnected client. Before it was using NATS to deliver the message to the session. --- hub.go | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/hub.go b/hub.go index 706fb85..577463b 100644 --- a/hub.go +++ b/hub.go @@ -1253,7 +1253,7 @@ func (h *Hub) processMessageMsg(client *Client, message *ClientMessage) { return } - var recipient *Client + var recipient *ClientSession var subject string var clientData *MessageClientMessageData var serverRecipient *MessageClientMessageRecipient @@ -1299,15 +1299,18 @@ func (h *Hub) processMessageMsg(client *Client, message *ClientMessage) { subject = "session." + msg.Recipient.SessionId h.mu.RLock() - recipient = h.clients[data.Sid] - if recipient == nil { + sess, found := h.sessions[data.Sid] + if found { + if sess, ok := sess.(*ClientSession); ok { + recipient = sess + } + // Send to client connection for virtual sessions. - sess := h.sessions[data.Sid] - if sess != nil && sess.ClientType() == HelloClientTypeVirtual { + if sess.ClientType() == HelloClientTypeVirtual { virtualSession := sess.(*VirtualSession) clientSession := virtualSession.Session() subject = "session." + clientSession.PublicId() - recipient = clientSession.GetClient() + recipient = clientSession // The client should see his session id as recipient. serverRecipient = &MessageClientMessageRecipient{ Type: "session", @@ -1391,15 +1394,11 @@ func (h *Hub) processMessageMsg(client *Client, message *ClientMessage) { return } - if recipientSession := recipient.GetSession(); recipientSession != nil { - msg.Recipient.SessionId = session.PublicId() - // It may take some time for the publisher (which is the current - // client) to start his stream, so we must not block the active - // goroutine. - go h.processMcuMessage(session, recipientSession, message, msg, clientData) - } else { // nolint - // Client is not connected yet. - } + msg.Recipient.SessionId = session.PublicId() + // It may take some time for the publisher (which is the current + // client) to start his stream, so we must not block the active + // goroutine. + go h.processMcuMessage(session, recipient, message, msg, clientData) return } recipient.SendMessage(response)