当前位置: 首页 > news >正文

Unity网络开发记录(四):在unity中进一步封装客户端类

在上一篇文章中,简单的封装了一下服务端中相关的socket对象,为了可以更方便的使用。所以在本篇中,进一步封装一下在unity中的相关客户端类

封装客户端类,首先采用单例模式,然后采用两个队列来存储我们相关的收发信息

    private static NetManager instance;public static NetManager Instance => instance;private Socket socket;//客户端socketprivate Queue<string> receiveQue = new Queue<string>();//接收队列,子线程放,主线程拿取private Queue<string> sendQue = new Queue<string>();//发送队列,主线程放,子线程拿取并发送private bool isConnteced;private int num;//接收信息字节大小private byte[] reveiveBuffer = new byte[1024 * 5];

连接函数

    public void Connect(string ip, int port){if (socket is null)socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse(ip), port);try{socket.Connect(ipPoint);isConnteced = true;ThreadPool.QueueUserWorkItem(SendMsg);ThreadPool.QueueUserWorkItem(ReceiveMsg);}catch (SocketException s){if (s.ErrorCode == 10061)print("服务器拒绝连接");elseprint("连接出错 " + s.ErrorCode);return;}}

处理信息发送和接收

    public void Send(string info){sendQue.Enqueue(info);}public void Receive(){if (receiveQue.Count > 0){print(receiveQue.Dequeue());}}

发送消息时,主线程只用把信息存进发送队列里就行,子线程负责从队列中取出信息进行发送

接收消息时,子线程负责对发送过来的字节流进行处理,将处理完毕后的信息存储接收队列里,主线程只需要不断的读取队列里的信息就行

    private void SendMsg(object obj){if (socket is null) return;while(isConnteced){if (sendQue.Count > 0){socket.Send(Encoding.UTF8.GetBytes(sendQue.Dequeue()));}}}private void ReceiveMsg(object obj){if (socket is null) return;while(isConnteced){if (socket.Available > 0){num = socket.Receive(reveiveBuffer);receiveQue.Enqueue(Encoding.UTF8.GetString(reveiveBuffer, 0, num));}}}

 

使用就很简单了,在unity中新建一个脚本,调用一下Connect方法就可以进行连接了

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Main : MonoBehaviour
{void Start(){if (NetManager.Instance is null){GameObject game = new GameObject();game.AddComponent<NetManager>();}NetManager.Instance.Connect("127.0.0.1", 8080);}
}

 新建一个ui画布,添加一个输入框,一个按钮,按钮绑定一下send方法就可以进行简单输入信息并发送了

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class Input : MonoBehaviour
{public Button button;public InputField inputField;private void Start(){button.onClick.AddListener(() =>{if (inputField.text != null){NetManager.Instance.Send(inputField.text);}});}
}

using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using UnityEngine;public class NetManager : MonoBehaviour
{private static NetManager instance;public static NetManager Instance => instance;private Socket socket;//客户端socketprivate Queue<string> receiveQue = new Queue<string>();//接收队列,子线程放,主线程拿取private Queue<string> sendQue = new Queue<string>();//发送队列,主线程放,子线程拿取并发送private bool isConnteced;private int num;//接收信息字节大小private byte[] reveiveBuffer = new byte[1024 * 5];private void Awake(){instance = this;}private void Update(){Receive();}public void Connect(string ip, int port){if (socket is null)socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse(ip), port);try{socket.Connect(ipPoint);isConnteced = true;ThreadPool.QueueUserWorkItem(SendMsg);ThreadPool.QueueUserWorkItem(ReceiveMsg);}catch (SocketException s){if (s.ErrorCode == 10061)print("服务器拒绝连接");elseprint("连接出错 " + s.ErrorCode);return;}}public void Send(string info){sendQue.Enqueue(info);}public void Receive(){if (receiveQue.Count > 0){print(receiveQue.Dequeue());}}public void Close(){if (socket != null){socket.Shutdown(SocketShutdown.Both);socket.Close();socket = null;}}private void SendMsg(object obj){if (socket is null) return;while(isConnteced){if (sendQue.Count > 0){socket.Send(Encoding.UTF8.GetBytes(sendQue.Dequeue()));}}}private void ReceiveMsg(object obj){if (socket is null) return;while(isConnteced){if (socket.Available > 0){num = socket.Receive(reveiveBuffer);receiveQue.Enqueue(Encoding.UTF8.GetString(reveiveBuffer, 0, num));}}}
}

http://www.lryc.cn/news/449734.html

相关文章:

  • Linux内核中的UART驱动-详解Linux内核UART驱动:结构与功能分析
  • 威胁检测与防范:如何及时、准确对抗安全风险
  • 数据结构串的kmp相关(求next和nextval)
  • 创建游戏暂停菜单
  • seata服务端部署
  • 理解Python闭包概念
  • 51单片机的教室智能照明系统【proteus仿真+程序+报告+原理图+演示视频】
  • 一款资产进行快速存活验证工具
  • I/O中断处理过程
  • 关于PHP 匿名函数在处理数据结构中的应用
  • 安卓13默认使用大鼠标 与配置分析 andriod13默认使用大鼠标 与配置分析
  • AI学习指南深度学习篇-批标准化在深度学习中的应用
  • 了解网络的相关信息
  • Java | Leetcode Java题解之第447题回旋镖的数量
  • Docker实践与应用举例
  • 828华为云征文 | 智能监控新篇章,Prometheus如何在华为云Flexusx容器环境中大展身手
  • 基于单片机的可调式中文电子日历系统
  • 《C++设计新思维-泛型编程与设计模式之应用》阅读记录
  • vue访问组件的数据和方法
  • Redis: RDB与AOF的选择和容灾备份以及Redis数据持久化的优化方案
  • Goweb---Gorm操作数据库(二)
  • 鸿蒙HarmonyOS之封装Http请求工具类
  • java基础(4)类和对象
  • [Linux]:线程(二)
  • 【unity进阶知识3】封装一个事件管理系统
  • 服务器使用frp做内网穿透详细教程,请码住
  • 小程序视频编辑SDK解决方案,轻量化视频制作解决方案
  • ERROR [internal] load metadata for docker.io/library/openjdk:8
  • Wed前端--HTML基础
  • Latex 自定义运算符加限定条件的实现