WChannel_WebGL.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 = $"ws://{ipEndPoint}";
  23. Log.Debug($"wc address:{address}");
  24. if (LauncherConfig.isHttps)
  25. {
  26. Uri uri = new Uri(address);
  27. string hostAndPath = uri.Host + uri.AbsolutePath;
  28. wsStr = $"wss://{hostAndPath}";
  29. }
  30. Log.Debug($"wc wsStr:{wsStr}");
  31. WebSocket ws = new WebSocket(new Uri(wsStr));
  32. if (LauncherConfig.isHttps)
  33. {
  34. this.RemoteAddress = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 0);
  35. }
  36. else
  37. {
  38. this.RemoteAddress = ipEndPoint;
  39. }
  40. // Subscribe to the WS events
  41. ws.OnOpen += OnOpen;
  42. ws.OnClosed += OnClosed;
  43. ws.OnError += OnError;
  44. ws.OnBinary += OnRead;
  45. // Start connecting to the server
  46. ws.Open();
  47. }
  48. public override void Dispose()
  49. {
  50. if (this.IsDisposed)
  51. {
  52. return;
  53. }
  54. this.Id = 0;
  55. this.webSocket?.Close();
  56. this.webSocket = null;
  57. }
  58. public void Send(MemoryStream memoryBuffer)
  59. {
  60. switch (this.Service.ServiceType)
  61. {
  62. case ServiceType.Inner:
  63. break;
  64. case ServiceType.Outer:
  65. memoryBuffer.Seek(Packet.ActorIdLength, SeekOrigin.Begin);
  66. ;
  67. break;
  68. }
  69. if (this.webSocket == null)
  70. {
  71. this.waitSend.Enqueue(memoryBuffer);
  72. return;
  73. }
  74. SendOne(memoryBuffer);
  75. }
  76. private void SendOne(MemoryStream memoryBuffer)
  77. {
  78. this.webSocket.Send(memoryBuffer.GetBuffer(), (ulong)memoryBuffer.Position,
  79. (ulong)(memoryBuffer.Length - memoryBuffer.Position));
  80. }
  81. private void OnOpen(WebSocket ws)
  82. {
  83. if (ws == null)
  84. {
  85. this.OnError(ErrorCore.ERR_WebsocketConnectError);
  86. return;
  87. }
  88. if (this.IsDisposed)
  89. {
  90. return;
  91. }
  92. this.webSocket = ws;
  93. while (this.waitSend.Count > 0)
  94. {
  95. MemoryStream memoryBuffer = this.waitSend.Dequeue();
  96. this.SendOne(memoryBuffer);
  97. }
  98. }
  99. /// <summary>
  100. /// Called when we received a text message from the server
  101. /// </summary>
  102. private void OnRead(WebSocket ws, byte[] data)
  103. {
  104. if (this.IsDisposed)
  105. {
  106. return;
  107. }
  108. MemoryStream memoryBuffer = this.Service.Fetch();
  109. memoryBuffer.Write(data, 0, data.Length); // 写入整个 data
  110. memoryBuffer.Seek(2, SeekOrigin.Begin);
  111. this.Service.ReadCallback(this.Id, memoryBuffer);
  112. }
  113. /// <summary>
  114. /// Called when the web socket closed
  115. /// </summary>
  116. private void OnClosed(WebSocket ws, UInt16 code, string message)
  117. {
  118. if (this.IsDisposed)
  119. {
  120. return;
  121. }
  122. Log.Error($"wchannel closed: {code} {message}");
  123. this.OnError(0);
  124. }
  125. /// <summary>
  126. /// Called when an error occured on client side
  127. /// </summary>
  128. private void OnError(WebSocket ws, string error)
  129. {
  130. if (this.IsDisposed)
  131. {
  132. return;
  133. }
  134. //Log.Error($"WChannel error: {this.Id} {ws.GetHashCode()} {error}");
  135. this.OnError(ErrorCore.ERR_WebsocketError);
  136. }
  137. private void OnError(int error)
  138. {
  139. Log.Info($"WChannel error: {this.Id} {error}");
  140. long channelId = this.Id;
  141. this.Service.Remove(channelId);
  142. this.Service.ErrorCallback(channelId, error);
  143. }
  144. }
  145. }
  146. #endif