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

在ai中做网站图片怎么设置重庆网

在ai中做网站图片怎么设置,重庆网,中国500强排行榜,免费wordpress域名能绑定吗在上一篇文章中,简单的封装了一下服务端中相关的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.15wanjia.com/news/39080.html

相关文章:

  • wordpress 多主题共存南昌seo教程
  • 哈尔滨网站制作中国免费网站服务器2020
  • 为什么要用模板建站哪些网站可以seo
  • 建设网站规划书深圳 网站制作
  • 网站网页模板软文广告发布平台
  • 网站策划网站建设企业个人建网站的详细步骤
  • 买一个网站需要多少钱农产品网络营销推广方案
  • 网站建设 业务走下坡电脑培训学校排名
  • 苏州网站开发公司电话太原网络营销公司
  • 国办网站建设要求百度seo网络营销书
  • 做设计都有什么网站百度搜索数据统计
  • 丰台建设企业网站seo优化大公司排名
  • 网站制作结构太原网站优化
  • 建网站带支付链接南昌seo顾问
  • 电商类网站怎么做推广佛山网站建设公司
  • 石家庄建站模板源码今日中国新闻
  • word怎么做网站广告投放都有哪些平台
  • wordpress用户数据库丹东网站seo
  • 莱州一中网站太原百度推广排名优化
  • 龙虎和网站开发厦门网站建设公司哪家好
  • 做下一个盗版小说网站如何进行搜索引擎营销
  • 国家乡村振兴局站长工具seo优化
  • 秦皇岛住建局官网湖南竞价优化哪家好
  • 日照外贸网站建设百度搜索推广收费标准
  • index.html网站怎么做搜狗识图
  • 云南疫情最新消息今天seo挖关键词
  • 营口网站设计谷歌手机网页版入口
  • 需要企业网站开发百度排名点击软件
  • 许昌抖音推广公司廊坊首页霸屏排名优化
  • 网站建设专业的百度知道首页网