SocketManager.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Collections.Generic;
  3. using SocketIOClient;
  4. using SocketIOClient.Newtonsoft.Json;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using Newtonsoft.Json.Linq;
  8. using Debug = System.Diagnostics.Debug;
  9. public class SocketManager : MonoBehaviour
  10. {
  11. public SocketIOUnity socket;
  12. public InputField EventNameTxt;
  13. public InputField DataTxt;
  14. public Text ReceivedText;
  15. public GameObject objectToSpin;
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. //TODO: check the Uri if Valid.
  20. var uri = new Uri("http://127.0.0.1:11100");
  21. socket = new SocketIOUnity(uri, new SocketIOOptions
  22. {
  23. Query = new Dictionary<string, string>
  24. {
  25. {"token", "UNITY" }
  26. }
  27. ,
  28. EIO = 4
  29. ,
  30. Transport = SocketIOClient.Transport.TransportProtocol.WebSocket
  31. });
  32. socket.JsonSerializer = new NewtonsoftJsonSerializer();
  33. ///// reserved socketio events
  34. socket.OnConnected += (sender, e) =>
  35. {
  36. UnityEngine.Debug.Log("socket.OnConnected");
  37. };
  38. socket.OnPing += (sender, e) =>
  39. {
  40. UnityEngine.Debug.Log("Ping");
  41. };
  42. socket.OnPong += (sender, e) =>
  43. {
  44. UnityEngine.Debug.Log("Pong: " + e.TotalMilliseconds);
  45. };
  46. socket.OnDisconnected += (sender, e) =>
  47. {
  48. UnityEngine.Debug.Log("disconnect: " + e);
  49. };
  50. socket.OnReconnectAttempt += (sender, e) =>
  51. {
  52. UnityEngine.Debug.Log($"{DateTime.Now} Reconnecting: attempt = {e}");
  53. };
  54. ////
  55. UnityEngine.Debug.Log("Connecting...");
  56. socket.Connect();
  57. socket.OnUnityThread("spin", (data) =>
  58. {
  59. rotateAngle = 0;
  60. });
  61. ReceivedText.text = "";
  62. socket.OnAnyInUnityThread((name, response) =>
  63. {
  64. ReceivedText.text += "Received On " + name + " : " + response.GetValue().GetRawText() + "\n";
  65. });
  66. }
  67. public void EmitTest()
  68. {
  69. string eventName = EventNameTxt.text.Trim().Length < 1 ? "hello" : EventNameTxt.text;
  70. string txt = DataTxt.text;
  71. if (!IsJSON(txt))
  72. {
  73. socket.Emit(eventName, txt);
  74. }
  75. else
  76. {
  77. socket.EmitStringAsJSON(eventName, txt);
  78. }
  79. }
  80. public static bool IsJSON(string str)
  81. {
  82. if (string.IsNullOrWhiteSpace(str)) { return false; }
  83. str = str.Trim();
  84. if ((str.StartsWith("{") && str.EndsWith("}")) || //For object
  85. (str.StartsWith("[") && str.EndsWith("]"))) //For array
  86. {
  87. try
  88. {
  89. var obj = JToken.Parse(str);
  90. return true;
  91. }catch (Exception ex) //some other exception
  92. {
  93. Console.WriteLine(ex.ToString());
  94. return false;
  95. }
  96. }
  97. else
  98. {
  99. return false;
  100. }
  101. }
  102. public void EmitSpin()
  103. {
  104. socket.Emit("spin");
  105. }
  106. public void EmitClass()
  107. {
  108. TestClass testClass = new TestClass(new string[] { "foo", "bar", "baz", "qux" });
  109. TestClass2 testClass2 = new TestClass2("lorem ipsum");
  110. socket.Emit("class", testClass2);
  111. }
  112. // our test class
  113. [System.Serializable]
  114. class TestClass
  115. {
  116. public string[] arr;
  117. public TestClass(string[] arr)
  118. {
  119. this.arr = arr;
  120. }
  121. }
  122. [System.Serializable]
  123. class TestClass2
  124. {
  125. public string text;
  126. public TestClass2(string text)
  127. {
  128. this.text = text;
  129. }
  130. }
  131. //
  132. float rotateAngle = 45;
  133. readonly float MaxRotateAngle = 45;
  134. void Update()
  135. {
  136. if(rotateAngle < MaxRotateAngle)
  137. {
  138. rotateAngle++;
  139. objectToSpin.transform.Rotate(0, 1, 0);
  140. }
  141. }
  142. }