# UnityTcpSocketSimple
**Repository Path**: therlessboy/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**: 2020-02-18
- **Last Updated**: 2021-08-18
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# UnityTcpSocketSimple
#### 介绍
unity3D简单的TcpSocket, 包含服务端和客户端, 用于unity双端通讯测试, 刚学习编程没多久, 希望看到的朋友本改进代码
#### 使用说明
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();
}
}
#### 参与贡献
1. Fork 本仓库
2. 新建 Feat_xxx 分支
3. 提交代码
4. 新建 Pull Request
#### 码云特技
1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md
2. 码云官方博客 [blog.gitee.com](https://blog.gitee.com)
3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解码云上的优秀开源项目
4. [GVP](https://gitee.com/gvp) 全称是码云最有价值开源项目,是码云综合评定出的优秀开源项目
5. 码云官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help)
6. 码云封面人物是一档用来展示码云会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)