1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 08:08:12 +00:00

LibWebSocket: Allow library clients to provide their own WebSocketImpl

This commit is contained in:
Andreas Kling 2022-11-08 19:17:29 +01:00
parent 8e50809f20
commit 9bbad08546
2 changed files with 9 additions and 7 deletions

View file

@ -17,21 +17,23 @@ namespace WebSocket {
// Note : The websocket protocol is defined by RFC 6455, found at https://tools.ietf.org/html/rfc6455 // Note : The websocket protocol is defined by RFC 6455, found at https://tools.ietf.org/html/rfc6455
// In this file, section numbers will refer to the RFC 6455 // In this file, section numbers will refer to the RFC 6455
NonnullRefPtr<WebSocket> WebSocket::create(ConnectionInfo connection) NonnullRefPtr<WebSocket> WebSocket::create(ConnectionInfo connection, RefPtr<WebSocketImpl> impl)
{ {
return adopt_ref(*new WebSocket(move(connection))); return adopt_ref(*new WebSocket(move(connection), move(impl)));
} }
WebSocket::WebSocket(ConnectionInfo connection) WebSocket::WebSocket(ConnectionInfo connection, RefPtr<WebSocketImpl> impl)
: m_connection(move(connection)) : m_connection(move(connection))
, m_impl(move(impl))
{ {
} }
void WebSocket::start() void WebSocket::start()
{ {
VERIFY(m_state == WebSocket::InternalState::NotStarted); VERIFY(m_state == WebSocket::InternalState::NotStarted);
VERIFY(!m_impl);
m_impl = adopt_ref(*new WebSocketImplSerenity); if (!m_impl)
m_impl = adopt_ref(*new WebSocketImplSerenity);
m_impl->on_connection_error = [this] { m_impl->on_connection_error = [this] {
dbgln("WebSocket: Connection error (underlying socket)"); dbgln("WebSocket: Connection error (underlying socket)");

View file

@ -25,7 +25,7 @@ enum class ReadyState {
class WebSocket final : public Core::Object { class WebSocket final : public Core::Object {
C_OBJECT(WebSocket) C_OBJECT(WebSocket)
public: public:
static NonnullRefPtr<WebSocket> create(ConnectionInfo); static NonnullRefPtr<WebSocket> create(ConnectionInfo, RefPtr<WebSocketImpl> = nullptr);
virtual ~WebSocket() override = default; virtual ~WebSocket() override = default;
URL const& url() const { return m_connection.url(); } URL const& url() const { return m_connection.url(); }
@ -54,7 +54,7 @@ public:
Function<void(Error)> on_error; Function<void(Error)> on_error;
private: private:
explicit WebSocket(ConnectionInfo); WebSocket(ConnectionInfo, RefPtr<WebSocketImpl>);
// As defined in section 5.2 // As defined in section 5.2
enum class OpCode : u8 { enum class OpCode : u8 {