From f13d72507c226cf78dc199d77440162c83ce5b89 Mon Sep 17 00:00:00 2001 From: godo Date: Fri, 6 Sep 2024 08:02:32 +0800 Subject: [PATCH] change chat --- .../src/components/localchat/ChatDomain.vue | 2 +- frontend/src/stores/localchat.ts | 25 ++++++++++++++----- godo/localchat/file.go | 7 +++++- godo/localchat/send.go | 10 +++++++- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/frontend/src/components/localchat/ChatDomain.vue b/frontend/src/components/localchat/ChatDomain.vue index 0cf05ef..6c71f0a 100644 --- a/frontend/src/components/localchat/ChatDomain.vue +++ b/frontend/src/components/localchat/ChatDomain.vue @@ -20,7 +20,7 @@ - {{ msg.reciperInfo.hostname }} + {{ msg.reciperInfo ? msg.reciperInfo.hostname : '' }} {{ msg.content }} diff --git a/frontend/src/stores/localchat.ts b/frontend/src/stores/localchat.ts index fb4385d..11cf7a3 100644 --- a/frontend/src/stores/localchat.ts +++ b/frontend/src/stores/localchat.ts @@ -28,16 +28,25 @@ export const useLocalChatStore = defineStore('localChatStore', () => { const chatTargetIp = ref("") const showAddUser = ref(false) const handlerMessage = (data : any) => { - //console.log(data) + console.log(data) if(data.onlines && data.onlines.length > 0){ setUserList(data.onlines); } if(data.messages){ - for(let i = 0; i < data.messages.length; i++){ - const msg = data.messages[i]; - if(msg.type == 'text'){ - addText(msg); - } + for(let ip in data.messages){ + const msgList:any = data.messages[ip].messages + if(!msgList || msgList.length < 1)return; + msgList.forEach((msg: any) => { + //console.log(msg) + if (msg.type === "text") { + msg.message = msg.message.replaceAll("\\n", "\n") + console.log(msg) + addText(msg) + } + //console.log(msg) + //console.log(msg.content) + //console.log(msg.content.length) + }) } } } @@ -316,6 +325,10 @@ export const useLocalChatStore = defineStore('localChatStore', () => { targetId: targetUser.id, targetIp: targetUser.ip, content: data.message, + reciperInfo:{ + hostname: data.hostname, + username: data.hostname + }, createdAt: Date.now(), isMe: false, isRead: false, diff --git a/godo/localchat/file.go b/godo/localchat/file.go index ca5a5d3..7349733 100644 --- a/godo/localchat/file.go +++ b/godo/localchat/file.go @@ -26,7 +26,6 @@ type FileChunk struct { } func HandlerFile(w http.ResponseWriter, r *http.Request) { - // 初始化多播地址 var msg UdpMessage decoder := json.NewDecoder(r.Body) if err := decoder.Decode(&msg); err != nil { @@ -36,6 +35,12 @@ func HandlerFile(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() toIp := msg.IP msg.Type = "file" + hostname, err := os.Hostname() + if err != nil { + libs.ErrorMsg(w, "HandleMessage error") + return + } + msg.Hostname = hostname basePath, err := libs.GetOsDir() if err != nil { libs.HTTPError(w, http.StatusInternalServerError, err.Error()) diff --git a/godo/localchat/send.go b/godo/localchat/send.go index 906c83d..520373a 100644 --- a/godo/localchat/send.go +++ b/godo/localchat/send.go @@ -3,9 +3,11 @@ package localchat import ( "encoding/json" "fmt" + "godo/libs" "log" "net" "net/http" + "os" ) // HandleMessage 处理 HTTP 请求 @@ -17,7 +19,13 @@ func HandleMessage(w http.ResponseWriter, r *http.Request) { return } defer r.Body.Close() - err := SendToIP(msg) + hostname, err := os.Hostname() + if err != nil { + libs.ErrorMsg(w, "HandleMessage error") + return + } + msg.Hostname = hostname + err = SendToIP(msg) if err != nil { http.Error(w, "Failed to send message", http.StatusInternalServerError) return