JCSocketIO.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using UnityEngine;
  3. using BestHTTP.WebSocket;
  4. using Newtonsoft.Json;
  5. namespace JC.SocketIO {
  6. public class SocketIOClient : MonoBehaviour {
  7. private WebSocket _ws;
  8. private string _uri;
  9. public virtual void OnDestroy() {
  10. if (_ws != null) {
  11. _ws.Close();
  12. _ws = null;
  13. }
  14. }
  15. public void connectServer(string uri) {
  16. this._uri = uri;
  17. this._ws = new WebSocket(new Uri(this._uri));
  18. this._ws.StartPingThread = true;
  19. this._ws.OnOpen += delegate(WebSocket webSocket) {
  20. loadEntity();
  21. };
  22. this._ws.OnClosed = delegate(WebSocket webSocket, UInt16 code, string message) {
  23. destroyEntity();
  24. };
  25. this._ws.OnError = delegate(WebSocket webSocket, Exception ex) {
  26. destroyEntity();
  27. };
  28. this._ws.OnMessage = delegate(WebSocket webSocket, string message) {
  29. this.invoke(JsonConvert.DeserializeObject<DataPack>(message));
  30. };
  31. this._ws.Open();
  32. }
  33. public void reconnectServer() {
  34. if (_ws != null) {
  35. _ws.Close();
  36. _ws = null;
  37. }
  38. if (_uri != null) {
  39. connectServer(_uri);
  40. }
  41. }
  42. [NonSerialized] public bool isValid;
  43. [NonSerialized] public bool loaded;
  44. public virtual void onLoad() {}
  45. public virtual void onReload() {}
  46. public virtual void onDestroy() {}
  47. public virtual void onMiss() {}
  48. public void call(string func, params object[] args) {
  49. if (!isValid) return;
  50. DataPack dataPack = new DataPack();
  51. dataPack.type = 1;
  52. dataPack.func = func;
  53. dataPack.args = Utility.FormatArgs(args);
  54. _ws.Send(JsonConvert.SerializeObject(dataPack));
  55. }
  56. private void invoke(DataPack dataPack) {
  57. if (dataPack.type == 1) {
  58. System.Reflection.MethodInfo method = this.GetType().GetMethod(dataPack.func);
  59. Utility.FormatArgsType(dataPack.args, method.GetParameters());
  60. method.Invoke(this, dataPack.args);
  61. }
  62. }
  63. public void loadEntity() {
  64. this.isValid = true;
  65. try {
  66. if (this.loaded) {
  67. this.onReload();
  68. } else {
  69. this.onLoad();
  70. }
  71. } catch (Exception e) { Debug.LogError(e.Message); }
  72. this.loaded = true;
  73. }
  74. private void destroyEntity() {
  75. try {
  76. if (this.isValid) {
  77. this.isValid = false;
  78. this.onDestroy();
  79. } else {
  80. this.onMiss();
  81. }
  82. } catch (Exception e) { Debug.LogError(e.Message); }
  83. }
  84. class DataPack {
  85. public int type;
  86. public string func;
  87. public object[] args;
  88. }
  89. class Utility {
  90. public static long GetTimestamp() {
  91. TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);
  92. return (long)ts.TotalMilliseconds;
  93. }
  94. public static object[] FormatArgs(object[] args) {
  95. object[] newArgs = new object[args.Length];
  96. for (int i = 0; i < args.Length; i++) {
  97. object arg = args[i];
  98. if (!IsBaseType(arg.GetType())) {
  99. newArgs[i] = JsonConvert.SerializeObject(arg);
  100. } else {
  101. newArgs[i] = arg;
  102. }
  103. }
  104. return newArgs;
  105. }
  106. public static void FormatArgsType(object[] args, System.Reflection.ParameterInfo[] parameters) {
  107. int i = 0;
  108. foreach (var param in parameters) {
  109. Type type = param.ParameterType;
  110. if (IsBaseType(type)) {
  111. args[i] = Convert.ChangeType(args[i], type);
  112. } else {
  113. args[i] = JsonConvert.DeserializeObject((string) args[i], type);
  114. }
  115. i++;
  116. }
  117. }
  118. public static bool IsBaseType(Type type) {
  119. if (typeof(System.Int32).Equals(type)) return true;
  120. if (typeof(System.Int64).Equals(type)) return true;
  121. if (typeof(System.Single).Equals(type)) return true;
  122. if (typeof(System.Double).Equals(type)) return true;
  123. if (typeof(System.String).Equals(type)) return true;
  124. if (typeof(System.Boolean).Equals(type)) return true;
  125. return false;
  126. }
  127. }
  128. }
  129. }