爲什麼要殺死一個簡單的「ClientList.Remove(entry)」來自其他客戶端的所有連接?從列表中刪除客戶端殺死其他連接
我有一個非常簡單的Go TCP服務器,可以進行連接操作和登錄操作。 之後,如果創建一個客戶端並與TCP客戶端啓動一個GO例程。
newClient:= &客戶{ 「」, 「」,login.LoginToken,康涅狄格州} 去ClientReader(newClient) ClientList.PushBack(* newClient)
的圍棋程序讀取所有輸入數據。 並且當連接有超時或網絡更改(客戶端獲得新IP) 它從客戶端列表中刪除客戶端。
但是當它從列表中刪除客戶端....所有其他客戶端連接都已死亡? 在循環中找到合適的客戶端並將其刪除。
看看removeloop:
常規:
func ClientReader(client *Client) {
buffer := make([]byte, 2048)
for {
bytesRead, error := client.Conn.Read(buffer)
if error != nil {
Log(error)
break
}
var m Message
err := json.Unmarshal([]byte(buffer[0:bytesRead]), &m)
if err != nil {
Log(err)
} else {
switch m.Cmd {
case "Message":
case "Ping":
Log("Ping from: ", client.Name, " on ", client.Conn.RemoteAddr())
client.Conn.SetDeadline(time.Now().Add(25 * time.Second))
pong := []byte(`{"PONG":"..."}` + "\r\n")
client.Conn.Write(pong)
Log("PONG: " + time.Now().Format(time.RFC850))
Log("User Online: " + strconv.Itoa(ClientList.Len()))
Log("Goroutines: " + strconv.Itoa(runtime.NumGoroutine()))
default:
Log("Not supported Command: ", m.Cmd)
clienterror := []byte(`{"Err":"Command not supported"}` + "\r\n")
client.Conn.Write(clienterror)
}
for i := 0; i < 2048; i++ {
buffer[i] = 0x00
}
}
}
RemoveLoop:
for entry := ClientList.Front(); entry != nil; entry = entry.Next() {
listclient := entry.Value.(Client)
if client.Conn.RemoteAddr() == listclient.Conn.RemoteAddr() {
ClientList.Remove(entry)
Log("## SEARCH: ", client.Name, client.Conn.RemoteAddr())
Log("## FOUND: ", listclient.Name,listclient.Conn.RemoteAddr())
Log("## REMOVED: ", entry.Value)
break RemoveLoop
}
}
Log("Exit Client Reader Routine ", client.Name, " on ", client.Conn.RemoteAddr())
}
感謝您的快速回答:) – user3033143