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

网站推广的目的是什门户网站开发过程

网站推广的目的是什,门户网站开发过程,迅捷在线图片编辑,网络推广都有哪些方式制作UDPNetMgr网络管理模块 这段代码定义了一个名为UDPNetMgr的 Unity 脚本类,用于管理 UDP 网络通信,它作为单例存在,在Awake方法中创建收发消息的线程,Update方法处理接收到的消息;StartClient方法启动客户端连接&a…

制作UDPNetMgr网络管理模块

这段代码定义了一个名为UDPNetMgr的 Unity 脚本类,用于管理 UDP 网络通信,它作为单例存在,在Awake方法中创建收发消息的线程,Update方法处理接收到的消息;StartClient方法启动客户端连接,ReceiveMsgSendMsg方法分别用于接收和发送消息,Send方法将消息加入发送队列,Close方法关闭套接字并发送退出消息,同时在脚本销毁时自动调用关闭操作

using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using UnityEngine;public class UDPNetMgr : MonoBehaviour
{ private static UDPNetMgr instance;public static UDPNetMgr Instance => instance;private EndPoint serverIpPoint;public Socket socket;private bool IsClose=true;public byte[] cacheBytes = new byte[512];//创建两个队列来收消息和发消息public Queue<BaseMsg> sendMsgQueue = new Queue<BaseMsg>();public Queue<BaseMsg> receiveMsgQueue = new Queue<BaseMsg>();void Awake(){instance = this;DontDestroyOnLoad(this.gameObject);//创建两个线程来执行收发消息ThreadPool.QueueUserWorkItem(ReceiveMsg);ThreadPool.QueueUserWorkItem(SendMsg);}// Update is called once per framevoid Update(){if(receiveMsgQueue .Count >0){BaseMsg basemsg = receiveMsgQueue.Dequeue();switch (basemsg ){case PlayerMsg msg:print(msg.playerID);print(msg.playerData .atk);print(msg.playerData .lev);print(msg.playerData .name);break;}}}//启动客户端Socket的方法public void StartClient(string ip,int port){if (!IsClose)return;//记录远端服务器的Ip和端口号serverIpPoint = new IPEndPoint(IPAddress.Parse(ip), port);IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8081);try{socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);socket.Bind(ipPoint);IsClose = false;print("客户端网络启动");}catch (System.Exception e){print("启动客户端Socket失败" + e.Message);}}//接收消息的方法public void ReceiveMsg(object obj){EndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 0);int msgID;int nowIndex;int msgLength;while (socket!=null&&!IsClose){try{socket.ReceiveFrom(cacheBytes, ref ipEndPoint);//为了避免非服务器发来的骚扰消息if (!ipEndPoint.Equals(serverIpPoint))continue;//如果不是服务器发来的消息,就不处理了//处理服务器发来的消息nowIndex = 0;//解析IDmsgID = BitConverter.ToInt32(cacheBytes, nowIndex);nowIndex += 4;//解析消息长度msgLength = BitConverter.ToInt32(cacheBytes, nowIndex);nowIndex += 4;//解析消息体BaseMsg msg = null;switch (msgID){case 1001:msg = new PlayerMsg();//反序列化消息体msg.Reading(cacheBytes, nowIndex);break;}if (msg != null)receiveMsgQueue.Enqueue(msg);}catch (SocketException s){print("接收消息出问题" + s.SocketErrorCode + s.Message);}catch (System.Exception e){print("接收消息出问题(非网络问题)" + e.Message);}}}//发送消息的方法public void SendMsg(object obj){while (socket!=null&&!IsClose){if(sendMsgQueue .Count >0){try{socket.SendTo(sendMsgQueue.Dequeue().Writing(), serverIpPoint);}catch (SocketException e){print("发送消息失败" + e.SocketErrorCode + e.Message);}}}}public void Send(BaseMsg msg){sendMsgQueue.Enqueue(msg);}//关闭socketpublic void Close(){if(socket!=null){IsClose = true;quitMsg quitMsg = new quitMsg();//发送一个退出消息给服务器,让他移除记录socket.SendTo(quitMsg.Writing(), serverIpPoint);socket.Shutdown(SocketShutdown.Both);socket.Close();}}private void OnDestroy(){Close();}
}

启动UDP客户端

MainUDP类继承自MonoBehaviour,是一个 Unity 脚本。它的主要功能是在游戏开始时确保UDPNetMgr单例实例存在,然后启动 UDP 客户端并连接到指定的服务器地址和端口。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class MainUDP : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){if(UDPNetMgr .Instance ==null){GameObject obj = new GameObject("UdpNet");obj.AddComponent<UDPNetMgr>();}UDPNetMgr.Instance.StartClient(("127.0.0.1"), 8080);}// Update is called once per framevoid Update(){}
}

让客户端给服务器发消息

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class Lesson15 : MonoBehaviour
{public Button btn;// Start is called before the first frame updatevoid Start(){btn.onClick.AddListener(() =>{PlayerMsg playerMsg = new PlayerMsg();playerMsg.playerID = 1;playerMsg.playerData.atk = 888;playerMsg.playerData.lev = 999;playerMsg.playerData.name = "DamnF客户端发来的消息";UDPNetMgr.Instance.Send(playerMsg);});}// Update is called once per framevoid Update(){}
}

实现客户端和服务器同步通信

运行服务器,启动客户端,使客户端和服务器实现UDP同步通信


文章转载自:

http://8mtypOIm.mLcwL.cn
http://DCp8l4yq.mLcwL.cn
http://WEgdFanP.mLcwL.cn
http://8ILwpOPE.mLcwL.cn
http://kKstrn6n.mLcwL.cn
http://l30CVE3I.mLcwL.cn
http://OJxY2GVV.mLcwL.cn
http://4QmYdZYg.mLcwL.cn
http://cbq1ntFe.mLcwL.cn
http://ZSI4xjHz.mLcwL.cn
http://HZxunLtL.mLcwL.cn
http://oZzovgL3.mLcwL.cn
http://GhpQzL1N.mLcwL.cn
http://bdnxRthk.mLcwL.cn
http://1Y2B6jyc.mLcwL.cn
http://j95HKdJX.mLcwL.cn
http://WWwUQPL2.mLcwL.cn
http://tQNDbcMX.mLcwL.cn
http://b1cau04U.mLcwL.cn
http://SULd0Gv3.mLcwL.cn
http://dvNxQW73.mLcwL.cn
http://wsjMrOnV.mLcwL.cn
http://sogcQ5B5.mLcwL.cn
http://QU8guIFO.mLcwL.cn
http://ytpVpqe1.mLcwL.cn
http://pSDaJivj.mLcwL.cn
http://I2IHrhRk.mLcwL.cn
http://Y7DwjsEP.mLcwL.cn
http://xEQPhWBH.mLcwL.cn
http://L29RQDLa.mLcwL.cn
http://www.dtcms.com/wzjs/695026.html

相关文章:

  • 建设网站网址是多少seo优化是什么意思
  • 网站由哪儿三部分组成鸿鹄网站建设
  • 个人网站经营性备案龙岗营销网站建设
  • 便宜的网站空间成都芯片设计公司
  • wordpress如何优化网站速度emulate wordpress
  • 奉贤深圳网站建设公司公司宣传册排版
  • 电影网站建设模板青岛网站网站建设
  • 永久免费的网站如何在网站中做二级下拉菜单
  • 南宁京象建站公司网络营销招聘
  • 网络型网站分为企业网站设计推广方案
  • 网站建设公司企业网站管理系统宁波网上房地产
  • 公司网站打不开不知道谁做的云南百度小程序开发
  • 网站点击率原因多用户商城系统的服务商
  • 房地产网站怎么做托管管理系统app
  • 爱站网关键字挖掘软件开发和网站建设
  • 新浪微博可以做网站吗wordpress是指什么系统
  • 湖南北山建设集团股份有限公司官方网站国内域名和国外域名区别
  • 网站极速备案免费个人网站模板下载
  • 天津网站建设首选津坤科技哪些网站首页做的好
  • 如何通过网站开发客户凡科网小程序制作教程
  • 网站建设如何销售兴宁网站设计
  • 找网站开发公司需要注意那几点北京网站手机站建设公司
  • 南昌网站建设品牌广东省公路建设公司网站
  • 个人博客网站注册crm开源
  • 网站建设要会英语吗云主机建设网站
  • 网站平台设计 问题jsp和php哪个做网站快
  • pc网站如何做seo做响应式网站的价格
  • 云主机是不是可以搭建无数个网站怎么样查询建设网站
  • dede网站如何换logo游戏动画设计师需要学什么
  • 陕西省环保厅建设备案网站办网站如何备案