SocketIOUnity.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // SocketIOUnity.cs
  3. // SocketIOUnity
  4. //
  5. // Created by itisnajim on 10/30/2021.
  6. // Copyright (c) 2021 itisnajim. All rights reserved.
  7. //
  8. using System;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using SocketIOClient;
  12. using SocketIOClient.Messages;
  13. public class SocketIOUnity : SocketIO
  14. {
  15. public enum UnityThreadScope
  16. {
  17. Update,
  18. LateUpdate,
  19. FixedUpdate
  20. }
  21. public UnityThreadScope unityThreadScope = UnityThreadScope.Update;
  22. public SocketIOUnity(string uri, UnityThreadScope unityThreadScope = UnityThreadScope.Update) : base(uri)
  23. {
  24. CommonInit(unityThreadScope);
  25. }
  26. public SocketIOUnity(Uri uri, UnityThreadScope unityThreadScope = UnityThreadScope.Update) : base(uri)
  27. {
  28. CommonInit(unityThreadScope);
  29. }
  30. public SocketIOUnity(string uri, SocketIOOptions options, UnityThreadScope unityThreadScope = UnityThreadScope.Update) : base(uri, options)
  31. {
  32. CommonInit(unityThreadScope);
  33. }
  34. public SocketIOUnity(Uri uri, SocketIOOptions options, UnityThreadScope unityThreadScope = UnityThreadScope.Update) : base(uri, options)
  35. {
  36. CommonInit(unityThreadScope);
  37. }
  38. private void CommonInit(UnityThreadScope unityThreadScope)
  39. {
  40. UnityThread.initUnityThread();
  41. this.unityThreadScope = unityThreadScope;
  42. }
  43. /// <summary>
  44. /// Register a new handler for the given event.
  45. /// </summary>
  46. /// <param name="eventName"></param>
  47. /// <param name="callback"></param>
  48. public void OnUnityThread(string eventName, Action<SocketIOResponse> callback)
  49. {
  50. On(eventName, res =>
  51. {
  52. ExecuteInUnityThread(() => callback(res));
  53. });
  54. }
  55. public void OnAnyInUnityThread(OnAnyHandler handler)
  56. {
  57. OnAny((name, response) =>
  58. {
  59. ExecuteInUnityThread(() => handler(name, response));
  60. });
  61. }
  62. /// <summary>
  63. /// Emits an event to the socket
  64. /// </summary>
  65. /// <param name="eventName"></param>
  66. /// <param name="data">Any other parameters can be included. All serializable datastructures are supported, including byte[]</param>
  67. /// <returns></returns>
  68. public void Emit(string eventName, params object[] data)
  69. {
  70. EmitAsync(eventName, data).ContinueWith(t => {});
  71. }
  72. public void Emit(string eventName, Action<SocketIOResponse> ack, params object[] data)
  73. {
  74. EmitAsync(eventName, CancellationToken.None, ack, data).ContinueWith(t => {});
  75. }
  76. public async Task EmitStringAsJSONAsync(string eventName, string json)
  77. {
  78. var msg = new EventMessage
  79. {
  80. Namespace = Namespace,
  81. Event = eventName,
  82. };
  83. if (!string.IsNullOrEmpty(json))
  84. {
  85. msg.Json = "["+json+"]";
  86. }
  87. await _transport.SendAsync(msg, CancellationToken.None).ConfigureAwait(false);
  88. }
  89. public void EmitStringAsJSON(string eventName, string json)
  90. {
  91. EmitStringAsJSONAsync(eventName, json).ContinueWith(t => { });
  92. }
  93. public void Connect()
  94. {
  95. ConnectAsync().ContinueWith(t => {});
  96. }
  97. public void Disconnect()
  98. {
  99. DisconnectAsync().ContinueWith(t => {});
  100. }
  101. private void ExecuteInUnityThread(Action action)
  102. {
  103. switch (unityThreadScope)
  104. {
  105. case UnityThreadScope.LateUpdate :
  106. UnityThread.executeInLateUpdate(action);
  107. break;
  108. case UnityThreadScope.FixedUpdate :
  109. UnityThread.executeInFixedUpdate(action);
  110. break;
  111. default :
  112. UnityThread.executeInUpdate(action);
  113. break;
  114. }
  115. }
  116. }