Browse Source

change chat

master
godo 9 months ago
parent
commit
f13d72507c
  1. 2
      frontend/src/components/localchat/ChatDomain.vue
  2. 25
      frontend/src/stores/localchat.ts
  3. 7
      godo/localchat/file.go
  4. 10
      godo/localchat/send.go

2
frontend/src/components/localchat/ChatDomain.vue

@ -20,7 +20,7 @@
<el-icon :size="12">
<Place />
</el-icon>
{{ msg.reciperInfo.hostname }}
{{ msg.reciperInfo ? msg.reciperInfo.hostname : '' }}
</span>
<span class="msg">{{ msg.content }}</span>

25
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,

7
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())

10
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

Loading…
Cancel
Save