diff --git a/godo/localchat/addr.go b/godo/localchat/addr.go index 54cc31f..530f13c 100644 --- a/godo/localchat/addr.go +++ b/godo/localchat/addr.go @@ -5,7 +5,7 @@ import ( "fmt" "log" "net" - "net/http" + "os" "runtime" "sync" "time" @@ -19,29 +19,16 @@ type UserStatus struct { Time time.Time `json:"time"` } -var OnlineUsers = make(map[string]UserStatus) - -type UDPPayload struct { - Action string `json:"action"` - Data string `json:"data"` -} - -func getHostname(ip string) (string, error) { - hostname, err := net.LookupAddr(ip) - if err != nil { - return "", fmt.Errorf("error getting hostname: %v", err) - } - if len(hostname) > 0 { - return hostname[0], nil - } - return "", fmt.Errorf("no hostname found for IP: %s", ip) -} - // 发送 UDP 包并忽略响应 func sendUDPPacket(ip string) error { - payload := UDPPayload{ - Action: "check", - Data: "", + hostname, err := os.Hostname() + if err != nil { + return fmt.Errorf("error getting hostname: %v", err) + } + payload := UdpMessage{ + Type: "heartbeat", + Hostname: hostname, + Time: time.Now(), } log.Printf("sending ip: %+v", ip) payloadBytes, err := json.Marshal(payload) @@ -96,17 +83,6 @@ func concurrentGetIpInfo(ips []string) { if err != nil { log.Printf("Failed to send packet to IP %s: %v", ip, err) failedIPs[ip] = true // 标记失败的 IP - } else { - hostname, err := getHostname(ip) - if err != nil { - log.Printf("Failed to get hostname for IP %s: %v", ip, err) - } else { - OnlineUsers[ip] = UserStatus{ - Hostname: hostname, - IP: ip, - Time: time.Now(), - } - } } }(ip) } @@ -138,17 +114,6 @@ func containArr(s []string, str string) bool { return false } -func HandleHeartbeat(w http.ResponseWriter, r *http.Request) { - ip := r.RemoteAddr // 可以根据实际情况获取 IP - hostname, err := getHostname(ip) - if err != nil { - libs.HTTPError(w, http.StatusInternalServerError, "Failed to get hostname") - return - } - userStatus := UpdateUserStatus(ip, hostname) - libs.SuccessMsg(w, userStatus, "Heartbeat received") -} - func UpdateUserStatus(ip string, hostname string) UserStatus { OnlineUsers[ip] = UserStatus{ Hostname: hostname, diff --git a/godo/localchat/file.go b/godo/localchat/file.go index 9f22558..2b811f8 100644 --- a/godo/localchat/file.go +++ b/godo/localchat/file.go @@ -153,7 +153,7 @@ func SendFile(file *os.File, numChunks int, toIp string, message UdpMessage) { fmt.Printf("发送文件块 %d 到 %s 成功\n", i, toIp) } } -func RecieveFile(msg UdpMessage) { +func ReceiveFile(msg UdpMessage) { chunk := msg.Message.(FileChunk) // 验证校验和 diff --git a/godo/localchat/server.go b/godo/localchat/server.go index 46a837b..4ad3c2b 100644 --- a/godo/localchat/server.go +++ b/godo/localchat/server.go @@ -22,6 +22,8 @@ type UserMessage struct { Onlines map[string]UserStatus `json:"onlines"` } +var OnlineUsers = make(map[string]UserStatus) + var UserMessages = make(map[string]*Messages) func init() { @@ -49,7 +51,7 @@ func UdpServer() { log.Println("UDP server started on :56780") - // 无限循环,监听 UDP 请求 + // 监听 UDP 请求 for { buffer := make([]byte, 1024) @@ -67,16 +69,26 @@ func UdpServer() { log.Printf("error unmarshalling UDP message: %v", err) continue } - if udpMsg.Type == "heartbeat" { - UpdateUserStatus(udpMsg.IP, udpMsg.Hostname) - continue - } - if udpMsg.Type == "file" { - RecieveFile(udpMsg) - continue + // 从 remoteAddr 获取 IP 地址 + if udpAddr, ok := remoteAddr.(*net.UDPAddr); ok { + ip := udpAddr.IP.String() + udpMsg.IP = ip + + if udpMsg.Type == "heartbeat" { + UpdateUserStatus(udpMsg.IP, udpMsg.Hostname) + continue + } + + if udpMsg.Type == "file" { + ReceiveFile(udpMsg) + continue + } + + // 添加消息到 UserMessages + AddMessage(udpMsg) + } else { + log.Printf("unexpected address type: %T", remoteAddr) } - // 添加消息到 UserMessages - AddMessage(udpMsg) } } func ClearAllUserMessages() {