// 客户端 using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; using UnityEngine; namespace AdamSync { public class SyncCreateRoom { public static TcpClient client; public static NetworkStream stream; public static string serverReturnValue; public static Action registRequset; public static Action getusersRequset; public static Action createRoomRequset; public static Action getRoomsRequset; public static Action getownRoomsRequset; public static Action joinRoomRequset; public static Action joinsRequset; public static Action leaveRoomRequset; public static Action closeRoomRequset; public static Action playerInfoRequset; public static Action getroomusersRequset; public static Action send2roomRequset; public static Action send2userRequset; public static async Task StartLinkTCPServer(string serverIp, int serverPort) { client = new TcpClient(); await client.ConnectAsync(serverIp, serverPort); stream = client.GetStream(); await ReciveMessage(); } public static async Task ReciveMessage() { CancellationTokenSource cts = new CancellationTokenSource(); await ReadMessagesAsync(stream, cts.Token); } public static async Task SendMessageAsync(string message) { if (stream != null) { if (!string.IsNullOrEmpty(message)) { byte[] data = Encoding.UTF8.GetBytes($"{message}"); await stream.WriteAsync(data, 0, data.Length); } } } public static async Task ReadMessagesAsync(NetworkStream stream, CancellationToken cancellationToken) { byte[] buffer = new byte[8192]; while (!cancellationToken.IsCancellationRequested) { try { int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken); if (bytesRead > 0) { string message = Encoding.UTF8.GetString(buffer, 0, bytesRead); string[] commands = message.Split(' '); if (commands.Length > 0) { //Debug.Log("commands[0]====" + commands[0]); switch (commands[0]) { case "regist": registRequset?.Invoke(commands[1]); break; case "getusers": getusersRequset?.Invoke(commands[1]); break; case "createroom": createRoomRequset?.Invoke(commands[1]); break; case "getrooms": getRoomsRequset?.Invoke(commands[1]); break; case "getownrooms": getownRoomsRequset?.Invoke(commands[1]); break; case "joinroom": joinRoomRequset?.Invoke(commands[1]); break; case "joins": joinsRequset?.Invoke(commands[1]); break; case "leaveroom": leaveRoomRequset?.Invoke(commands[1]); break; case "closeroom": closeRoomRequset?.Invoke(commands[1]); break; case "player": string[] data = commands[1].Split(';'); if (data.Length == 3) { playerInfoRequset?.Invoke(int.Parse(data[0]), data[1], data[2]); } break; case "getroomusers": getroomusersRequset?.Invoke(commands[1]); break; case "send2room": send2roomRequset?.Invoke(commands[1]); break; case "send2user": send2userRequset?.Invoke(commands[1]); break; } } } } catch (OperationCanceledException) { // Ignore cancellation exceptions } } } public static void CloseClint() { client.Close(); if (stream != null) { stream.Close(); stream.Dispose(); } } } }