JCSocketIO.cs 5.1 KB

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