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

自己建网站做微商网站服务费一年多少钱

自己建网站做微商,网站服务费一年多少钱,西安门户网,做gif的网站以前使用UNet实现网络连接,Unity2018以后被弃用了。要将以前的老程序升到高版本,最开始打算使用Mirro,结果发现并不好用。那就只能自己写连接了。 1.TCP消息结构 (1). TCP消息是按流传输的,会发生粘包。那么在发射和接收消息时就需要对消息进行打包和解包。如果接收的消息…

以前使用UNet实现网络连接,Unity2018以后被弃用了。要将以前的老程序升到高版本,最开始打算使用Mirro,结果发现并不好用。那就只能自己写连接了。

1.TCP消息结构

(1). TCP消息是按流传输的,会发生粘包。那么在发射和接收消息时就需要对消息进行打包和解包。如果接收的消息长度不足,先不处理,继续接收。

(2).当TCP客户端断开时,服务端是收不到通知的。解决的方法是通过是否收到自定义消息来判断客户端是否在线。

这里用的消息结构如下,

第1部分为4个字节,表示消息长度,包括消息ID和消息体;

第2部分为2个字节,表示消息ID;

第3部分为n个字节,表示消息体;

2.辅助类和插件

(1). UnityThread:接收消息时在子线程中进行,处理消息后更新界面则只能在主线程中进行,这个类就是为了把实子线程中的有些操作放到主线程中。

(2). Newtonsoft.Json:这个插件可以实现Json字符串与Json对象之间的转换。

3.注意事项

(1). 将服务端和客户端设置为可后台运行Application.runInBackground = true

(2). UnityThread使用之前一定要初始化UnityThread.initUnityThread();

4.服务端代码

TcpServerScript .cs

using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;public class TcpServerScript : MonoBehaviour
{public Image imageTcpServerStatus;public Text textConnectCount;public Text textPrompt;//上一次接收到消息时间,客户端是否在线以本数组为准Dictionary<int, TcpClientInfo> dictConnectId2Client;public Dictionary<int, float> dictConnectId2Time;private Dictionary<int, ClientRemainData> dictConnectId2RemainData;private int port = 6001;/// <summary>private TcpListener tcpListener;private bool running = false;/// <summary>/// Background thread for TcpServer workload.  /// </summary>  private Thread tcpListenerThread;int globalConnectId = 1;void Awake(){UnityThread.initUnityThread();}void Start(){dictConnectId2Time = new Dictionary<int, float>();dictConnectId2Client = new Dictionary<int, TcpClientInfo>();dictConnectId2RemainData = new Dictionary<int, ClientRemainData>();StartCoroutine(delayStartTcpServer());}IEnumerator delayStartTcpServer(){yield return new WaitForSeconds(0.5f);try{tcpListener = new TcpListener(IPAddress.Any, 6001);tcpListener.Start();imageTcpServerStatus.color = Color.green;}catch (Exception ex){imageTcpServerStatus.color = Color.gray;Debug.Log(ex.Message);yield break;}tcpListenerThread = new Thread(new ThreadStart(ListenForIncommingRequests));tcpListenerThread.IsBackground = true;tcpListenerThread.Start();}private void Update(){if (Time.frameCount % 10 == 0){List<int> keys = dictConnectId2Time.Keys.ToList();for (int i = 0; i < keys.Count; i++){int connectId = keys[i];if (Time.time - dictConnectId2Time[connectId] > 3.0f){OnServerDisconnected(connectId);}}//StringMsg msg = new StringMsg();//msg.str = "hello";//keys = dictConnectId2Client.Keys.ToList();//for (int i = 0; i < keys.Count; i++)//{//    int key = keys[i];//    ServerSendOne(dictConnectId2Client[key].client,MessageId.MsgId_StringMsg, msg);//}}textConnectCount.text = string.Format("连接数:{0}", dictConnectId2Client.Count);}private void ListenForIncommingRequests(){running = true;ThreadPool.QueueUserWorkItem(this.ListenerWorker, null);}private void ListenerWorker(object token){while (running){TcpClient connectedTcpClient = tcpListener.AcceptTcpClient();TcpClientInfo clientInfo = new TcpClientInfo(connectedTcpClient, globalConnectId);UnityThread.executeInUpdate(() =>{if (!dictConnectId2Client.ContainsKey(clientInfo.connectionId)){dictConnectId2Client.Add(clientInfo.connectionId, clientInfo);           IPEndPoint endPoint = connectedTcpClient.Client.RemoteEndPoint as IPEndPoint;string clientIp = endPoint.Address.ToString();Debug.Log(clientIp);}});Debug.Log("连接:" + dictConnectId2Client.Count);ThreadPool.QueueUserWorkItem(this.HandleClientWorker, clientInfo);}tcpListener.Stop();}private void HandleClientWorker(object token){var clientInfo = token as TcpClientInfo;int connId = clientInfo.connectionId;if (!dictConnectId2RemainData.ContainsKey(clientInfo.connectionId)){dictConnectId2RemainData.Add(clientInfo.connectionId, new ClientRemainData(clientInfo));}using (var client = clientInfo.client as TcpClient)using (var nwStream = client.GetStream()){while (running){byte[] bufNumber = new byte[1000];int byReadNumber = nwStream.Read(bufNumber, 0, 1000);if (byReadNumber < 1){dictConnectId2Client.Remove(clientInfo.connectionId);Debug.Log("断开1:" + clientInfo.connectionId);break;}byte[] btsAdd = new byte[dictConnectId2RemainData[connId].lenRemain + byReadNumber];if (dictConnectId2RemainData[connId].lenRemain > 0){//拼接上次处理的字节Array.Copy(dictConnectId2RemainData[connId].btsRemain, 0, btsAdd, 0, dictConnectId2RemainData[connId].lenRemain);}Array.Copy(bufNumber, 0, btsAdd, dictConnectId2RemainData[connId].lenRemain, byReadNumber);List<byte> listRemain = new List<byte>();dealwithData(clientInfo, btsAdd, listRemain);if (listRemain.Count > 0){byte[] btsTemp = listRemain.ToArray();Array.Copy(btsTemp, 0, dictConnectId2RemainData[connId].btsRemain, 0, btsTemp.Length);dictConnectId2RemainData[connId].lenRemain = btsTemp.Length;}else
http://www.yidumall.com/news/17917.html

相关文章:

  • 开发软件网站多少钱网站seo视频教程
  • 榆林网站制作windows7系统优化工具
  • 上饶做网站的公司软文外链代发
  • 网站建设教程菜鸟物流优化关键词推广
  • 上虞网站建设文广网络网页版登录入口
  • 买空间送网站360指数
  • 做村易通网站站长要收费吗网站推广seo设置
  • 会展展厅设计搜索引擎优化网站
  • 免费建立网站的软件搜索竞价
  • 郑州网站优化汉狮网络世界排名前十位
  • 大兴区建设委员会网站搜索引擎平台有哪些软件
  • 淘宝推广网站怎么做百度网盘优化
  • 上海网站建设的网站域名查询seo
  • 出境旅游哪个网站做的好自己做一个网站要多少钱
  • 宁波网站建设与设计开发电脑学校培训
  • 关于网站开发的商业计划书如何在百度上做广告宣传
  • 做网站要学的知识链交换反应
  • 开利网络企业网站建设第一品牌成都搜狗seo
  • 淘宝网站建设没法上传怎么办广告代运营
  • 团购网站建设目的国内真正的永久免费砖石
  • 网站开发 案例详解vi设计公司
  • 南阳建设网站招聘国外网站推广平台有哪些
  • 大佛寺广州网站迅雷磁力链bt磁力天堂
  • 有机蔬菜哪个网站做的更好商城推广
  • 河北网站建站系统哪家好深圳最新政策消息
  • 网站建设软件定制开发网站友情链接怎么添加
  • html做网站公告产品软文代写
  • 品牌网站部门建设方案百度搜索引擎优化的方法
  • 临河可以做网站的公司怎么做ppt
  • 东山网站制作郑州网络营销顾问