From a90e1245d1a6c6e7815c44dfd1f5606a63551028 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 29 Dec 2021 14:14:38 +0100 Subject: [PATCH] changed getting web client func --- serverCommunication/web.go | 47 +++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/serverCommunication/web.go b/serverCommunication/web.go index ce2e98e..ddeec57 100644 --- a/serverCommunication/web.go +++ b/serverCommunication/web.go @@ -76,12 +76,10 @@ func HandleWebMessage(isVoice bool, conn *websocket.Conn, uuid string, status in } else { // web if isVoice { // message from voice if dest == kraProtocol.DestMobile { // forwarding message to mobile - connForDest := getConnForDest(dest, uuid) // get conn for mobile - - if connForDest != nil { // mobile is connected + if webClient.MobileConn != nil { // mobile is connected raw = kraProtocol.EncodeWebMessage(kraProtocol.StatusReply, cmdID, cmdNumber, args) - err = webClient.SendBinaryMessage(connForDest, raw) + err = webClient.SendBinaryMessage(webClient.MobileConn, raw) if err != nil { logger.WebVoice.Warnln("write:", err) @@ -111,12 +109,10 @@ func HandleWebMessage(isVoice bool, conn *websocket.Conn, uuid string, status in } } else { // message from mobile if dest == kraProtocol.DestVoice { // forwarding message to voice - connForDest := getConnForDest(dest, uuid) // get conn for voice - - if connForDest != nil { // voice connected + if webClient.VoiceConn != nil { // voice connected raw = kraProtocol.EncodeWebMessage(kraProtocol.StatusSend, cmdID, cmdNumber, args) - err = webClient.SendBinaryMessage(connForDest, raw) + err = webClient.SendBinaryMessage(webClient.VoiceConn, raw) if err != nil { logger.WebMobile.Warnln("write:", err) @@ -261,6 +257,22 @@ func getWebClientByConn(conn *websocket.Conn) *structs.WebClient { return nil } +func GetWebClientOrCreateNewByUuid(uuid string) *structs.WebClient { + webClient := GetWebClientByUuid(uuid) + + if webClient == nil { + // add to clients list + client := &structs.WebClient{Uuid: uuid} + + cache.WebClients[uuid] = client + + logger.Web.Debugln("new client") + + return client + } + return webClient +} + func GetWebClientByUuid(Uuid string) *structs.WebClient { logger.Web.Debugln("len clients", len(cache.WebClients)) @@ -271,21 +283,18 @@ func GetWebClientByUuid(Uuid string) *structs.WebClient { return client } } - - // add to clients list - client := &structs.WebClient{Uuid: Uuid} - - cache.WebClients[Uuid] = client - - logger.Web.Debugln("new client") - - return client + return nil } +// @Deprecated func IsVoiceAndMobileSocketConnected(uuid string) string { - client := GetWebClientByUuid(uuid) + webClient := GetWebClientByUuid(uuid) - if client.MobileConn == nil && client.VoiceConn == nil { + if webClient == nil { + return "0" + } + + if webClient.MobileConn == nil && webClient.VoiceConn == nil { return "0" } else { return "1"