Skip to content

Commit 066c9ef

Browse files
committed
test: verify websocket connection
1 parent 475b000 commit 066c9ef

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ elseif(BUILD_TEST STREQUAL "mqtt")
8080
Threads::Threads
8181
${MOSQUITTO_LIBS}
8282
)
83+
elseif(BUILD_TEST STREQUAL "websocket")
84+
add_executable(test_websocket test/test_websocket.cpp)
85+
86+
target_link_libraries(test_websocket
87+
ssl
88+
crypto
89+
)
8390
elseif(BUILD_TEST STREQUAL "openh264")
8491
add_executable(test_openh264 test/test_openh264.cpp)
8592
target_link_libraries(test_openh264

test/test_websocket.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <boost/asio.hpp>
2+
#include <boost/beast.hpp>
3+
#include <iostream>
4+
#include <string>
5+
6+
#include <nlohmann/json.hpp>
7+
8+
namespace beast = boost::beast;
9+
namespace websocket = beast::websocket;
10+
namespace net = boost::asio;
11+
using tcp = net::ip::tcp;
12+
using json = nlohmann::json;
13+
14+
std::string createConnectionParams(const std::string &accessToken) {
15+
return "token=" + accessToken;
16+
}
17+
18+
int main() {
19+
try {
20+
net::io_context ioc;
21+
22+
tcp::resolver resolver(ioc);
23+
websocket::stream<tcp::socket> ws(ioc);
24+
25+
std::string host = "192.168.4.21";
26+
std::string port = "8080";
27+
std::string accessToken =
28+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
29+
"eyJleHAiOjE3NzM4OTI2NjEsImlzcyI6IkFQSVduUVRzNHRtVVp2QSIsIm5iZiI6MTc0MjM1NjY2MSwic3ViIj"
30+
"oiMTMxZGZjMzItNjRlMi00YjZiLTllZGEtZDdjYTU5NTNjYWJlIiwidmlkZW8iOnsicm9vbSI6ImRldmljZS0x"
31+
"Iiwicm9vbUpvaW4iOnRydWV9fQ.ThQTHYd8CBR0t3epwcak6oaleeu760V96UF8GbOMUks";
32+
std::string queryParams = createConnectionParams(accessToken);
33+
std::string target = "/rtc?" + queryParams;
34+
35+
// WebSocket without SSL
36+
auto const results = resolver.resolve(host, port);
37+
net::connect(ws.next_layer(), results.begin(), results.end());
38+
ws.handshake(host, target);
39+
std::cout << "WebSocket is connected!" << std::endl;
40+
41+
while (true) {
42+
beast::flat_buffer buffer;
43+
try {
44+
ws.read(buffer);
45+
} catch (const std::exception &e) {
46+
std::cerr << "Error: " << e.what() << std::endl;
47+
break;
48+
}
49+
50+
if (ws.got_text()) {
51+
std::string res = beast::buffers_to_string(buffer.data());
52+
53+
json jsonObj = json::parse(res.c_str());
54+
std::string action = jsonObj["Action"];
55+
std::string message = jsonObj["Message"];
56+
std::cout << "Received Action: " << action << std::endl;
57+
std::cout << "Received Message: " << message << std::endl;
58+
59+
if (action == "join") {
60+
std::string response = "{\"Action\":\"offer\",\"Message\":\"test\"}";
61+
ws.write(net::buffer(response));
62+
} else {
63+
break;
64+
}
65+
} else if (ws.got_binary()) {
66+
std::vector<uint8_t> binaryData(boost::asio::buffers_begin(buffer.data()),
67+
boost::asio::buffers_end(buffer.data()));
68+
std::cout << "Received Binary Data (size: " << binaryData.size() << " bytes)"
69+
<< std::endl;
70+
}
71+
}
72+
73+
ws.close(websocket::close_code::normal);
74+
} catch (const std::exception &e) {
75+
std::cerr << "Error: " << e.what() << std::endl;
76+
}
77+
return 0;
78+
}

0 commit comments

Comments
 (0)