diff --git a/frontend/src/stores/localchat.ts b/frontend/src/stores/localchat.ts index 11cf7a3..913d51d 100644 --- a/frontend/src/stores/localchat.ts +++ b/frontend/src/stores/localchat.ts @@ -29,8 +29,15 @@ export const useLocalChatStore = defineStore('localChatStore', () => { const showAddUser = ref(false) const handlerMessage = (data : any) => { console.log(data) - if(data.onlines && data.onlines.length > 0){ - setUserList(data.onlines); + if(data.onlines){ + const ips = [] + for(let ip in data.onlines){ + const info = data.onlines[ip] + if (info.ip && isValidIP(info.ip)) { + ips.push(info) + } + } + setUserList(ips); } if(data.messages){ for(let ip in data.messages){ diff --git a/godo/cmd/main.go b/godo/cmd/main.go index c65dc5b..a08c957 100644 --- a/godo/cmd/main.go +++ b/godo/cmd/main.go @@ -80,9 +80,8 @@ func OsStart() { localchatRouter := router.PathPrefix("/localchat").Subrouter() localchatRouter.HandleFunc("/message", localchat.HandleMessage).Methods(http.MethodPost) - //localchatRouter.HandleFunc("/file", localchat.HandlerFile).Methods(http.MethodPost) - localchatRouter.HandleFunc("/udpaddr", localchat.HandleAddr).Methods(http.MethodGet) - localchatRouter.HandleFunc("/check", localchat.HandleCheck).Methods(http.MethodGet) + localchatRouter.HandleFunc("/file", localchat.HandlerFile).Methods(http.MethodPost) + localchatRouter.HandleFunc("/setting", localchat.HandleAddr).Methods(http.MethodPost) // 注册 WebDAV 路由 webdavRouter := router.PathPrefix("/webdav").Subrouter() webdavRouter.HandleFunc("/read", webdav.HandleReadDir).Methods(http.MethodGet) diff --git a/godo/libs/chatip.go b/godo/libs/chatip.go index 94f9462..b056a61 100644 --- a/godo/libs/chatip.go +++ b/godo/libs/chatip.go @@ -6,6 +6,7 @@ import ( ) type UserChatIpSetting struct { + CheckTime int `json:"CheckTime"` First string `json:"First"` Second string `json:"Second"` ThirdStart string `json:"ThirdStart"` @@ -16,6 +17,7 @@ type UserChatIpSetting struct { func GetDefaultChatIpSetting() UserChatIpSetting { return UserChatIpSetting{ + CheckTime: 60, First: "192", Second: "168", ThirdStart: "1", diff --git a/godo/localchat/addr.go b/godo/localchat/addr.go index 530f13c..9871086 100644 --- a/godo/localchat/addr.go +++ b/godo/localchat/addr.go @@ -13,12 +13,6 @@ import ( "godo/libs" ) -type UserStatus struct { - Hostname string `json:"hostname"` - IP string `json:"ip"` - Time time.Time `json:"time"` -} - // 发送 UDP 包并忽略响应 func sendUDPPacket(ip string) error { hostname, err := os.Hostname() diff --git a/godo/localchat/check.go b/godo/localchat/check.go index d4fdec1..b7443cb 100644 --- a/godo/localchat/check.go +++ b/godo/localchat/check.go @@ -23,5 +23,6 @@ func HandleAddr(w http.ResponseWriter, r *http.Request) { return } libs.SetConfigByName("ChatIpSetting", ipStr) + go CheckOnline() libs.SuccessMsg(w, nil, "success") } diff --git a/godo/localchat/server.go b/godo/localchat/server.go index 4ad3c2b..0475546 100644 --- a/godo/localchat/server.go +++ b/godo/localchat/server.go @@ -2,6 +2,7 @@ package localchat import ( "encoding/json" + "godo/libs" "log" "net" "time" @@ -14,25 +15,31 @@ type UdpMessage struct { IP string `json:"ip"` Message any `json:"message"` } -type Messages struct { - Messages []UdpMessage `json:"messages"` -} + type UserMessage struct { - Messages map[string]*Messages `json:"messages"` - Onlines map[string]UserStatus `json:"onlines"` + Messages map[string][]UdpMessage `json:"messages"` + Onlines map[string]UserStatus `json:"onlines"` +} +type UserStatus struct { + Hostname string `json:"hostname"` + IP string `json:"ip"` + Time time.Time `json:"time"` } var OnlineUsers = make(map[string]UserStatus) - -var UserMessages = make(map[string]*Messages) +var UserMessages = make(map[string][]UdpMessage) func init() { go UdpServer() go CheckOnlines() } + func CheckOnlines() { CheckOnline() - ticker := time.NewTicker(60 * time.Second) + chatIpSetting := libs.GetChatIpSetting() + checkTimeDuration := time.Duration(chatIpSetting.CheckTime) * time.Second + + ticker := time.NewTicker(checkTimeDuration) defer ticker.Stop() for range ticker.C { // 检查客户端是否已断开连接 @@ -91,21 +98,25 @@ func UdpServer() { } } } + func ClearAllUserMessages() { - for ip, msg := range UserMessages { - msg.Messages = []UdpMessage{} // 清空切片 - UserMessages[ip] = msg // 更新映射中的值 - } + UserMessages = make(map[string][]UdpMessage) } + func GetMessages() UserMessage { return UserMessage{ Messages: UserMessages, Onlines: OnlineUsers, } } + func AddMessage(msg UdpMessage) { + // 检查 UserMessages 中是否已经有这个 IP 地址的消息列表 if _, ok := UserMessages[msg.IP]; !ok { - UserMessages[msg.IP] = &Messages{} + // 如果没有,则创建一个新的消息列表 + UserMessages[msg.IP] = []UdpMessage{} } - UserMessages[msg.IP].Messages = append(UserMessages[msg.IP].Messages, msg) + + // 将新消息添加到对应 IP 地址的消息列表中 + UserMessages[msg.IP] = append(UserMessages[msg.IP], msg) }