changed getting web client func

master
Alex 2021-12-29 14:14:38 +01:00
parent eaa8d0b142
commit a90e1245d1
1 changed files with 28 additions and 19 deletions

View File

@ -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"