SocketManager.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Text;
  5. using System;
  6. using System.Net;
  7. using System.IO;
  8. public class SocketManager : MonoBehaviour
  9. {
  10. // Use this for initialization
  11. public SocketClient serverConnerction;
  12. int number = 0;
  13. string date = "{\"type\":" + 0 + "}";
  14. void Start()
  15. {
  16. //启动连接
  17. StartServerConnerction();
  18. }
  19. // Update is called once per frame
  20. void Update()
  21. {
  22. HandleTasks(serverConnerction.receiveQueue);
  23. }
  24. /// <summary>
  25. /// 程序关闭前,关闭Socket连接
  26. /// </summary>
  27. private void OnDisable()
  28. {
  29. ClosedServerConnerction();
  30. }
  31. #region Socket双向数据
  32. /// <summary>
  33. /// 开启双向接收数据连接
  34. /// </summary>
  35. public void StartServerConnerction()
  36. {
  37. serverConnerction = new SocketClient("192.168.0.120", "8000");
  38. serverConnerction.OnConnectAsync();
  39. }
  40. public void SendData()
  41. {
  42. //serverConnerction.Send(date);
  43. }
  44. /// <summary>
  45. /// 关闭双向接收数据连接
  46. /// </summary>
  47. public void ClosedServerConnerction()
  48. {
  49. if (serverConnerction != null)
  50. {
  51. serverConnerction.Closed();
  52. }
  53. }
  54. #endregion
  55. /// <summary>
  56. /// 任务分发
  57. /// </summary>
  58. /// <param name="data_Queue"></param>
  59. void HandleTasks(Queue<byte[]> data_Queue)
  60. {
  61. if (data_Queue.Count > 0)
  62. {
  63. byte[] data = data_Queue.Dequeue();
  64. //标志位
  65. byte[] bufferTag = new byte[4];
  66. int idx = 20;
  67. Array.Copy(data, idx, bufferTag, 0, bufferTag.Length);
  68. //长度
  69. byte[] newLen = new byte[4];
  70. int idx_1 = 10;
  71. Array.Copy(data, idx_1, newLen, 0, newLen.Length);
  72. //包体
  73. byte[] bufferBody = new byte[int.Parse(GetString(newLen))];
  74. int idx_2 = 32;
  75. Array.Copy(data, idx_2, bufferBody, 0, bufferBody.Length);
  76. string json = GetString(bufferBody);//减去包头的最终json数据
  77. //string bodyJson = HttpUtility.UrlDecode(json, Encoding.UTF8);
  78. //print(bodyJson);
  79. if (GetString(bufferTag).Contains("LOGI"))//登录操作
  80. {
  81. }
  82. }
  83. }
  84. string GetString(byte[] buffer)
  85. {
  86. return Encoding.UTF8.GetString(buffer, 0, buffer.Length);
  87. }
  88. /// <summary>
  89. /// 登录格式
  90. /// </summary>
  91. /// <param name="user"></param>
  92. /// <param name="password"></param>
  93. /// <returns></returns>
  94. public string LoginString(string user, string password)
  95. {
  96. string result = null; ;
  97. string json = "{\"account\":\"" + user + "\",\"password\":\"" + password + "\"}";
  98. string len = "";
  99. if (json.Length.ToString().Length < 4)
  100. {
  101. for (int i = 0; i < 4 - json.Length.ToString().Length; i++)
  102. {
  103. len += "0";
  104. }
  105. len += json.Length.ToString();
  106. }
  107. result = "aa00838961" + len + "00 CLTLOGI0000avcd" + json + "ff";
  108. print("发送给服务器的帐号密码:"+result);
  109. return result;
  110. }
  111. }