1 Star 12 Fork 4

couday.com/UnityTcpSocketSimple

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

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;
	/// <summary>
	/// 设置重连次数
	/// </summary>
	[Space(10), Header("尝试重连次数,-1无限次")] public int reConnCount = 1000;
	/// <summary>
	/// 链接间隔时间
	/// </summary>
	float connectInterval = 1f;

	/// <summary>
	/// 链接次数
	/// </summary>
	int connectCount = 0;

	public UnityAction<int> OnConnInterval = null;

	/// <summary>
	/// 心跳检查 断开重连 在Update中调用才有效
	/// </summary>
	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
  3. 你可以 https://gitee.com/explore 这个地址来了解码云上的优秀开源项目
  4. GVP 全称是码云最有价值开源项目,是码云综合评定出的优秀开源项目
  5. 码云官方提供的使用手册 https://gitee.com/help
  6. 码云封面人物是一档用来展示码云会员风采的栏目 https://gitee.com/gitee-stars/
MIT License Copyright (c) 2020 www.couday.com 传送门 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

unity3D Tcp框架, 基于Telepathy , 包含服务端和客户端, 用于unity内端通讯,高效稳定. 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者

全部

语言

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/couday/UnityTcpSocketSimple.git
git@gitee.com:couday/UnityTcpSocketSimple.git
couday
UnityTcpSocketSimple
UnityTcpSocketSimple
master

搜索帮助