DemoSimple.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using UnityEngine;
  11. using ZLog;
  12. public class DemoSimple : MonoBehaviour
  13. {
  14. //string m_Ip = "192.168.0.121";
  15. //string m_Ip = "8.134.36.175";
  16. //int m_Port = 9305;
  17. string m_Ip = "192.168.0.55";
  18. int m_Port = 9410;
  19. float reconnectTime = 0;
  20. private void Start()
  21. {
  22. // 设置日志的Level,屏蔽Socket的部分日志。设到2以下,查看所有日志
  23. Options.Instance.LogLevel = 3;
  24. //Log.Verbose = false;
  25. // 在主线程初始化ZLog
  26. Log.Debug("");
  27. // 连接服务器
  28. SimpleSocketManager.Connect(m_Ip, m_Port);
  29. // 发送心跳包消息的测试
  30. StartCoroutine(SendLoop());
  31. }
  32. private void Update()
  33. {
  34. if (Input.GetKeyUp(KeyCode.Space))
  35. {
  36. CompositePictureReq();
  37. }
  38. if (SimpleSocketManager.Update() != ZSocket.ErrorCode.ERR_Success)
  39. {
  40. // 发生错误,重连处理
  41. if (Time.time > reconnectTime)
  42. {
  43. Debug.LogError($"Demo Reconnect ErrorCode: {SimpleSocketManager.ErrorCode}");
  44. SimpleSocketManager.Connect(m_Ip, m_Port);
  45. reconnectTime = Time.time + 1f;
  46. }
  47. }
  48. }
  49. private void OnDestroy()
  50. {
  51. // 断开与服务器的连接
  52. SimpleSocketManager.Disconnect();
  53. }
  54. [Serializable]
  55. public class Rect
  56. {
  57. public float x;
  58. public float y;
  59. public float width;
  60. public float height;
  61. }
  62. [Serializable]
  63. public class PictureReq
  64. {
  65. public Rect rect;
  66. public string jacketUrl;
  67. public string userUrl;
  68. }
  69. void CompositePictureReq()
  70. {
  71. byte[] bytes = File.ReadAllBytes(Path.Combine(Application.streamingAssetsPath, "panda.png"));
  72. string base64 = Convert.ToBase64String(bytes);
  73. string data = JsonUtility.ToJson(new PictureReq { rect = new Rect(), jacketUrl = "", userUrl = base64 });
  74. SimpleSocketManager.Call("COMPOSITE_PICTURE_REQ", data, (err, msg) => {
  75. if (err != ZSocket.ErrorCode.ERR_Success)
  76. {
  77. Debug.LogError($"Demo Rpc Error: {err} {msg}");
  78. return;
  79. }
  80. Debug.Log($"Demo Recv {msg}");
  81. });
  82. }
  83. IEnumerator SendLoop()
  84. {
  85. int i = 1;
  86. //while (i > 0)
  87. while (true)
  88. {
  89. i--;
  90. for (int j = 0; j < 1; ++j)
  91. {
  92. // 发送不需要回复的消息
  93. //Debug.Log($"Demo Send {SimpleSocketManager.ErrorCode}");
  94. //SimpleSocketManager.Send( "HEARTBEAT", @"{""ping"":true}");
  95. // 发送需要回复的RPC消息,结果通过回调获取
  96. Debug.Log($"Demo Rpc {SimpleSocketManager.ErrorCode}");
  97. SimpleSocketManager.Call("HEARTBEAT_REQ", @"{""ping"":true}", (err, msg) =>
  98. {
  99. if (err != ZSocket.ErrorCode.ERR_Success)
  100. {
  101. Debug.LogError($"Demo Rpc Error: {err} {msg}");
  102. return;
  103. }
  104. Debug.Log($"Demo Recv {msg}");
  105. });
  106. }
  107. yield return new WaitForSeconds(10f);
  108. //yield return new WaitForEndOfFrame();
  109. }
  110. }
  111. }