# UnityTcpSocketSimple **Repository Path**: senwj/UnityTcpSocketSimple ## Basic Information - **Project Name**: UnityTcpSocketSimple - **Description**: unity3D简单的TcpSocket, 包含服务端和客户端, 用于unity双端通讯测试, 刚学习编程没多久, 希望看到的朋友本改进代码 - **Primary Language**: C# - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 5 - **Created**: 2021-08-18 - **Last Updated**: 2021-08-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # UnityTcpSocketSimple #### Description unity3D简单的TcpSocket, 包含服务端和客户端, 用于unity双端通讯测试, 刚学习编程没多久, 希望看到的朋友本改进代码 #### Instructions Client: public class SocketClientManager : MonoBehaviour { static string IP = "127.0.0.1"; static int Port = 10010; static TcpSocketClient socketClient; static bool isConnected = false; private void Start() { socketClient = new TcpSocketClient(IP, Port); socketClient.Initialized(); socketClient.OnReceive += OnReceive; socketClient.OnConnected += OnConnected; socketClient.OnError += OnError; socketClient.OnDisconnected += OnDisconnected; socketClient.OnConnInterval += OnConnInterval; socketClient.Connected(100); } private void Update() { socketClient.ConnectedInterval(); } /// /// 连接成功; /// void OnConnected() { Debug.Log("连接成功!+"); isConnected = true; } void OnConnInterval(int count) { Debug.Log("正在尝试第 "+count + " 次连接!"); } /// /// 读取消息 /// /// void OnReceive(string msg) { Debug.Log("收到消息: "+msg); GameManager.Instence.SetText(msg); } /// /// 连接错误 /// void OnError() { Debug.Log("连接失败!+"); isConnected = false; } /// /// 断开连接 /// void OnDisconnected() { Debug.Log("断开连接!+"); isConnected = false; } public static bool OnSendToServer(string msg) { if (isConnected) { socketClient.Send(msg); } return isConnected; } void _destroy() { if (socketClient != null) socketClient.Close(); } private void OnDestroy() { _destroy(); } private void OnApplicationQuit() { _destroy(); } } Server: public class SocketServerManager : MonoBehaviour { static TcpSocketServer TcpServer; static string IP = "127.0.0.1"; static int Port = 10010; public static List ClientInfos = new List(); private void Start() { TcpServer = new TcpSocketServer(IP, Port); TcpServer.Initialized(); TcpServer.OnConnection += OnConnection; TcpServer.OnReceive += OnReceive; TcpServer.OnClientQuit += OnClientQuit; TcpServer.OnDisconnected += OnDisconnected; TcpServer.Connection(); } /// /// 监听客户端连接 /// /// void OnConnection(ClientInfo info) { string msg = "客户端 " + info.id + " 加入会话!"; Debug.Log(msg); OnSendAllMsg(msg); ClientInfos.Add(info); GameManager.Instence.AddDropdownOptionData(info.id); } /// /// 监听接收客户端消息 /// /// /// void OnReceive(ClientInfo info, string msg) { Debug.Log("接收到客户端 " + info.id + " 消息: " + msg); GameManager.Instence.textMessage= info.id + ": " + msg; } /// /// 监听客户端退出 /// /// void OnClientQuit(ClientInfo info) { Debug.Log("客户端 " + info.id + " 退出会话!"); ClientInfos.Remove(info); GameManager.Instence.RemoveDropdownOptionData(info.id); } /// /// 服务器关闭 /// void OnDisconnected() { Debug.Log("服务端关闭!"); } /// /// 群发消息 /// public static void OnSendAllMsg(string resMsg) { TcpServer.sendAllMsg(resMsg); } /// /// 发送消息给客户端 /// /// /// public static bool OnSendToClient(string idName, string resMsg) { bool state = false; for (int i = 0; i < ClientInfos.Count; i++) { ClientInfo info = ClientInfos[i]; if (idName == info.id) { Socket socket = ClientInfos[i].socket; TcpServer.Send(socket, resMsg); state = true; break; } } return state; } private void OnDestroy() { if(TcpServer != null) TcpServer.Close(); } private void OnApplicationQuit() { if(TcpServer != null) TcpServer.Close(); } } #### Contribution 1. Fork the repository 2. Create Feat_xxx branch 3. Commit your code 4. Create Pull Request #### Gitee Feature 1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md 2. Gitee blog [blog.gitee.com](https://blog.gitee.com) 3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore) 4. The most valuable open source project [GVP](https://gitee.com/gvp) 5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help) 6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)