为什么要杀死来自其他客户端的简单“ClientList.Remove(entry)”所有连接?我有一个非常简单的 Go TCP Server,可以进行连接处理和登录处理。之后,如果创建一个客户端并使用 TCP 客户端启动一个 GO 程序。newClient := &Client{"", "", login.LoginToken, conn} go ClientReader(newClient) ClientList.PushBack(*newClient)Go 例程读取所有传入的数据。当连接超时或网络更改(客户端获得新 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())}
2 回答
- 2 回答
- 0 关注
- 183 浏览
添加回答
举报
0/150
提交
取消