async static Task <IPacket> Receive(IPacket packet) { if (string.Equals(packet.Protocol, "0")) { PacketJson json = packet as PacketJson; Dictionary <string, string> dic = JsonConvert.DeserializeObject <Dictionary <string, string> >(json.Json); string oldStageID = dic["OldStageID"]; string newStageID = dic["NewStageID"]; string userID = dic["UserID"]; Console.WriteLine($"[{json.Protocol}] oldStageID: {oldStageID}, newStageID: {newStageID}, userID: {userID}"); await listener.MoveStageAsync(oldStageID : oldStageID, newStageID : newStageID, userID : userID); return(null); } else if (string.Equals(packet.Protocol, "-1")) { PacketJson json = packet as PacketJson; Dictionary <string, string> dic = JsonConvert.DeserializeObject <Dictionary <string, string> >(json.Json); string stageID = dic["StageID"]; string userID = dic["UserID"]; Console.WriteLine($"[{json.Protocol}] StageID: {stageID}, userID: {userID}"); await listener.DisconnectAsync(stageID : stageID, userID : userID); return(null); } else { PacketValue receive = packet as PacketValue; Console.WriteLine($"[{receive.Protocol}] {receive.Value}"); return(new PacketValue(protocol: receive.Protocol, value: receive.Value)); } }
static void Main(string[] args) { string ip = ClientIP; string msg; PacketValue receivePacket, sendPacket; PacketJson sendPacketJson; int stageID = 0; Task task1 = Task.Run(async() => { TcpClientConcurrencyAsync tcpClinet = new TcpClientConcurrencyAsync(serverIP: SERVER_IP, serverPort: SERVER_PORT, callback: async(IPacket packet) => { receivePacket = packet as PacketValue; Console.WriteLine($"[receive] {receivePacket.Protocol}: {receivePacket.Value}"); }); await tcpClinet.StartAsync(stageID: stageID.ToString(), userID: ip); Dictionary <string, string> dic; while (true) { msg = Console.ReadLine(); if (string.Equals(msg, "0")) { dic = new Dictionary <string, string>(); dic.Add("OldStageID", stageID++.ToString()); dic.Add("NewStageID", stageID.ToString()); dic.Add("UserID", ip); sendPacketJson = new PacketJson(protocol: msg, json: JsonConvert.SerializeObject(dic)); await tcpClinet.SendAsync(sendPacketJson); } else if (string.Equals(msg, "-1")) { dic = new Dictionary <string, string>(); dic.Add("StageID", stageID.ToString()); dic.Add("UserID", ip); sendPacketJson = new PacketJson(protocol: msg, json: JsonConvert.SerializeObject(dic)); await tcpClinet.SendAsync(sendPacketJson); } else { sendPacket = new PacketValue(protocol: ip, value: msg); await tcpClinet.SendAsync(sendPacket); } } }); task1.Wait(); //try //{ // TcpClientConcurrencySync tcpClinet = new TcpClientConcurrencySync(serverIP: SERVER_IP, serverPort: SERVER_PORT, callback: (IPacket packet) => // { // receivePacket = packet as PacketValue; // Console.WriteLine($"[receive] {receivePacket.Protocol}: {receivePacket.Value}"); // }); // tcpClinet.Start(stageID: stageID.ToString(), userID: ip); // Dictionary<string, string> dic; // while (true) // { // msg = Console.ReadLine(); // if (string.Equals(msg, "0")) // { // dic = new Dictionary<string, string>(); // dic.Add("OldStageID", stageID++.ToString()); // dic.Add("NewStageID", stageID.ToString()); // dic.Add("UserID", ip); // sendPacketJson = new PacketJson(protocol: msg, json: JsonConvert.SerializeObject(dic)); // tcpClinet.Send(sendPacketJson); // } // else if (string.Equals(msg, "-1")) // { // dic = new Dictionary<string, string>(); // dic.Add("StageID", stageID.ToString()); // dic.Add("UserID", ip); // sendPacketJson = new PacketJson(protocol: msg, json: JsonConvert.SerializeObject(dic)); // tcpClinet.Send(sendPacketJson); // } // else // { // sendPacket = new PacketValue(protocol: ip, value: msg); // tcpClinet.Send(sendPacket); // } // } //} //catch (Exception ex) //{ //} }