From d398943c37a82470ac9f279816e36c3ac7d39b7b Mon Sep 17 00:00:00 2001 From: DexesTTP Date: Sat, 24 Apr 2021 01:47:40 +0200 Subject: [PATCH] Utilities: Update telws to use the isolated WebSocket service --- Userland/Utilities/CMakeLists.txt | 2 +- Userland/Utilities/telws.cpp | 75 +++++++++++++++---------------- 2 files changed, 38 insertions(+), 39 deletions(-) diff --git a/Userland/Utilities/CMakeLists.txt b/Userland/Utilities/CMakeLists.txt index 3a839c0b26..dbdfd42856 100644 --- a/Userland/Utilities/CMakeLists.txt +++ b/Userland/Utilities/CMakeLists.txt @@ -42,7 +42,7 @@ target_link_libraries(shot LibGUI) target_link_libraries(sql LibLine LibSQL) target_link_libraries(su LibCrypt) target_link_libraries(tar LibArchive LibCompress) -target_link_libraries(telws LibCrypto LibTLS LibWebSocket LibLine) +target_link_libraries(telws LibProtocol LibLine) target_link_libraries(test-crypto LibCrypto LibTLS LibLine) target_link_libraries(test-fuzz LibCore LibGemini LibGfx LibHTTP LibIPC LibJS LibMarkdown LibShell) target_link_libraries(test-js LibJS LibLine LibCore) diff --git a/Userland/Utilities/telws.cpp b/Userland/Utilities/telws.cpp index b1803fd13a..dc1db6775b 100644 --- a/Userland/Utilities/telws.cpp +++ b/Userland/Utilities/telws.cpp @@ -12,9 +12,8 @@ #include #include #include -#include -#include -#include +#include +#include int main(int argc, char** argv) { @@ -43,6 +42,33 @@ int main(int argc, char** argv) Core::EventLoop loop; RefPtr editor = Line::Editor::construct(); bool should_quit = false; + auto websocket_client = Protocol::WebSocketClient::construct(); + auto socket = websocket_client->connect(url, origin); + if (!socket) { + warnln("Failed to start socket for '{}'\n", url); + return 1; + } + socket->on_open = [&]() { + outln("[WebSocket opened]"sv); + }; + socket->on_error = [&](auto error) { + outln("[WebSocket Error : {}]", (unsigned)error); + }; + socket->on_message = [&](auto message) { + if (!message.is_text) { + outln("[Received binary data : {} bytes]", message.data.size()); + return; + } + outln("[Received utf8 text] {}", String(ReadonlyBytes(message.data))); + }; + socket->on_close = [&](auto code, auto message, bool was_clean) { + outln("[Server {} closed connection : '{}' (code {})]", + was_clean ? "cleanly" : "dirtily", + message, + code); + should_quit = true; + Core::EventLoop::current().quit(0); + }; if (pledge("stdio unix inet accept rpath wpath tty sigaction", nullptr) < 0) { perror("pledge"); @@ -54,33 +80,6 @@ int main(int argc, char** argv) return 1; } - WebSocket::ConnectionInfo connection_info(url); - connection_info.set_origin(origin); - - auto socket = WebSocket::WebSocket::create(connection_info); - socket->on_open = [&]() { - outln("[WebSocket opened]"sv); - }; - socket->on_error = [&](auto error) { - outln("[WebSocket Error : {}]", (unsigned)error); - }; - socket->on_message = [&](auto message) { - if (!message.is_text()) { - outln("[Received binary data : {} bytes]", message.data().size()); - return; - } - outln("[Received utf8 text] {}", String(ReadonlyBytes(message.data()))); - }; - socket->on_close = [&](auto code, auto message, bool was_clean) { - outln("[Server {} closed connection : '{}' (code {})]", - was_clean ? "cleanly" : "dirtily", - message, - code); - should_quit = true; - Core::EventLoop::current().quit(0); - }; - socket->start(); - outln("Started server. Commands :"); outln("- '' send the text as message"); outln("- '.text ' send the text as message"); @@ -99,27 +98,27 @@ int main(int argc, char** argv) if (line.starts_with(".")) { if (line.starts_with(".text ")) { editor->add_to_history(line); - if (socket->ready_state() != WebSocket::ReadyState::Open) { + if (socket->ready_state() != Protocol::WebSocket::ReadyState::Open) { outln("Could not send message : socket is not open."); continue; } - socket->send(WebSocket::Message(line.substring(6))); + socket->send(line.substring(6)); continue; } if (line.starts_with(".base64 ")) { editor->add_to_history(line); - if (socket->ready_state() != WebSocket::ReadyState::Open) { + if (socket->ready_state() != Protocol::WebSocket::ReadyState::Open) { outln("Could not send message : socket is not open."); continue; } auto base64_data = line.substring(8); auto buffer = decode_base64(base64_data); - socket->send(WebSocket::Message(buffer, false)); + socket->send(buffer, false); continue; } if (line == ".exit") { editor->add_to_history(line); - if (socket->ready_state() != WebSocket::ReadyState::Open) { + if (socket->ready_state() != Protocol::WebSocket::ReadyState::Open) { outln("Socket is not open. Exiting."); should_quit = true; continue; @@ -129,7 +128,7 @@ int main(int argc, char** argv) } if (line == ".forceexit") { editor->add_to_history(line); - if (socket->ready_state() == WebSocket::ReadyState::Open) + if (socket->ready_state() == Protocol::WebSocket::ReadyState::Open) socket->close(); return 1; } @@ -137,11 +136,11 @@ int main(int argc, char** argv) continue; } editor->add_to_history(line); - if (socket->ready_state() != WebSocket::ReadyState::Open) { + if (socket->ready_state() != Protocol::WebSocket::ReadyState::Open) { outln("Could not send message : socket is not open."); continue; } - socket->send(WebSocket::Message(line)); + socket->send(line); } return 0;