WebSocketService.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #if UNITY_WEBGL // && !UNITY_EDITOR
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using AOT;
  6. namespace XGame.Framework.Network.Web
  7. {
  8. /// <summary>
  9. /// Class providing static access methods to work with JSLIB WebSocket
  10. /// </summary>
  11. internal static class WebSocketService
  12. {
  13. /* Map of websocket instances */
  14. private static Dictionary<int, WebSocket> sockets = new Dictionary<int, WebSocket>();
  15. /* Delegates */
  16. public delegate void OnOpenCallback(int instanceId);
  17. public delegate void OnMessageCallback(int instanceId, IntPtr msgPtr, int msgSize);
  18. public delegate void OnMessageStrCallback(int instanceId, IntPtr msgStrPtr);
  19. public delegate void OnErrorCallback(int instanceId, IntPtr errorPtr);
  20. public delegate void OnCloseCallback(int instanceId, int closeCode, IntPtr reasonPtr);
  21. /* WebSocket JSLIB functions */
  22. [DllImport("__Internal")]
  23. public static extern int WebSocketConnect(int instanceId);
  24. [DllImport("__Internal")]
  25. public static extern int WebSocketClose(int instanceId, int code, string reason);
  26. [DllImport("__Internal")]
  27. public static extern int WebSocketSend(int instanceId, byte[] dataPtr, int dataLength);
  28. [DllImport("__Internal")]
  29. public static extern int WebSocketSendStr(int instanceId, string data);
  30. [DllImport("__Internal")]
  31. public static extern int WebSocketGetState(int instanceId);
  32. /* WebSocket JSLIB callback setters and other functions */
  33. [DllImport("__Internal")]
  34. public static extern int WebSocketAllocate(string url);
  35. [DllImport("__Internal")]
  36. public static extern int WebSocketAddSubProtocol(int instanceId, string protocol);
  37. [DllImport("__Internal")]
  38. public static extern void WebSocketFree(int instanceId);
  39. [DllImport("__Internal")]
  40. public static extern void WebSocketSetOnOpen(OnOpenCallback callback);
  41. [DllImport("__Internal")]
  42. public static extern void WebSocketSetOnMessage(OnMessageCallback callback);
  43. [DllImport("__Internal")]
  44. public static extern void WebSocketSetOnMessageStr(OnMessageStrCallback callback);
  45. [DllImport("__Internal")]
  46. public static extern void WebSocketSetOnError(OnErrorCallback callback);
  47. [DllImport("__Internal")]
  48. public static extern void WebSocketSetOnClose(OnCloseCallback callback);
  49. /* If callbacks was initialized and set */
  50. private static bool isInitialized = false;
  51. /* Initialize WebSocket callbacks to JSLIB */
  52. private static void Initialize()
  53. {
  54. WebSocketSetOnOpen(DelegateOnOpenEvent);
  55. WebSocketSetOnMessage(DelegateOnMessageEvent);
  56. WebSocketSetOnMessageStr(DelegateOnMessageStrEvent);
  57. WebSocketSetOnError(DelegateOnErrorEvent);
  58. WebSocketSetOnClose(DelegateOnCloseEvent);
  59. isInitialized = true;
  60. }
  61. [MonoPInvokeCallback(typeof(OnOpenCallback))]
  62. public static void DelegateOnOpenEvent(int instanceId)
  63. {
  64. if (sockets.TryGetValue(instanceId, out var socket))
  65. {
  66. socket.HandleOnOpen();
  67. }
  68. }
  69. [MonoPInvokeCallback(typeof(OnMessageCallback))]
  70. public static void DelegateOnMessageEvent(int instanceId, IntPtr msgPtr, int msgSize)
  71. {
  72. if (sockets.TryGetValue(instanceId, out var socket))
  73. {
  74. var bytes = SessionBufferPool.Acquire();
  75. Marshal.Copy(msgPtr, bytes, 0, msgSize);
  76. socket.HandleOnMessage(bytes, 0, msgSize);
  77. }
  78. }
  79. [MonoPInvokeCallback(typeof(OnMessageStrCallback))]
  80. public static void DelegateOnMessageStrEvent(int instanceId, IntPtr msgStrPtr)
  81. {
  82. if (sockets.TryGetValue(instanceId, out var socket))
  83. {
  84. string msgStr = Marshal.PtrToStringAuto(msgStrPtr);
  85. socket.HandleOnMessageStr(msgStr);
  86. }
  87. }
  88. [MonoPInvokeCallback(typeof(OnErrorCallback))]
  89. public static void DelegateOnErrorEvent(int instanceId, IntPtr errorPtr)
  90. {
  91. if (sockets.TryGetValue(instanceId, out var socket))
  92. {
  93. string errorMsg = Marshal.PtrToStringAuto(errorPtr);
  94. socket.HandleOnError(errorMsg);
  95. }
  96. }
  97. [MonoPInvokeCallback(typeof(OnCloseCallback))]
  98. public static void DelegateOnCloseEvent(int instanceId, int closeCode, IntPtr reasonPtr)
  99. {
  100. if (sockets.TryGetValue(instanceId, out var socket))
  101. {
  102. string reason = Marshal.PtrToStringAuto(reasonPtr);
  103. socket.HandleOnClose((ushort)closeCode, reason);
  104. sockets.Remove(instanceId);
  105. }
  106. }
  107. internal static int AllocateInstance(string address)
  108. {
  109. if (!isInitialized) Initialize();
  110. return WebSocketAllocate(address);
  111. }
  112. internal static void Add(WebSocket socket)
  113. {
  114. if (!sockets.ContainsKey(socket.instanceId))
  115. {
  116. sockets.Add(socket.instanceId, socket);
  117. }
  118. }
  119. internal static void Remove(int instanceId)
  120. {
  121. if (sockets.ContainsKey(instanceId))
  122. {
  123. sockets.Remove(instanceId);
  124. }
  125. }
  126. }
  127. }
  128. #endif