WebSocket.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. #if !BESTHTTP_DISABLE_WEBSOCKET
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.IO;
  6. using BestHTTP.Extensions;
  7. #if UNITY_WEBGL && !UNITY_EDITOR
  8. using System.Runtime.InteropServices;
  9. #else
  10. using BestHTTP.WebSocket.Frames;
  11. using BestHTTP.WebSocket.Extensions;
  12. #endif
  13. namespace BestHTTP.WebSocket
  14. {
  15. /// <summary>
  16. /// States of the underlying browser's WebSocket implementation's state.
  17. /// </summary>
  18. public enum WebSocketStates : byte
  19. {
  20. Connecting = 0,
  21. Open = 1,
  22. Closing = 2,
  23. Closed = 3,
  24. Unknown
  25. };
  26. public delegate void OnWebSocketOpenDelegate(WebSocket webSocket);
  27. public delegate void OnWebSocketMessageDelegate(WebSocket webSocket, string message);
  28. public delegate void OnWebSocketBinaryDelegate(WebSocket webSocket, byte[] data);
  29. public delegate void OnWebSocketClosedDelegate(WebSocket webSocket, UInt16 code, string message);
  30. public delegate void OnWebSocketErrorDelegate(WebSocket webSocket, Exception ex);
  31. public delegate void OnWebSocketErrorDescriptionDelegate(WebSocket webSocket, string reason);
  32. #if (!UNITY_WEBGL || UNITY_EDITOR)
  33. public delegate void OnWebSocketIncompleteFrameDelegate(WebSocket webSocket, WebSocketFrameReader frame);
  34. #else
  35. delegate void OnWebGLWebSocketOpenDelegate(uint id);
  36. delegate void OnWebGLWebSocketTextDelegate(uint id, string text);
  37. delegate void OnWebGLWebSocketBinaryDelegate(uint id, IntPtr pBuffer, int length);
  38. delegate void OnWebGLWebSocketErrorDelegate(uint id, string error);
  39. delegate void OnWebGLWebSocketCloseDelegate(uint id, int code, string reason);
  40. #endif
  41. public sealed class WebSocket
  42. {
  43. #region Properties
  44. #if !UNITY_WEBGL || UNITY_EDITOR
  45. public WebSocketStates State { get; private set; }
  46. #else
  47. public WebSocketStates State { get { return ImplementationId != 0 ? WS_GetState(ImplementationId) : WebSocketStates.Unknown; } }
  48. #endif
  49. /// <summary>
  50. /// The connection to the WebSocket server is open.
  51. /// </summary>
  52. public bool IsOpen
  53. {
  54. get
  55. {
  56. #if (!UNITY_WEBGL || UNITY_EDITOR)
  57. return webSocket != null && !webSocket.IsClosed;
  58. #else
  59. return ImplementationId != 0 && WS_GetState(ImplementationId) == WebSocketStates.Open;
  60. #endif
  61. }
  62. }
  63. public int BufferedAmount
  64. {
  65. get
  66. {
  67. #if (!UNITY_WEBGL || UNITY_EDITOR)
  68. return webSocket.BufferedAmount;
  69. #else
  70. return WS_GetBufferedAmount(ImplementationId);
  71. #endif
  72. }
  73. }
  74. #if (!UNITY_WEBGL || UNITY_EDITOR)
  75. /// <summary>
  76. /// Set to true to start a new thread to send Pings to the WebSocket server
  77. /// </summary>
  78. public bool StartPingThread { get; set; }
  79. /// <summary>
  80. /// The delay between two Pings in millisecs. Minimum value is 100, default is 1000.
  81. /// </summary>
  82. public int PingFrequency { get; set; }
  83. /// <summary>
  84. /// If StartPingThread set to true, the plugin will close the connection and emit an OnError/OnErrorDesc event if no
  85. /// message is received from the server in the given time. Its default value is 10 sec.
  86. /// </summary>
  87. public TimeSpan CloseAfterNoMesssage { get; set; }
  88. /// <summary>
  89. /// The internal HTTPRequest object.
  90. /// </summary>
  91. public HTTPRequest InternalRequest { get; private set; }
  92. /// <summary>
  93. /// IExtension implementations the plugin will negotiate with the server to use.
  94. /// </summary>
  95. public IExtension[] Extensions { get; private set; }
  96. /// <summary>
  97. /// Latency calculated from the ping-pong message round-trip times.
  98. /// </summary>
  99. public int Latency { get { return webSocket.Latency; } }
  100. #endif
  101. /// <summary>
  102. /// Called when the connection to the WebSocket server is established.
  103. /// </summary>
  104. public OnWebSocketOpenDelegate OnOpen;
  105. /// <summary>
  106. /// Called when a new textual message is received from the server.
  107. /// </summary>
  108. public OnWebSocketMessageDelegate OnMessage;
  109. /// <summary>
  110. /// Called when a new binary message is received from the server.
  111. /// </summary>
  112. public OnWebSocketBinaryDelegate OnBinary;
  113. /// <summary>
  114. /// Called when the WebSocket connection is closed.
  115. /// </summary>
  116. public OnWebSocketClosedDelegate OnClosed;
  117. /// <summary>
  118. /// Called when an error is encountered. The Exception parameter may be null.
  119. /// </summary>
  120. public OnWebSocketErrorDelegate OnError;
  121. /// <summary>
  122. /// Called when an error is encountered. The parameter will be the description of the error.
  123. /// </summary>
  124. public OnWebSocketErrorDescriptionDelegate OnErrorDesc;
  125. #if (!UNITY_WEBGL || UNITY_EDITOR)
  126. /// <summary>
  127. /// Called when an incomplete frame received. No attempt will be made to reassemble these fragments internally, and no reference are stored after this event to this frame.
  128. /// </summary>
  129. public OnWebSocketIncompleteFrameDelegate OnIncompleteFrame;
  130. #endif
  131. #endregion
  132. #region Private Fields
  133. #if (!UNITY_WEBGL || UNITY_EDITOR)
  134. /// <summary>
  135. /// Indicates wheter we sent out the connection request to the server.
  136. /// </summary>
  137. private bool requestSent;
  138. /// <summary>
  139. /// The internal WebSocketResponse object
  140. /// </summary>
  141. private WebSocketResponse webSocket;
  142. #else
  143. internal static Dictionary<uint, WebSocket> WebSockets = new Dictionary<uint, WebSocket>();
  144. private uint ImplementationId;
  145. private Uri Uri;
  146. private string Protocol;
  147. #endif
  148. #endregion
  149. #region Constructors
  150. /// <summary>
  151. /// Creates a WebSocket instance from the given uri.
  152. /// </summary>
  153. /// <param name="uri">The uri of the WebSocket server</param>
  154. public WebSocket(Uri uri)
  155. :this(uri, string.Empty, string.Empty)
  156. {
  157. #if (!UNITY_WEBGL || UNITY_EDITOR) && !BESTHTTP_DISABLE_GZIP
  158. this.Extensions = new IExtension[] { new PerMessageCompression(/*compression level: */ Decompression.Zlib.CompressionLevel.Default,
  159. /*clientNoContextTakeover: */ false,
  160. /*serverNoContextTakeover: */ false,
  161. /*clientMaxWindowBits: */ Decompression.Zlib.ZlibConstants.WindowBitsMax,
  162. /*desiredServerMaxWindowBits: */ Decompression.Zlib.ZlibConstants.WindowBitsMax,
  163. /*minDatalengthToCompress: */ 5) };
  164. #endif
  165. }
  166. /// <summary>
  167. /// Creates a WebSocket instance from the given uri, protocol and origin.
  168. /// </summary>
  169. /// <param name="uri">The uri of the WebSocket server</param>
  170. /// <param name="origin">Servers that are not intended to process input from any web page but only for certain sites SHOULD verify the |Origin| field is an origin they expect.
  171. /// If the origin indicated is unacceptable to the server, then it SHOULD respond to the WebSocket handshake with a reply containing HTTP 403 Forbidden status code.</param>
  172. /// <param name="protocol">The application-level protocol that the client want to use(eg. "chat", "leaderboard", etc.). Can be null or empty string if not used.</param>
  173. /// <param name="extensions">Optional IExtensions implementations</param>
  174. public WebSocket(Uri uri, string origin, string protocol
  175. #if !UNITY_WEBGL || UNITY_EDITOR
  176. , params IExtension[] extensions
  177. #endif
  178. )
  179. {
  180. string scheme = HTTPProtocolFactory.IsSecureProtocol(uri) ? "wss" : "ws";
  181. int port = uri.Port != -1 ? uri.Port : (scheme.Equals("wss", StringComparison.OrdinalIgnoreCase) ? 443 : 80);
  182. // Somehow if i use the UriBuilder it's not the same as if the uri is constructed from a string...
  183. //uri = new UriBuilder(uri.Scheme, uri.Host, uri.Scheme.Equals("wss", StringComparison.OrdinalIgnoreCase) ? 443 : 80, uri.PathAndQuery).Uri;
  184. uri = new Uri(scheme + "://" + uri.Host + ":" + port + uri.GetRequestPathAndQueryURL());
  185. #if !UNITY_WEBGL || UNITY_EDITOR
  186. // Set up some default values.
  187. this.PingFrequency = 1000;
  188. this.CloseAfterNoMesssage = TimeSpan.FromSeconds(10);
  189. InternalRequest = new HTTPRequest(uri, OnInternalRequestCallback);
  190. // Called when the regular GET request is successfully upgraded to WebSocket
  191. InternalRequest.OnUpgraded = OnInternalRequestUpgraded;
  192. //http://tools.ietf.org/html/rfc6455#section-4
  193. //The request MUST contain a |Host| header field whose value contains /host/ plus optionally ":" followed by /port/ (when not using the default port).
  194. if (uri.Port != 80)
  195. InternalRequest.SetHeader("Host", uri.Host + ":" + uri.Port);
  196. else
  197. InternalRequest.SetHeader("Host", uri.Host);
  198. // The request MUST contain an |Upgrade| header field whose value MUST include the "websocket" keyword.
  199. InternalRequest.SetHeader("Upgrade", "websocket");
  200. // The request MUST contain a |Connection| header field whose value MUST include the "Upgrade" token.
  201. InternalRequest.SetHeader("Connection", "keep-alive, Upgrade");
  202. // The request MUST include a header field with the name |Sec-WebSocket-Key|. The value of this header field MUST be a nonce consisting of a
  203. // randomly selected 16-byte value that has been base64-encoded (see Section 4 of [RFC4648]). The nonce MUST be selected randomly for each connection.
  204. InternalRequest.SetHeader("Sec-WebSocket-Key", GetSecKey(new object[] { this, InternalRequest, uri, new object() }));
  205. // The request MUST include a header field with the name |Origin| [RFC6454] if the request is coming from a browser client.
  206. // If the connection is from a non-browser client, the request MAY include this header field if the semantics of that client match the use-case described here for browser clients.
  207. // More on Origin Considerations: http://tools.ietf.org/html/rfc6455#section-10.2
  208. if (!string.IsNullOrEmpty(origin))
  209. InternalRequest.SetHeader("Origin", origin);
  210. // The request MUST include a header field with the name |Sec-WebSocket-Version|. The value of this header field MUST be 13.
  211. InternalRequest.SetHeader("Sec-WebSocket-Version", "13");
  212. if (!string.IsNullOrEmpty(protocol))
  213. InternalRequest.SetHeader("Sec-WebSocket-Protocol", protocol);
  214. // Disable caching
  215. InternalRequest.SetHeader("Cache-Control", "no-cache");
  216. InternalRequest.SetHeader("Pragma", "no-cache");
  217. this.Extensions = extensions;
  218. #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
  219. InternalRequest.DisableCache = true;
  220. InternalRequest.DisableRetry = true;
  221. #endif
  222. InternalRequest.TryToMinimizeTCPLatency = true;
  223. #if !BESTHTTP_DISABLE_PROXY
  224. // WebSocket is not a request-response based protocol, so we need a 'tunnel' through the proxy
  225. if (HTTPManager.Proxy != null)
  226. InternalRequest.Proxy = new HTTPProxy(HTTPManager.Proxy.Address,
  227. HTTPManager.Proxy.Credentials,
  228. false, /*turn on 'tunneling'*/
  229. false, /*sendWholeUri*/
  230. HTTPManager.Proxy.NonTransparentForHTTPS);
  231. #endif
  232. #else
  233. this.Uri = uri;
  234. this.Protocol = protocol;
  235. #endif
  236. HTTPManager.Setup();
  237. }
  238. #endregion
  239. #region Request Callbacks
  240. #if (!UNITY_WEBGL || UNITY_EDITOR)
  241. private void OnInternalRequestCallback(HTTPRequest req, HTTPResponse resp)
  242. {
  243. string reason = string.Empty;
  244. switch (req.State)
  245. {
  246. case HTTPRequestStates.Finished:
  247. if (resp.IsSuccess || resp.StatusCode == 101)
  248. {
  249. // The request finished without any problem.
  250. HTTPManager.Logger.Information("WebSocket", string.Format("Request finished. Status Code: {0} Message: {1}", resp.StatusCode.ToString(), resp.Message));
  251. return;
  252. }
  253. else
  254. reason = string.Format("Request Finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2}",
  255. resp.StatusCode,
  256. resp.Message,
  257. resp.DataAsText);
  258. break;
  259. // The request finished with an unexpected error. The request's Exception property may contain more info about the error.
  260. case HTTPRequestStates.Error:
  261. reason = "Request Finished with Error! " + (req.Exception != null ? ("Exception: " + req.Exception.Message + req.Exception.StackTrace) : string.Empty);
  262. break;
  263. // The request aborted, initiated by the user.
  264. case HTTPRequestStates.Aborted:
  265. reason = "Request Aborted!";
  266. break;
  267. // Connecting to the server is timed out.
  268. case HTTPRequestStates.ConnectionTimedOut:
  269. reason = "Connection Timed Out!";
  270. break;
  271. // The request didn't finished in the given time.
  272. case HTTPRequestStates.TimedOut:
  273. reason = "Processing the request Timed Out!";
  274. break;
  275. default:
  276. return;
  277. }
  278. if (this.State != WebSocketStates.Connecting || !string.IsNullOrEmpty(reason))
  279. {
  280. if (OnError != null)
  281. OnError(this, req.Exception);
  282. if (OnErrorDesc != null)
  283. OnErrorDesc(this, reason);
  284. if (OnError == null && OnErrorDesc == null)
  285. HTTPManager.Logger.Error("WebSocket", reason);
  286. }
  287. else if (OnClosed != null)
  288. OnClosed(this, (ushort)WebSocketStausCodes.NormalClosure, "Closed while opening");
  289. if (!req.IsKeepAlive && resp != null && resp is WebSocketResponse)
  290. (resp as WebSocketResponse).CloseStream();
  291. }
  292. private void OnInternalRequestUpgraded(HTTPRequest req, HTTPResponse resp)
  293. {
  294. webSocket = resp as WebSocketResponse;
  295. if (webSocket == null)
  296. {
  297. if (OnError != null)
  298. OnError(this, req.Exception);
  299. if (OnErrorDesc != null)
  300. {
  301. string reason = string.Empty;
  302. if (req.Exception != null)
  303. reason = req.Exception.Message + " " + req.Exception.StackTrace;
  304. OnErrorDesc(this, reason);
  305. }
  306. this.State = WebSocketStates.Closed;
  307. return;
  308. }
  309. // If Close called while we connected
  310. if (this.State == WebSocketStates.Closed)
  311. {
  312. webSocket.CloseStream();
  313. return;
  314. }
  315. webSocket.WebSocket = this;
  316. if (this.Extensions != null)
  317. {
  318. for (int i = 0; i < this.Extensions.Length; ++i)
  319. {
  320. var ext = this.Extensions[i];
  321. try
  322. {
  323. if (ext != null && !ext.ParseNegotiation(webSocket))
  324. this.Extensions[i] = null; // Keep extensions only that successfully negotiated
  325. }
  326. catch (Exception ex)
  327. {
  328. HTTPManager.Logger.Exception("WebSocket", "ParseNegotiation", ex);
  329. // Do not try to use a defective extension in the future
  330. this.Extensions[i] = null;
  331. }
  332. }
  333. }
  334. this.State = WebSocketStates.Open;
  335. if (OnOpen != null)
  336. {
  337. try
  338. {
  339. OnOpen(this);
  340. }
  341. catch(Exception ex)
  342. {
  343. HTTPManager.Logger.Exception("WebSocket", "OnOpen", ex);
  344. }
  345. }
  346. webSocket.OnText = (ws, msg) =>
  347. {
  348. if (OnMessage != null)
  349. OnMessage(this, msg);
  350. };
  351. webSocket.OnBinary = (ws, bin) =>
  352. {
  353. if (OnBinary != null)
  354. OnBinary(this, bin);
  355. };
  356. webSocket.OnClosed = (ws, code, msg) =>
  357. {
  358. this.State = WebSocketStates.Closed;
  359. if (OnClosed != null)
  360. OnClosed(this, code, msg);
  361. };
  362. if (OnIncompleteFrame != null)
  363. webSocket.OnIncompleteFrame = (ws, frame) =>
  364. {
  365. if (OnIncompleteFrame != null)
  366. OnIncompleteFrame(this, frame);
  367. };
  368. if (StartPingThread)
  369. webSocket.StartPinging(Math.Max(PingFrequency, 100));
  370. webSocket.StartReceive();
  371. }
  372. #endif
  373. #endregion
  374. #region Public Interface
  375. /// <summary>
  376. /// Start the opening process.
  377. /// </summary>
  378. public void Open()
  379. {
  380. #if (!UNITY_WEBGL || UNITY_EDITOR)
  381. if (requestSent)
  382. throw new InvalidOperationException("Open already called! You can't reuse this WebSocket instance!");
  383. if (this.Extensions != null)
  384. {
  385. try
  386. {
  387. for (int i = 0; i < this.Extensions.Length; ++i)
  388. {
  389. var ext = this.Extensions[i];
  390. if (ext != null)
  391. ext.AddNegotiation(InternalRequest);
  392. }
  393. }
  394. catch(Exception ex)
  395. {
  396. HTTPManager.Logger.Exception("WebSocket", "Open", ex);
  397. }
  398. }
  399. InternalRequest.Send();
  400. requestSent = true;
  401. this.State = WebSocketStates.Connecting;
  402. #else
  403. try
  404. {
  405. ImplementationId = WS_Create(this.Uri.OriginalString, this.Protocol, OnOpenCallback, OnTextCallback, OnBinaryCallback, OnErrorCallback, OnCloseCallback);
  406. WebSockets.Add(ImplementationId, this);
  407. }
  408. catch(Exception ex)
  409. {
  410. HTTPManager.Logger.Exception("WebSocket", "Open", ex);
  411. }
  412. #endif
  413. }
  414. /// <summary>
  415. /// It will send the given message to the server in one frame.
  416. /// </summary>
  417. public void Send(string message)
  418. {
  419. if (!IsOpen)
  420. return;
  421. #if (!UNITY_WEBGL || UNITY_EDITOR)
  422. webSocket.Send(message);
  423. #else
  424. WS_Send_String(this.ImplementationId, message);
  425. #endif
  426. }
  427. /// <summary>
  428. /// It will send the given data to the server in one frame.
  429. /// </summary>
  430. public void Send(byte[] buffer)
  431. {
  432. if (!IsOpen)
  433. return;
  434. #if (!UNITY_WEBGL || UNITY_EDITOR)
  435. webSocket.Send(buffer);
  436. #else
  437. WS_Send_Binary(this.ImplementationId, buffer, 0, buffer.Length);
  438. #endif
  439. }
  440. /// <summary>
  441. /// Will send count bytes from a byte array, starting from offset.
  442. /// </summary>
  443. public void Send(byte[] buffer, ulong offset, ulong count)
  444. {
  445. if (!IsOpen)
  446. return;
  447. #if (!UNITY_WEBGL || UNITY_EDITOR)
  448. webSocket.Send(buffer, offset, count);
  449. #else
  450. WS_Send_Binary(this.ImplementationId, buffer, (int)offset, (int)count);
  451. #endif
  452. }
  453. #if (!UNITY_WEBGL || UNITY_EDITOR)
  454. /// <summary>
  455. /// It will send the given frame to the server.
  456. /// </summary>
  457. public void Send(WebSocketFrame frame)
  458. {
  459. if (IsOpen)
  460. webSocket.Send(frame);
  461. }
  462. #endif
  463. /// <summary>
  464. /// It will initiate the closing of the connection to the server.
  465. /// </summary>
  466. public void Close()
  467. {
  468. if (State >= WebSocketStates.Closing)
  469. return;
  470. #if !UNITY_WEBGL || UNITY_EDITOR
  471. if (this.State == WebSocketStates.Connecting)
  472. {
  473. this.State = WebSocketStates.Closed;
  474. if (OnClosed != null)
  475. OnClosed(this, (ushort)WebSocketStausCodes.NoStatusCode, string.Empty);
  476. }
  477. else
  478. {
  479. this.State = WebSocketStates.Closing;
  480. webSocket.Close();
  481. }
  482. #else
  483. WS_Close(this.ImplementationId, 1000, "Bye!");
  484. #endif
  485. }
  486. /// <summary>
  487. /// It will initiate the closing of the connection to the server sending the given code and message.
  488. /// </summary>
  489. public void Close(UInt16 code, string message)
  490. {
  491. if (!IsOpen)
  492. return;
  493. #if (!UNITY_WEBGL || UNITY_EDITOR)
  494. webSocket.Close(code, message);
  495. #else
  496. WS_Close(this.ImplementationId, code, message);
  497. #endif
  498. }
  499. public static byte[] EncodeCloseData(UInt16 code, string message)
  500. {
  501. //If there is a body, the first two bytes of the body MUST be a 2-byte unsigned integer
  502. // (in network byte order) representing a status code with value /code/ defined in Section 7.4 (http://tools.ietf.org/html/rfc6455#section-7.4). Following the 2-byte integer,
  503. // the body MAY contain UTF-8-encoded data with value /reason/, the interpretation of which is not defined by this specification.
  504. // This data is not necessarily human readable but may be useful for debugging or passing information relevant to the script that opened the connection.
  505. int msgLen = Encoding.UTF8.GetByteCount(message);
  506. using (MemoryStream ms = new MemoryStream(2 + msgLen))
  507. {
  508. byte[] buff = BitConverter.GetBytes(code);
  509. if (BitConverter.IsLittleEndian)
  510. Array.Reverse(buff, 0, buff.Length);
  511. ms.Write(buff, 0, buff.Length);
  512. buff = Encoding.UTF8.GetBytes(message);
  513. ms.Write(buff, 0, buff.Length);
  514. return ms.ToArray();
  515. }
  516. }
  517. #endregion
  518. #region Private Helpers
  519. #if !UNITY_WEBGL || UNITY_EDITOR
  520. private string GetSecKey(object[] from)
  521. {
  522. byte[] keys = new byte[16];
  523. int pos = 0;
  524. for (int i = 0; i < from.Length; ++i)
  525. {
  526. byte[] hash = BitConverter.GetBytes((Int32)from[i].GetHashCode());
  527. for (int cv = 0; cv < hash.Length && pos < keys.Length; ++cv)
  528. keys[pos++] = hash[cv];
  529. }
  530. return Convert.ToBase64String(keys);
  531. }
  532. #endif
  533. #endregion
  534. #region WebGL Static Callbacks
  535. #if UNITY_WEBGL && !UNITY_EDITOR
  536. [AOT.MonoPInvokeCallback(typeof(OnWebGLWebSocketOpenDelegate))]
  537. static void OnOpenCallback(uint id)
  538. {
  539. WebSocket ws;
  540. if (WebSockets.TryGetValue(id, out ws))
  541. {
  542. if (ws.OnOpen != null)
  543. {
  544. try
  545. {
  546. ws.OnOpen(ws);
  547. }
  548. catch(Exception ex)
  549. {
  550. HTTPManager.Logger.Exception("WebSocket", "OnOpen", ex);
  551. }
  552. }
  553. }
  554. else
  555. HTTPManager.Logger.Warning("WebSocket", "OnOpenCallback - No WebSocket found for id: " + id.ToString());
  556. }
  557. [AOT.MonoPInvokeCallback(typeof(OnWebGLWebSocketTextDelegate))]
  558. static void OnTextCallback(uint id, string text)
  559. {
  560. WebSocket ws;
  561. if (WebSockets.TryGetValue(id, out ws))
  562. {
  563. if (ws.OnMessage != null)
  564. {
  565. try
  566. {
  567. ws.OnMessage(ws, text);
  568. }
  569. catch (Exception ex)
  570. {
  571. HTTPManager.Logger.Exception("WebSocket", "OnMessage", ex);
  572. }
  573. }
  574. }
  575. else
  576. HTTPManager.Logger.Warning("WebSocket", "OnTextCallback - No WebSocket found for id: " + id.ToString());
  577. }
  578. [AOT.MonoPInvokeCallback(typeof(OnWebGLWebSocketBinaryDelegate))]
  579. static void OnBinaryCallback(uint id, IntPtr pBuffer, int length)
  580. {
  581. WebSocket ws;
  582. if (WebSockets.TryGetValue(id, out ws))
  583. {
  584. if (ws.OnBinary != null)
  585. {
  586. try
  587. {
  588. byte[] buffer = new byte[length];
  589. // Copy data from the 'unmanaged' memory to managed memory. Buffer will be reclaimed by the GC.
  590. Marshal.Copy(pBuffer, buffer, 0, length);
  591. ws.OnBinary(ws, buffer);
  592. }
  593. catch (Exception ex)
  594. {
  595. HTTPManager.Logger.Exception("WebSocket", "OnBinary", ex);
  596. }
  597. }
  598. }
  599. else
  600. HTTPManager.Logger.Warning("WebSocket", "OnBinaryCallback - No WebSocket found for id: " + id.ToString());
  601. }
  602. [AOT.MonoPInvokeCallback(typeof(OnWebGLWebSocketErrorDelegate))]
  603. static void OnErrorCallback(uint id, string error)
  604. {
  605. WebSocket ws;
  606. if (WebSockets.TryGetValue(id, out ws))
  607. {
  608. WebSockets.Remove(id);
  609. if (ws.OnError != null)
  610. {
  611. try
  612. {
  613. ws.OnError(ws, new Exception(error));
  614. }
  615. catch (Exception ex)
  616. {
  617. HTTPManager.Logger.Exception("WebSocket", "OnError", ex);
  618. }
  619. }
  620. if (ws.OnErrorDesc != null)
  621. {
  622. try
  623. {
  624. ws.OnErrorDesc(ws, error);
  625. }
  626. catch (Exception ex)
  627. {
  628. HTTPManager.Logger.Exception("WebSocket", "OnErrorDesc", ex);
  629. }
  630. }
  631. }
  632. else
  633. HTTPManager.Logger.Warning("WebSocket", "OnErrorCallback - No WebSocket found for id: " + id.ToString());
  634. try
  635. {
  636. WS_Release(id);
  637. }
  638. catch(Exception ex)
  639. {
  640. HTTPManager.Logger.Exception("WebSocket", "WS_Release", ex);
  641. }
  642. }
  643. [AOT.MonoPInvokeCallback(typeof(OnWebGLWebSocketCloseDelegate))]
  644. static void OnCloseCallback(uint id, int code, string reason)
  645. {
  646. WebSocket ws;
  647. if (WebSockets.TryGetValue(id, out ws))
  648. {
  649. WebSockets.Remove(id);
  650. if (ws.OnClosed != null)
  651. {
  652. try
  653. {
  654. ws.OnClosed(ws, (ushort)code, reason);
  655. }
  656. catch (Exception ex)
  657. {
  658. HTTPManager.Logger.Exception("WebSocket", "OnClosed", ex);
  659. }
  660. }
  661. }
  662. else
  663. HTTPManager.Logger.Warning("WebSocket", "OnCloseCallback - No WebSocket found for id: " + id.ToString());
  664. try
  665. {
  666. WS_Release(id);
  667. }
  668. catch(Exception ex)
  669. {
  670. HTTPManager.Logger.Exception("WebSocket", "WS_Release", ex);
  671. }
  672. }
  673. #endif
  674. #endregion
  675. #region WebGL Interface
  676. #if UNITY_WEBGL && !UNITY_EDITOR
  677. [DllImport("__Internal")]
  678. static extern uint WS_Create(string url, string protocol, OnWebGLWebSocketOpenDelegate onOpen, OnWebGLWebSocketTextDelegate onText, OnWebGLWebSocketBinaryDelegate onBinary, OnWebGLWebSocketErrorDelegate onError, OnWebGLWebSocketCloseDelegate onClose);
  679. [DllImport("__Internal")]
  680. static extern WebSocketStates WS_GetState(uint id);
  681. [DllImport("__Internal")]
  682. static extern int WS_GetBufferedAmount(uint id);
  683. [DllImport("__Internal")]
  684. static extern int WS_Send_String(uint id, string strData);
  685. [DllImport("__Internal")]
  686. static extern int WS_Send_Binary(uint id, byte[] buffer, int pos, int length);
  687. [DllImport("__Internal")]
  688. static extern void WS_Close(uint id, ushort code, string reason);
  689. [DllImport("__Internal")]
  690. static extern void WS_Release(uint id);
  691. #endif
  692. #endregion
  693. }
  694. }
  695. #endif