Netty游戏服务器之三搭建Unity客户端
2024-09-04 12:31:53
既然已经写完了相关的服务器处理类,那么我们就来搭建客户端测试一下。
打开我们的unity3d,然后新建一个c#脚本,取名为MainClient。
public class MainClient : MonoBehaviour{
private const string HOST = "127.0.0.1";
private const int PORT = 8080;
public static MainClient instance;
public static TcpClient client;
void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(this.gameObject);
}
}
void Start()
{
if (client == null)
{
Connect();
}
}
void Update()
{
}
void OnApplicationQuit()
{
client.Close();
}
public void Connect()
{
client = new TcpClient();
try{
client.Connect(HOST,PORT);
}catch(Exception e){
Debug.LogException(e);
client.Close();
}
} }
然后再Hierarchy窗口新建一个gameobject,将MainClient赋给它,将之做成prefab。
好了,我们先启动服务器,再启动我们的unity3d工程,会发现呢
服务器会跳转到之前我们写的处理类ServerHandler的方法,public void channelActive(ChannelHandlerContext ctx),打印这句话,channel.id()就是唯一标识该客户端,感兴趣的童鞋可以去学习netty的源代码。
当我们断开unity3d客户端,就会调用public void channelInactive(ChannelHandlerContext ctx)
好了测试相关的已经成功了,如果有什么问题可以留言给我,这个是我服务器写到一定程度才写这篇博文的,可能有步骤不对,你们尽管指出来。
最新文章
- WinCE项目应用之车载导航
- 【Alpha版本】冲刺-Day7
- top命令详解(转,详细)
- libtool: link: `dftables.lo' is not a valid libtool object
- Java中只有按值传递,没有按引用传递!
- 最小K个数之和
- ZOJ3732 Graph Reconstruction Havel-Hakimi定理
- sql server语句
- cf B. Levko and Permutation
- Jmeter函数组件开发
- 使用paramiko远程执行
- 消息中间件kafka+zookeeper集群部署、测试与应用
- JSP之Cookie的实现
- unity---背景循环滚动
- mysql灾备演练问题
- jQuery 省份选择
- MongoDB pymongo模块 更新数据
- MySQL Binlog和Relaylog生成和清理
- MapReduce模型探究--总览
- 循环队列 c 实现!!!!