# UnityTcpSocketSimple
**Repository Path**: couday/UnityTcpSocketSimple
## Basic Information
- **Project Name**: UnityTcpSocketSimple
- **Description**: unity3D Tcp框架, 基于Telepathy , 包含服务端和客户端, 用于unity内端通讯,高效稳定.
- **Primary Language**: C#
- **License**: MIT
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 12
- **Forks**: 5
- **Created**: 2020-01-06
- **Last Updated**: 2025-07-16
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# UnityTcpSocketSimple
#### 介绍
unity3D Tcp框架, 基于Telepathy , 包含服务端和客户端, 用于unity内端通讯,高效稳定.
unity3D Udp框架, 基于kcp2k, 包含服务端和客户端, 用于unity内端通讯,高效稳定(UDP没做分片处理, 消息长度有限制).
Telepathy 和 kcp2k 是unity Mirror联机插件里面的应用的
Telepathy https://github.com/vis2k/Telepathy
kcp2k https://github.com/vis2k/kcp2k
#### 使用说明
1. unity中 新建物体 => 添加组件 clientManager.cs
2. 新建 MessageController.cs脚本并继承 BaseMessageController类, 挂载到unity物体并拖到 clientManager 缺失项上
使用方法简单快捷,请查看demo
Client:
public class ClientManager : BaseManager
{
protected override void Awake()
{
switch (networkType)
{
case NetworkType.Tcp:
network = new TcpClientManager(this.Ip, this.Port);
section = "Client_TCP";
break;
case NetworkType.Udp:
network = new UdpClientManager(this.Ip, this.Port);
section = "Client_UDP";
break;
}
base.Awake();
}
private void Start()
{
InvokeRepeating("ConnectedInterval", 0, connectInterval); //发送心跳
}
protected override void OnConnected(Message msg)
{
base.OnConnected(msg);
connectCount = 0;
LoginClass login = new LoginClass();
login.userid = 1;
login.username = "client01";
login.password = "123456";
string str = JsonConvert.SerializeObject(login);
SendMsg(str, HeaderType.Login);
}
#region 心跳
[Header("心跳发送总次数:")]
public int HeartCount = 0;
///
/// 设置重连次数
///
[Space(10), Header("尝试重连次数,-1无限次")] public int reConnCount = 1000;
///
/// 链接间隔时间
///
float connectInterval = 1f;
///
/// 链接次数
///
int connectCount = 0;
public UnityAction OnConnInterval = null;
///
/// 心跳检查 断开重连 在Update中调用才有效
///
protected void ConnectedInterval()
{
if (network == null || network.Connected == false)
{
HeartCount = 0;
network.Destroy();
UnityAction action = () =>
{
//链接次数增加
connectCount++;
string str = string.Format("这是第{0}次 连接", connectCount);
Debug.Log(str);
network.StartNetwork(); //重连一次
network.MaxMessageSize = 1024 * 1024;
};
if (reConnCount < 0)
{
action();
}
else
{
if (connectCount < reConnCount)
{
action();
}
}
OnConnInterval?.Invoke(connectCount);
}
else if (network.Connected)
{
HeartCount += 1;
SendMsg("1", HeaderType.Heart);
}
}
#endregion
}
Server:
public class ServerManager : BaseManager
{
public BaseLoginController loginController;
protected override void Awake()
{
switch (networkType)
{
case NetworkType.Tcp:
network = new TcpServerManager(this.Ip, this.Port);
section = "Server_TCP";
secDoc = "socket服务监听控制端参数, IP为本机IP,只可读无需更改";
break;
case NetworkType.Udp:
network = new UdpServerManager(this.Ip, this.Port);
section = "Server_UDP";
secDoc = "socket服务监听控制端参数, IP为本机IP,只可读无需更改";
break;
}
//network = networkType == NetworkType.Tcp ? new TcpServerManager() : new UdpServerManager();
base.Awake();
if (loginController != null) loginController.manager = this;
}
protected override void OnDisconnected(Message msg)
{
base.OnDisconnected(msg);
loginController?.OnDisconnected(msg);
}
protected override void OnLoginMessage(int connID, LoginMessage msg)
{
base.OnLoginMessage(connID, msg);
loginController?.OnLoginMessage(connID, msg);
}
}
#### 参与贡献
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/)