DemoLua.cs 3.0 KB

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