WebSocket.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/ByteBuffer.h>
  8. #include <AK/RefCounted.h>
  9. #include <AK/URL.h>
  10. #include <AK/Weakable.h>
  11. #include <LibCore/Object.h>
  12. #include <LibWeb/Bindings/WindowObject.h>
  13. #include <LibWeb/Bindings/Wrappable.h>
  14. #include <LibWeb/DOM/EventTarget.h>
  15. #include <LibWeb/DOM/ExceptionOr.h>
  16. #include <LibWeb/Forward.h>
  17. #define ENUMERATE_WEBSOCKET_EVENT_HANDLERS(E) \
  18. E(onerror, HTML::EventNames::error) \
  19. E(onclose, HTML::EventNames::close) \
  20. E(onopen, HTML::EventNames::open) \
  21. E(onmessage, HTML::EventNames::message)
  22. namespace Protocol {
  23. class WebSocketClient;
  24. class WebSocket;
  25. }
  26. namespace Web::WebSockets {
  27. class WebSocketClientManager : public Core::Object {
  28. C_OBJECT_ABSTRACT(WebSocketClientManager)
  29. public:
  30. static WebSocketClientManager& the();
  31. RefPtr<Protocol::WebSocket> connect(const AK::URL&, String const& origin);
  32. private:
  33. static ErrorOr<NonnullRefPtr<WebSocketClientManager>> try_create();
  34. WebSocketClientManager(NonnullRefPtr<Protocol::WebSocketClient>);
  35. RefPtr<Protocol::WebSocketClient> m_websocket_client;
  36. };
  37. class WebSocket final
  38. : public RefCounted<WebSocket>
  39. , public Weakable<WebSocket>
  40. , public DOM::EventTarget
  41. , public Bindings::Wrappable {
  42. public:
  43. enum class ReadyState : u16 {
  44. Connecting = 0,
  45. Open = 1,
  46. Closing = 2,
  47. Closed = 3,
  48. };
  49. using WrapperType = Bindings::WebSocketWrapper;
  50. static NonnullRefPtr<WebSocket> create(HTML::Window& window, AK::URL& url)
  51. {
  52. return adopt_ref(*new WebSocket(window, url));
  53. }
  54. static DOM::ExceptionOr<NonnullRefPtr<WebSocket>> create_with_global_object(Bindings::WindowObject& window, const String& url);
  55. virtual ~WebSocket() override;
  56. using RefCounted::ref;
  57. using RefCounted::unref;
  58. String url() const { return m_url.to_string(); }
  59. #undef __ENUMERATE
  60. #define __ENUMERATE(attribute_name, event_name) \
  61. void set_##attribute_name(Optional<Bindings::CallbackType>); \
  62. Bindings::CallbackType* attribute_name();
  63. ENUMERATE_WEBSOCKET_EVENT_HANDLERS(__ENUMERATE)
  64. #undef __ENUMERATE
  65. ReadyState ready_state() const;
  66. String extensions() const;
  67. String protocol() const;
  68. const String& binary_type() { return m_binary_type; };
  69. void set_binary_type(const String& type) { m_binary_type = type; };
  70. DOM::ExceptionOr<void> close(u16 code, const String& reason);
  71. DOM::ExceptionOr<void> send(const String& data);
  72. private:
  73. virtual void ref_event_target() override { ref(); }
  74. virtual void unref_event_target() override { unref(); }
  75. virtual JS::Object* create_wrapper(JS::GlobalObject&) override;
  76. void on_open();
  77. void on_message(ByteBuffer message, bool is_text);
  78. void on_error();
  79. void on_close(u16 code, String reason, bool was_clean);
  80. explicit WebSocket(HTML::Window&, AK::URL&);
  81. NonnullRefPtr<HTML::Window> m_window;
  82. AK::URL m_url;
  83. String m_binary_type { "blob" };
  84. RefPtr<Protocol::WebSocket> m_websocket;
  85. };
  86. }