WChannel_WebGL.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #if UNITY_WEBGL
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Net;
  6. using System.Runtime.InteropServices.ComTypes;
  7. using BestHTTP.WebSocket;
  8. using GFGGame;
  9. namespace ET
  10. {
  11. public class WChannel : AChannel
  12. {
  13. private readonly WService Service;
  14. private WebSocket webSocket;
  15. private Queue<MemoryStream> waitSend = new Queue<MemoryStream>();
  16. //address=http://webgltest.goufuguiwxw.com/ws
  17. public WChannel(long id, IPEndPoint ipEndPoint, WService service, string address)
  18. {
  19. this.Service = service;
  20. this.ChannelType = ChannelType.Connect;
  21. this.Id = id;
  22. string wsStr;
  23. bool isHttps = LauncherConfig.isHttps;
  24. // 检查address是否是URL格式
  25. if (!string.IsNullOrEmpty(address) && (address.StartsWith("http://") || address.StartsWith("https://")))
  26. {
  27. // 如果是URL格式,转换为WebSocket协议
  28. if (address.StartsWith("https://"))
  29. {
  30. wsStr = address.Replace("https://", "wss://");
  31. isHttps = true;
  32. }
  33. else
  34. {
  35. wsStr = address.Replace("http://", "ws://");
  36. }
  37. }
  38. else if (ipEndPoint != null)
  39. {
  40. // 如果不是URL格式但有IPEndPoint,使用IP+端口方式
  41. wsStr = $"ws://{ipEndPoint}";
  42. }
  43. else if (!string.IsNullOrEmpty(address))
  44. {
  45. // 如果没有IPEndPoint但有address,直接使用address作为WebSocket地址
  46. // 支持IP:端口格式
  47. wsStr = address.StartsWith("ws://") || address.StartsWith("wss://") ? address : $"ws://{address}";
  48. }
  49. else
  50. {
  51. // 无法构造WebSocket地址
  52. Log.Error($"WChannel: Invalid address and ipEndPoint are both null");
  53. throw new ArgumentException("Invalid address and ipEndPoint are both null");
  54. }
  55. Log.Debug($"wc address:{address}");
  56. Log.Debug($"wc wsStr:{wsStr}");
  57. WebSocket ws = new WebSocket(new Uri(wsStr));
  58. if (isHttps || ipEndPoint == null)
  59. {
  60. this.RemoteAddress = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 0);
  61. }
  62. else
  63. {
  64. this.RemoteAddress = ipEndPoint;
  65. }
  66. // Subscribe to the WS events
  67. ws.OnOpen += OnOpen;
  68. ws.OnClosed += OnClosed;
  69. ws.OnError += OnError;
  70. ws.OnBinary += OnRead;
  71. // Start connecting to the server
  72. ws.Open();
  73. }
  74. public override void Dispose()
  75. {
  76. if (this.IsDisposed)
  77. {
  78. return;
  79. }
  80. this.Id = 0;
  81. this.webSocket?.Close();
  82. this.webSocket = null;
  83. }
  84. public void Send(MemoryStream memoryBuffer)
  85. {
  86. switch (this.Service.ServiceType)
  87. {
  88. case ServiceType.Inner:
  89. break;
  90. case ServiceType.Outer:
  91. memoryBuffer.Seek(Packet.ActorIdLength, SeekOrigin.Begin);
  92. ;
  93. break;
  94. }
  95. if (this.webSocket == null)
  96. {
  97. this.waitSend.Enqueue(memoryBuffer);
  98. return;
  99. }
  100. SendOne(memoryBuffer);
  101. }
  102. private void SendOne(MemoryStream memoryBuffer)
  103. {
  104. this.webSocket.Send(memoryBuffer.GetBuffer(), (ulong)memoryBuffer.Position,
  105. (ulong)(memoryBuffer.Length - memoryBuffer.Position));
  106. }
  107. private void OnOpen(WebSocket ws)
  108. {
  109. if (ws == null)
  110. {
  111. this.OnError(ErrorCore.ERR_WebsocketConnectError);
  112. return;
  113. }
  114. if (this.IsDisposed)
  115. {
  116. return;
  117. }
  118. this.webSocket = ws;
  119. while (this.waitSend.Count > 0)
  120. {
  121. MemoryStream memoryBuffer = this.waitSend.Dequeue();
  122. this.SendOne(memoryBuffer);
  123. }
  124. }
  125. /// <summary>
  126. /// Called when we received a text message from the server
  127. /// </summary>
  128. private void OnRead(WebSocket ws, byte[] data)
  129. {
  130. if (this.IsDisposed)
  131. {
  132. return;
  133. }
  134. MemoryStream memoryBuffer = this.Service.Fetch();
  135. memoryBuffer.Write(data, 0, data.Length); // 写入整个 data
  136. memoryBuffer.Seek(2, SeekOrigin.Begin);
  137. this.Service.ReadCallback(this.Id, memoryBuffer);
  138. }
  139. /// <summary>
  140. /// Called when the web socket closed
  141. /// </summary>
  142. private void OnClosed(WebSocket ws, UInt16 code, string message)
  143. {
  144. if (this.IsDisposed)
  145. {
  146. return;
  147. }
  148. Log.Error($"wchannel closed: {code} {message}");
  149. this.OnError(0);
  150. }
  151. /// <summary>
  152. /// Called when an error occured on client side
  153. /// </summary>
  154. private void OnError(WebSocket ws, string error)
  155. {
  156. if (this.IsDisposed)
  157. {
  158. return;
  159. }
  160. //Log.Error($"WChannel error: {this.Id} {ws.GetHashCode()} {error}");
  161. this.OnError(ErrorCore.ERR_WebsocketError);
  162. }
  163. private void OnError(int error)
  164. {
  165. Log.Info($"WChannel error: {this.Id} {error}");
  166. long channelId = this.Id;
  167. this.Service.Remove(channelId);
  168. this.Service.ErrorCallback(channelId, error);
  169. }
  170. }
  171. }
  172. #endif