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
Notes: sideshowbarker 2024-07-17 04:39:31 +09:00
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
// 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_impl(move(impl))
{
}
void WebSocket::start()
{
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] {
dbgln("WebSocket: Connection error (underlying socket)");

View File

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