diff --git a/Userland/Libraries/LibWeb/WebSockets/WebSocket.cpp b/Userland/Libraries/LibWeb/WebSockets/WebSocket.cpp index 05b9432814..6814ac7a5a 100644 --- a/Userland/Libraries/LibWeb/WebSockets/WebSocket.cpp +++ b/Userland/Libraries/LibWeb/WebSockets/WebSocket.cpp @@ -11,12 +11,14 @@ #include #include #include +#include #include #include #include #include #include #include +#include #include #include #include @@ -185,13 +187,30 @@ WebIDL::ExceptionOr WebSocket::close(Optional code, Optional WebSocket::send(DeprecatedString const& data) +WebIDL::ExceptionOr WebSocket::send(Variant, JS::Handle, DeprecatedString> const& data) { auto state = ready_state(); if (state == WebSocket::ReadyState::Connecting) return WebIDL::InvalidStateError::create(realm(), "Websocket is still CONNECTING"); if (state == WebSocket::ReadyState::Open) { - m_websocket->send(data); + TRY_OR_THROW_OOM(vm(), + data.visit( + [this](DeprecatedString const& string) -> ErrorOr { + m_websocket->send(string); + return {}; + }, + [this](JS::Handle const& buffer_source) -> ErrorOr { + // FIXME: While the spec doesn't say to do this, it's not observable except from potentially throwing OOM. + // Can we avoid this copy? + auto data_buffer = TRY(WebIDL::get_buffer_source_copy(*buffer_source.cell())); + m_websocket->send(data_buffer, false); + return {}; + }, + [this](JS::Handle const& blob) -> ErrorOr { + auto byte_buffer = TRY(ByteBuffer::copy(blob->bytes())); + m_websocket->send(byte_buffer, false); + return {}; + })); // TODO : If the data cannot be sent, e.g. because it would need to be buffered but the buffer is full, the user agent must flag the WebSocket as full and then close the WebSocket connection. // TODO : Any invocation of this method with a string argument that does not throw an exception must increase the bufferedAmount attribute by the number of bytes needed to express the argument as UTF-8. } diff --git a/Userland/Libraries/LibWeb/WebSockets/WebSocket.h b/Userland/Libraries/LibWeb/WebSockets/WebSocket.h index 40b3d185a4..31b5b4050a 100644 --- a/Userland/Libraries/LibWeb/WebSockets/WebSocket.h +++ b/Userland/Libraries/LibWeb/WebSockets/WebSocket.h @@ -58,7 +58,7 @@ public: void set_binary_type(DeprecatedString const& type) { m_binary_type = type; }; WebIDL::ExceptionOr close(Optional code, Optional reason); - WebIDL::ExceptionOr send(DeprecatedString const& data); + WebIDL::ExceptionOr send(Variant, JS::Handle, DeprecatedString> const& data); private: void on_open(); diff --git a/Userland/Libraries/LibWeb/WebSockets/WebSocket.idl b/Userland/Libraries/LibWeb/WebSockets/WebSocket.idl index c857e07763..97ce64abad 100644 --- a/Userland/Libraries/LibWeb/WebSockets/WebSocket.idl +++ b/Userland/Libraries/LibWeb/WebSockets/WebSocket.idl @@ -25,9 +25,5 @@ interface WebSocket : EventTarget { attribute EventHandler onmessage; attribute DOMString binaryType; - undefined send(USVString data); - // FIXME: Support other kinds of send() calls - // undefined send(Blob data); - // undefined send(ArrayBuffer data); - // undefined send(ArrayBufferView data); + undefined send((BufferSource or Blob or USVString) data); };