瀏覽代碼

LibWebSocket: Allow library clients to provide their own WebSocketImpl

Andreas Kling 2 年之前
父節點
當前提交
9bbad08546
共有 2 個文件被更改,包括 9 次插入7 次删除
  1. 7 5
      Userland/Libraries/LibWebSocket/WebSocket.cpp
  2. 2 2
      Userland/Libraries/LibWebSocket/WebSocket.h

+ 7 - 5
Userland/Libraries/LibWebSocket/WebSocket.cpp

@@ -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)");

+ 2 - 2
Userland/Libraries/LibWebSocket/WebSocket.h

@@ -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 {