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

做婚纱摄影网站多少钱seo关键词查询工具

做婚纱摄影网站多少钱,seo关键词查询工具,泰州企业网站建设公司,泰安人才网求职在上一篇文章中,简单的封装了一下服务端中相关的socket对象,为了可以更方便的使用。所以在本篇中,进一步封装一下在unity中的相关客户端类 封装客户端类,首先采用单例模式,然后采用两个队列来存储我们相关的收发信息 p…

在上一篇文章中,简单的封装了一下服务端中相关的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.dinnco.com/news/14798.html

相关文章:

  • 中国复转军人建设集团网站seo门户网站优化
  • 个人做网站有什么好处重庆网站seo公司
  • 平顶山车祸最新新闻事件浙江搜索引擎优化
  • 品牌的网站建设企业营销策略有哪些
  • 网站的建站公司突发大事震惊全国
  • 网站一级页面标题怎么做的百度收录比较好的网站
  • 做网站站长开通vip谷歌浏览器下载官方正版
  • 在线做编程题的网站员工培训内容
  • 成都网络科技公司排名seo的全称是什么
  • 网站推广关键词排名优化站长字体
  • vr 网站怎么做的网站定制
  • 怎么看一个网站是用什么代码做的电商广告
  • 漂亮的个人网站阿里巴巴国际贸易网站
  • 有没有做公务员题的网站网页设计培训
  • 怎么推广网站平台seo美式
  • avada如何做购物网站最新国内新闻重大事件
  • 做盗版小说网站犯法吗谷歌google下载
  • 做问卷调查赚钱网站好影视后期哪个培训靠谱
  • 杭州模板建站软件前端seo主要优化哪些
  • iis网站改端口地推公司
  • java做的网站怎么突破公司seo推广营销网站
  • top域名的网站打不开深圳网站优化推广方案
  • 做平面的网站互联网广告代理商
  • 蔚县做网站站长工具之家seo查询
  • 手表网站 源码班级优化大师官方网站
  • 哪家做企业网站百度网盘服务电话6988
  • 广州网站建设是什么网站seo优化皆宣徐州百都网络不错
  • jsp动态网站开发教程各大网站提交入口网址
  • 南昌做网站后台投票东莞seo网络公司
  • 个人婚礼网站模板长沙靠谱seo优化价格