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

淘宝客推广怎样做网站书法网站模板下载

淘宝客推广怎样做网站,书法网站模板下载,怎么做淘宝一样的网站,滨城区住房和城乡建设局网站在上一篇文章中,简单的封装了一下服务端中相关的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.yayakq.cn/news/23758/

相关文章:

  • 中国风网站配色方案有哪些企业可以做招聘的网站有哪些
  • 做瞹瞹爱视频网站营销师资格证报名官网
  • 南开做网站网站开发都需要什么
  • 怎样为网站做外链页面设计是最后一步吗
  • 做家装的网站有哪些内容网站建设 鸿
  • vs做的网站如何使用app制作平台神医嫡女漫画
  • 免费空间 网站代码重构网站
  • 宜兴淘宝网站建设推荐手机网站建设
  • 男女做爰视频网站在线湖北省建设厅网站证件
  • 网站建设需求文案案例wordpress网站管理
  • 上海企业网站优化企业网站建设背景
  • 有用模板网在线制作免费网站wordpress页面链接
  • 长宁区网站建设设计深圳地铁建设集团网站
  • 个人网站效果图咋做甘肃省城乡建设厅网站
  • 网站维护和更新wordpress相册管理
  • 瑞昌市环保局网站建设传奇网页游戏排行
  • 山东省建设厅招标网站首页实业公司注册条件
  • 万网如何建设购物网站我的世界封面制作网站
  • 长安响应式网站建设wordpress自定义搜索框
  • 王者荣耀网站开发目的青岛网站建设制作
  • 《商务网站开发》 实训报告书公司装修深圳
  • 做网站广告送报纸广告湖南做网站公司
  • 营销型网站建设大千企业邮箱给我一个
  • 新网互联的网站成都网站建设联系方式
  • 中学网站域名用什么用建筑工地招工网
  • 响应式网站制作公司wordpress移动端发表失败
  • 网上做网站网站吗昆山做网站优化
  • 音乐网站后台模板做网站 用什么做数据库最好
  • 淄博网站建设服务淮北网站建设求职简历
  • 合肥房产网东莞seo排名公司