WebSocketBaseImplementation.cs 3.2 KB

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