為什么要?dú)⑺纴碜云渌蛻舳说暮?jiǎn)單“ClientList.Remove(entry)”所有連接?我有一個(gè)非常簡(jiǎn)單的 Go TCP Server,可以進(jìn)行連接處理和登錄處理。之后,如果創(chuàng)建一個(gè)客戶端并使用 TCP 客戶端啟動(dòng)一個(gè) GO 程序。newClient := &Client{"", "", login.LoginToken, conn} go ClientReader(newClient) ClientList.PushBack(*newClient)Go 例程讀取所有傳入的數(shù)據(jù)。當(dāng)連接超時(shí)或網(wǎng)絡(luò)更改(客戶端獲得新 IP)時(shí),它會(huì)從客戶端列表中刪除客戶端。但是當(dāng)它從列表中刪除客戶端時(shí)......所有其他客戶端連接都死了?在循環(huán)中它找到正確的客戶端并將其刪除??纯磖emoveloop:常規(guī):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())}
- 2 回答
- 0 關(guān)注
- 196 瀏覽
添加回答
舉報(bào)
0/150
提交
取消