Browse Source

change chat bug

master
godo 5 months ago
parent
commit
1a6d6e9121
  1. 12
      godo/localchat/addr.go

12
godo/localchat/addr.go

@ -34,6 +34,8 @@ import (
"godo/store" "godo/store"
) )
var onlineUsersMutex sync.Mutex
// 发送 UDP 包并忽略响应 // 发送 UDP 包并忽略响应
func sendUDPPacket(ip string) error { func sendUDPPacket(ip string) error {
hostname, err := os.Hostname() hostname, err := os.Hostname()
@ -87,7 +89,7 @@ func concurrentGetIpInfo(ips []string) {
semaphore := make(chan struct{}, maxConcurrency) semaphore := make(chan struct{}, maxConcurrency)
failedIPs := make(map[string]bool) failedIPs := make(map[string]bool)
failedIPsMutex := sync.Mutex{}
for _, ip := range ips { for _, ip := range ips {
if containArr(hostips, ip) || failedIPs[ip] || !containArr(validIPs, ip) { if containArr(hostips, ip) || failedIPs[ip] || !containArr(validIPs, ip) {
continue continue
@ -102,7 +104,9 @@ func concurrentGetIpInfo(ips []string) {
err := sendUDPPacket(ip) err := sendUDPPacket(ip)
if err != nil { if err != nil {
log.Printf("Failed to send packet to IP %s: %v", ip, err) log.Printf("Failed to send packet to IP %s: %v", ip, err)
failedIPs[ip] = true // 标记失败的 IP failedIPsMutex.Lock() // 加锁
failedIPs[ip] = true // 标记失败的 IP
failedIPsMutex.Unlock() // 解锁
} }
}(ip) }(ip)
} }
@ -122,6 +126,8 @@ func CheckOnline() {
} }
func CleanOnlineUsers() { func CleanOnlineUsers() {
onlineUsersMutex.Lock()
defer onlineUsersMutex.Unlock()
OnlineUsers = make(map[string]UserStatus) OnlineUsers = make(map[string]UserStatus)
} }
@ -135,6 +141,8 @@ func containArr(s []string, str string) bool {
} }
func UpdateUserStatus(ip string, hostname string) UserStatus { func UpdateUserStatus(ip string, hostname string) UserStatus {
onlineUsersMutex.Lock()
defer onlineUsersMutex.Unlock()
OnlineUsers[ip] = UserStatus{ OnlineUsers[ip] = UserStatus{
Hostname: hostname, Hostname: hostname,
IP: ip, IP: ip,

Loading…
Cancel
Save