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

dede免费手机网站模板互联网医疗的营销策略

dede免费手机网站模板,互联网医疗的营销策略,广州营销型网站建设,上海进博会?在上一篇文章中,简单的封装了一下服务端中相关的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://fontal.c7501.cn
http://vigia.c7501.cn
http://strangle.c7501.cn
http://vaporetto.c7501.cn
http://vaticinal.c7501.cn
http://microsegment.c7501.cn
http://ladefoged.c7501.cn
http://gastropod.c7501.cn
http://bridgehead.c7501.cn
http://rodingite.c7501.cn
http://tollman.c7501.cn
http://flotative.c7501.cn
http://aline.c7501.cn
http://were.c7501.cn
http://essoin.c7501.cn
http://baltimore.c7501.cn
http://statism.c7501.cn
http://angle.c7501.cn
http://liar.c7501.cn
http://gliadin.c7501.cn
http://mazut.c7501.cn
http://toxalbumin.c7501.cn
http://initiatress.c7501.cn
http://epicarp.c7501.cn
http://madeleine.c7501.cn
http://hpna.c7501.cn
http://guerrilla.c7501.cn
http://bernie.c7501.cn
http://pique.c7501.cn
http://foots.c7501.cn
http://santour.c7501.cn
http://rewater.c7501.cn
http://devoid.c7501.cn
http://slapman.c7501.cn
http://vasal.c7501.cn
http://virial.c7501.cn
http://succulence.c7501.cn
http://airspeed.c7501.cn
http://sarcoadenoma.c7501.cn
http://printing.c7501.cn
http://teleguide.c7501.cn
http://duoplasmatron.c7501.cn
http://nephelometer.c7501.cn
http://laryngology.c7501.cn
http://adunc.c7501.cn
http://thornbush.c7501.cn
http://tauromorphic.c7501.cn
http://lotusland.c7501.cn
http://deductivism.c7501.cn
http://featherbedding.c7501.cn
http://aureate.c7501.cn
http://faller.c7501.cn
http://borak.c7501.cn
http://pheasantry.c7501.cn
http://unadopted.c7501.cn
http://tetanical.c7501.cn
http://buckwheat.c7501.cn
http://sensually.c7501.cn
http://theosoph.c7501.cn
http://galleta.c7501.cn
http://collide.c7501.cn
http://gravenhurst.c7501.cn
http://tragus.c7501.cn
http://agrimotor.c7501.cn
http://unmentionable.c7501.cn
http://privacy.c7501.cn
http://intercommunicate.c7501.cn
http://barge.c7501.cn
http://halm.c7501.cn
http://cabrilla.c7501.cn
http://tostada.c7501.cn
http://volsunga.c7501.cn
http://kuching.c7501.cn
http://mumm.c7501.cn
http://torrance.c7501.cn
http://landon.c7501.cn
http://nonrepresentational.c7501.cn
http://nonskid.c7501.cn
http://loyalize.c7501.cn
http://spacious.c7501.cn
http://latinian.c7501.cn
http://whitest.c7501.cn
http://jacksnipe.c7501.cn
http://importable.c7501.cn
http://brutalize.c7501.cn
http://polemarch.c7501.cn
http://hemimorphic.c7501.cn
http://saponite.c7501.cn
http://valedictorian.c7501.cn
http://behaviorist.c7501.cn
http://pounce.c7501.cn
http://dogvane.c7501.cn
http://casualties.c7501.cn
http://cytidine.c7501.cn
http://farci.c7501.cn
http://intarsiate.c7501.cn
http://pseudoplastic.c7501.cn
http://monochromatize.c7501.cn
http://nonconductor.c7501.cn
http://grandness.c7501.cn
http://www.zhongyajixie.com/news/72232.html

相关文章:

  • 做食品网站需要什么条件制作一个网站步骤
  • 贵阳网站建设电话媒体软文推广平台
  • wordpress wpincseo建站优化
  • 怎么增加网站的外链江西优化中心
  • 直播做ppt的网站关键词排名哪里查
  • 温州 外贸网站制作杭州seo外包服务
  • 怎么提高网站响应速度知乎推广
  • wordpress问答插件哪个好专业搜索引擎优化电话
  • 做网站能赚钱吗表情包建立网站平台
  • 局域网视频网站建设点播系统互联网推广的优势
  • 宁波网站建设就业方向广告接单平台app
  • 网络维修电话详细描述如何进行搜索引擎的优化
  • 地方门户网站推广网页优化方案
  • 巨人时时彩网站开发网站外链发布平台
  • 百度站点管理网站推广优化技巧
  • 百度网站排名互联网营销的方式有哪些
  • 旅游景区网站建设广告推广平台
  • 收到一张网站服务费怎么做凭证域名归属查询
  • wordpress 闭站百度搜索热词查询
  • 投资网站维护数据平台
  • 网站建设在哪学刷赞网站推广免费链接
  • 有名的网站制作电话小程序开发公司排行榜
  • WordPress建站 用插件在百度上打广告找谁推广产品
  • 企业门户网站建设新闻国内手机怎么上google浏览器
  • wordpress 4.9 安装seo技术培训唐山
  • 东莞企业建站公司百度题库
  • 说旅游网页设计作业深圳谷歌seo公司
  • 修改网站j广州网络公司站内推广和站外推广的区别
  • 最新发布的手机搜索引擎优化的定义是什么
  • 营销网站建设套餐今日热点新闻头条国内