ππ» μλλ μΉμμΌμ λν μ€λͺ
μ
λλ€.
Below is an explanation of WebSockets.
ππ» μΉμμΌμ μλ²μ ν΄λΌμ΄μΈνΈκ°μ μλ°©ν₯ ν΅μ μ κ°λ₯νκ² ν©λλ€.
WebSockets enable bidirectional communication between a server and a client.
ππ» μλμ μ½λλ μΉμμΌμ μ¬μ©ν΄μ κ°λ¨ν μ±ν
κΈ°λ₯μ ꡬνν κ²μ
λλ€.
The code below implements a simple chat function using WebSockets.
ππ» ν°λ―Έλκ³Ό λΈλΌμ°μ κ°μ μ±ν
μ΄ κ°λ₯ν©λλ€.
Chatting between the terminal and the browser is possible.
ππ» bc_server.cpp
βοΈμλ²μ λΈλΌμ°μ ν΄λΌμ΄μΈνΈμ λν μ€μ μ΄ μμ΅λλ€.
There are settings for the server and the browser client.
βοΈ μ½λ / Code
#include "httplib.h"
#include <algorithm>
#include <iostream>
#include <mutex>
#include <string>
#include <vector>
/* ---- λͺ¨λ μμΌ μ μμμκ² λ©μΈμ§ μ μ‘ / Send message to all socket connected users---- */
std::mutex g_mtx;
// g_clients λͺ¨λ μΉμμΌ ν΄λΌμ΄μΈνΈ / g_clients all WebSocket clients
std::vector<httplib::ws::WebSocket*> g_clients;
void broadcast(const std::string& msg) {
// μ€λ λμμ κ°μλ°μ΄ν° 건λ릴λ μΆ©λλ§λ μμ μ₯μΉ
// A safety mechanism to prevent conflicts when threads access the same data
std::lock_guard<std::mutex> lock(g_mtx);
// λͺ¨λ μμΌ μ μμμκ² λ©μΈμ§μ λ¬ / Deliver message to all socket users
for (auto* c : g_clients) {
c->send(msg);
}
}
int main() {
httplib::Server svr;
svr.Get("/", [](const httplib::Request&, httplib::Response& res) {
res.set_content(R"(
<!doctype html><html><body>
<input id="msg" value="hello aloy~~π"><button id="send">Send</button>
<pre id="log"></pre>
<script>
const log = document.getElementById('log');
const ws = new WebSocket('ws://localhost:5080/ws');
ws.onmessage = (e) => log.textContent += '[recv] ' + e.data + '\n';
document.getElementById('send').onclick = () => {
const v = document.getElementById('msg').value;
const vplus = "ππ»" + v;
//ws.send(v);
ws.send(vplus);
log.textContent += '[send] ' + v + '\n';
};
</script></body></html>
)", "text/html; charset=utf-8");
});
svr.WebSocket("/ws", [](const httplib::Request&, httplib::ws::WebSocket& ws) {
{
std::lock_guard<std::mutex> lock(g_mtx);
g_clients.push_back(&ws);
}
std::string msg;
while (ws.read(msg)) {
broadcast(msg); // λͺ¨λμκ² μ μ‘ / send to everyone
}
{
std::lock_guard<std::mutex> lock(g_mtx);
g_clients.erase(std::remove(g_clients.begin(), g_clients.end(), &ws), g_clients.end());
}
});
std::cout << "ws server: http://localhost:5080\n";
svr.listen("0.0.0.0", 5080);
}
βοΈ μ»΄νμΌ / Compiling
g++ bc_server.cpp -o bc_server -std=c++17 -pthread
βοΈ μλ²μ€ν / Run Server

ππ» client.cpp
βοΈ ν°λ―Έλμμ μ€νκ°λ₯ν ν΄λΌμ΄μΈνΈμ
λλ€.
This is a client executable in the terminal.
βοΈ μ½λ / Code
#include "httplib.h"
#include <atomic>
#include <iostream>
#include <string>
#include <thread>
int main() {
httplib::ws::WebSocketClient ws("ws://localhost:5080/ws");
if (!ws.connect()) {
std::cerr << "connect failed\n";
return 1;
}
std::atomic<bool> running{true};
// μμ μ€λ λ / receive thread
std::thread reader([&]() {
std::string msg;
while (running && ws.read(msg)) {
std::cout << "[recv] " << msg << '\n';
}
running = false;
});
std::cout << "connected. type message and press Enter.\n";
std::cout << "type /quit to exit.\n";
// μ‘μ 루ν / Send loop
std::string line;
while (running && std::getline(std::cin, line)) {
if (line == "/quit") break;
// ws.send(line);
ws.send("βοΈ" + line);
}
running = false;
ws.close();
if (reader.joinable()) reader.join();
std::cout << "closed\n";
return 0;
}
βοΈ μ»΄νμΌ / Compiling
g++ client.cpp -o client -std=c++17 -pthread
βοΈ μ€ν / Run

