Demo.cs 1001 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using UnityEngine;
  6. public class Demo : MonoBehaviour
  7. {
  8. private void Start()
  9. {
  10. LuaSocketManager.Connect("192.168.1.85", 9305);
  11. StartCoroutine(SendLoop());
  12. StartCoroutine(ReceiveLoop());
  13. }
  14. private void OnDestroy()
  15. {
  16. LuaSocketManager.Disconnect();
  17. }
  18. IEnumerator SendLoop()
  19. {
  20. while (enabled)
  21. {
  22. Debug.Log("Demo Send");
  23. LuaSocketManager.Send(@"{ ""msgType"":""HEARTBEAT"", ""uid"":""cc187cd3-f36d-4b8a-9500-1f826e709b5d"" }");
  24. yield return new WaitForSeconds(1f);
  25. }
  26. }
  27. IEnumerator ReceiveLoop()
  28. {
  29. while (enabled)
  30. {
  31. yield return new WaitForSeconds(.1f);
  32. string msg = LuaSocketManager.RecvMessage();
  33. if (msg != null)
  34. {
  35. Debug.Log($"Demo Recv: {msg}");
  36. }
  37. }
  38. }
  39. }