๐๐ป ์๋๋ ์น์์ผ์ ๋ํ ์ค๋ช
์
๋๋ค.
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

