WebSocketBaseImplementation.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #if !BESTHTTP_DISABLE_WEBSOCKET
  2. using System;
  3. #if !UNITY_WEBGL || UNITY_EDITOR
  4. using BestHTTP.WebSocket.Frames;
  5. #endif
  6. namespace BestHTTP.WebSocket
  7. {
  8. /// <summary>
  9. /// States of the underlying implementation's state.
  10. /// </summary>
  11. public enum WebSocketStates : byte
  12. {
  13. Connecting = 0,
  14. Open = 1,
  15. Closing = 2,
  16. Closed = 3,
  17. Unknown
  18. };
  19. public delegate void OnWebSocketOpenDelegate(WebSocket webSocket);
  20. public delegate void OnWebSocketMessageDelegate(WebSocket webSocket, string message);
  21. public delegate void OnWebSocketBinaryDelegate(WebSocket webSocket, byte[] data);
  22. public delegate void OnWebSocketClosedDelegate(WebSocket webSocket, UInt16 code, string message);
  23. public delegate void OnWebSocketErrorDelegate(WebSocket webSocket, string reason);
  24. #if !UNITY_WEBGL || UNITY_EDITOR
  25. public delegate void OnWebSocketIncompleteFrameDelegate(WebSocket webSocket, WebSocketFrameReader frame);
  26. #endif
  27. public abstract class WebSocketBaseImplementation
  28. {
  29. public virtual WebSocketStates State { get; protected set; }
  30. public virtual bool IsOpen { get; protected set; }
  31. public virtual int BufferedAmount { get; protected set; }
  32. #if !UNITY_WEBGL || UNITY_EDITOR
  33. public HTTPRequest InternalRequest
  34. {
  35. get
  36. {
  37. if (this._internalRequest == null)
  38. CreateInternalRequest();
  39. return this._internalRequest;
  40. }
  41. }
  42. protected HTTPRequest _internalRequest;
  43. public virtual int Latency { get; protected set; }
  44. public virtual DateTime LastMessageReceived { get; protected set; }
  45. #endif
  46. public WebSocket Parent { get; }
  47. public Uri Uri { get; protected set; }
  48. public string Origin { get; }
  49. public string Protocol { get; }
  50. public WebSocketBaseImplementation(WebSocket parent, Uri uri, string origin, string protocol)
  51. {
  52. this.Parent = parent;
  53. this.Uri = uri;
  54. this.Origin = origin;
  55. this.Protocol = protocol;
  56. #if !UNITY_WEBGL || UNITY_EDITOR
  57. this.LastMessageReceived = DateTime.MinValue;
  58. // Set up some default values.
  59. this.Parent.PingFrequency = 1000;
  60. this.Parent.CloseAfterNoMessage = TimeSpan.FromSeconds(2);
  61. #endif
  62. }
  63. public abstract void StartOpen();
  64. public abstract void StartClose(UInt16 code, string message);
  65. public abstract void Send(string message);
  66. public abstract void Send(byte[] buffer);
  67. public abstract void Send(byte[] buffer, ulong offset, ulong count);
  68. #if !UNITY_WEBGL || UNITY_EDITOR
  69. protected abstract void CreateInternalRequest();
  70. /// <summary>
  71. /// It will send the given frame to the server.
  72. /// </summary>
  73. public abstract void Send(WebSocketFrame frame);
  74. #endif
  75. }
  76. }
  77. #endif