Каркас проекта: ТЗ, nginx+gameserver(uWebSockets) в Docker, /ws echo
Frontend — чистый HTML/CSS/JS без сборки (резюме, проекты, игра-заглушки). Gameserver — C++ на uWebSockets (CMake FetchContent), протокол hello/lobby.list_rooms/error проверен end-to-end.
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
// Каркас (этап 1 из TZ.md): проверка связки nginx <-ws-> gameserver.
|
||||
// Лобби/комнаты/игровая логика — следующие этапы.
|
||||
|
||||
#include <App.h>
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
struct PerSocketData {};
|
||||
|
||||
int main() {
|
||||
uWS::App()
|
||||
.ws<PerSocketData>("/ws", {
|
||||
.open = [](auto *ws) {
|
||||
std::cout << "client connected\n";
|
||||
},
|
||||
.message = [](auto *ws, std::string_view message, uWS::OpCode) {
|
||||
json response;
|
||||
try {
|
||||
json request = json::parse(message);
|
||||
std::string type = request.value("type", "");
|
||||
|
||||
if (type == "hello") {
|
||||
response = {{"type", "hello.ack"}, {"payload", {{"player_id", "stub"}}}};
|
||||
} else if (type == "lobby.list_rooms") {
|
||||
response = {{"type", "lobby.rooms"}, {"payload", {{"rooms", json::array()}}}};
|
||||
} else {
|
||||
response = {{"type", "error"},
|
||||
{"payload", {{"code", "unknown_type"}, {"message", "unknown message type: " + type}}}};
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
response = {{"type", "error"}, {"payload", {{"code", "bad_json"}, {"message", e.what()}}}};
|
||||
}
|
||||
ws->send(response.dump(), uWS::OpCode::TEXT);
|
||||
},
|
||||
.close = [](auto *ws, int, std::string_view) {
|
||||
std::cout << "client disconnected\n";
|
||||
},
|
||||
})
|
||||
.listen(9001, [](auto *token) {
|
||||
if (token) {
|
||||
std::cout << "gameserver listening on :9001\n";
|
||||
} else {
|
||||
std::cerr << "failed to listen on :9001\n";
|
||||
}
|
||||
})
|
||||
.run();
|
||||
}
|
||||
Reference in New Issue
Block a user