void Callback_Receive(IAsyncResult ar) { PlayerData player = (PlayerData)ar.AsyncState; try { int t = player.client.socket.EndReceive(ar); if (t == 0) { player.client.isLink = false; return; } } catch { player.client.isLink = false; return; } string data = System.Text.Encoding.UTF8.GetString(player.client.data).TrimEnd('\0'); string[] devideData; //try //{ devideData = ServerFunction.DevideMsg(data); for (int i = 0; i < devideData.Length; i++) { if (devideData[i].CompareTo("HB") != 0 && devideData[i].Substring(0, 2).CompareTo("PD") != 0) { Console.WriteLine("房间内: " + devideData[i]); } Parse(devideData[i], player); } //} //catch //{ // Console.WriteLine("Error Package"); //} //清空数组数据 Array.Clear(player.client.data, 0, player.client.data.Length); if (player.client.isLink && !player.isLeftGameRoom) { try { player.client.socket.BeginReceive(player.client.data, 0, player.client.data.Length, SocketFlags.None, new AsyncCallback(Callback_Receive), player); } catch { } } }
/// <summary> /// 接收数据回调 /// 不接收游戏中数据 /// </summary> /// <param name="ar"></param> static void Callback_Receive(IAsyncResult ar) { Client t_client = (Client)ar.AsyncState; try { int t = t_client.socket.EndReceive(ar); if (t == 0) { CloseClient(t_client); return; } } catch { CloseClient(t_client); return; } string data = System.Text.Encoding.UTF8.GetString(t_client.data).TrimEnd('\0'); string[] devideData; try { devideData = ServerFunction.DevideMsg(data); for (int i = 0; i < devideData.Length; i++) { if (devideData[i].CompareTo("HB") != 0) { Console.WriteLine(devideData[i]); } ParseClientData(t_client, devideData[i]); } } catch { Console.WriteLine("Error Package"); } //清空数组数据 Array.Clear(t_client.data, 0, t_client.data.Length); if (t_client.isLink && !t_client.isGame) { t_client.socket.BeginReceive(t_client.data, 0, t_client.data.Length, SocketFlags.None, new AsyncCallback(Callback_Receive), t_client); } }