WebSocket.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2021, Dex♪ <dexes.ttp@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Span.h>
  8. #include <LibCore/Object.h>
  9. #include <LibWebSocket/ConnectionInfo.h>
  10. #include <LibWebSocket/Impl/AbstractWebSocketImpl.h>
  11. #include <LibWebSocket/Message.h>
  12. namespace WebSocket {
  13. enum class ReadyState {
  14. Connecting = 0,
  15. Open = 1,
  16. Closing = 2,
  17. Closed = 3,
  18. };
  19. class WebSocket final : public Core::Object {
  20. C_OBJECT(WebSocket)
  21. public:
  22. static NonnullRefPtr<WebSocket> create(ConnectionInfo);
  23. virtual ~WebSocket() override;
  24. URL const& url() const { return m_connection.url(); }
  25. ReadyState ready_state();
  26. // Call this to start the WebSocket connection.
  27. void start();
  28. // This can only be used if the `ready_state` is `ReadyState::Open`
  29. void send(Message const&);
  30. // This can only be used if the `ready_state` is `ReadyState::Open`
  31. void close(u16 code = 1005, String const& reason = {});
  32. Function<void()> on_open;
  33. Function<void(u16 code, String reason, bool was_clean)> on_close;
  34. Function<void(Message message)> on_message;
  35. enum class Error {
  36. CouldNotEstablishConnection,
  37. ConnectionUpgradeFailed,
  38. ServerClosedSocket,
  39. };
  40. Function<void(Error)> on_error;
  41. private:
  42. explicit WebSocket(ConnectionInfo);
  43. // As defined in section 5.2
  44. enum class OpCode : u8 {
  45. Continuation = 0x0,
  46. Text = 0x1,
  47. Binary = 0x2,
  48. ConnectionClose = 0x8,
  49. Ping = 0x9,
  50. Pong = 0xA,
  51. };
  52. void drain_read();
  53. void send_client_handshake();
  54. void read_server_handshake();
  55. void read_frame();
  56. void send_frame(OpCode, ReadonlyBytes, bool is_final);
  57. void notify_open();
  58. void notify_close(u16 code, String reason, bool was_clean);
  59. void notify_error(Error);
  60. void notify_message(Message);
  61. void fatal_error(Error);
  62. void discard_connection();
  63. enum class InternalState {
  64. NotStarted,
  65. EstablishingProtocolConnection,
  66. SendingClientHandshake,
  67. WaitingForServerHandshake,
  68. Open,
  69. Closing,
  70. Closed,
  71. Errored,
  72. };
  73. InternalState m_state { InternalState::NotStarted };
  74. String m_websocket_key;
  75. bool m_has_read_server_handshake_first_line { false };
  76. bool m_has_read_server_handshake_upgrade { false };
  77. bool m_has_read_server_handshake_connection { false };
  78. bool m_has_read_server_handshake_accept { false };
  79. u16 m_last_close_code { 1005 };
  80. String m_last_close_message;
  81. ConnectionInfo m_connection;
  82. RefPtr<AbstractWebSocketImpl> m_impl;
  83. };
  84. }