修改
This commit is contained in:
parent
866499b304
commit
b1a906ec18
@ -43,27 +43,26 @@ std::string TunnelClient::handle_local_http(const std::string& method, const std
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
json err = {{"status", 405}, {"body", "unsupported method"}};
|
return json({{"status", 405}, {"body", "unsupported method"}}).dump();
|
||||||
return err.dump();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!res)
|
if (!res)
|
||||||
{
|
{
|
||||||
json err = {{"status", 500}, {"body", "local http error"}};
|
return json({{"status", 500}, {"body", "local http error"}}).dump();
|
||||||
return err.dump();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
json resp = {{"status", res->status}, {"body", res->body}};
|
return json({{"status", res->status}, {"body", res->body}}).dump();
|
||||||
return resp.dump();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TunnelClient::run_loop()
|
void TunnelClient::run_loop()
|
||||||
{
|
{
|
||||||
ws_client c;
|
ws_client c;
|
||||||
|
c.clear_access_channels(websocketpp::log::alevel::all);
|
||||||
c.init_asio();
|
c.init_asio();
|
||||||
|
|
||||||
websocketpp::connection_hdl hdl;
|
websocketpp::connection_hdl hdl;
|
||||||
|
|
||||||
|
// ----------------------- WebSocket 回调 -----------------------
|
||||||
c.set_open_handler(
|
c.set_open_handler(
|
||||||
[&](websocketpp::connection_hdl h)
|
[&](websocketpp::connection_hdl h)
|
||||||
{
|
{
|
||||||
@ -76,7 +75,8 @@ void TunnelClient::run_loop()
|
|||||||
c.set_message_handler(
|
c.set_message_handler(
|
||||||
[&](websocketpp::connection_hdl, ws_client::message_ptr msg)
|
[&](websocketpp::connection_hdl, ws_client::message_ptr msg)
|
||||||
{
|
{
|
||||||
// 服务器传来的 HTTP 请求(JSON)
|
try
|
||||||
|
{
|
||||||
std::string payload = msg->get_payload();
|
std::string payload = msg->get_payload();
|
||||||
json req = json::parse(payload);
|
json req = json::parse(payload);
|
||||||
|
|
||||||
@ -85,18 +85,21 @@ void TunnelClient::run_loop()
|
|||||||
std::string path = req["path"];
|
std::string path = req["path"];
|
||||||
std::string body = req.value("body", "");
|
std::string body = req.value("body", "");
|
||||||
|
|
||||||
// 转发给本地 HTTP 服务
|
std::string local_result = handle_local_http(method, path, body);
|
||||||
std::string result = handle_local_http(method, path, body);
|
|
||||||
|
|
||||||
// 包装 response
|
|
||||||
json resp;
|
json resp;
|
||||||
resp["req_id"] = req_id;
|
resp["req_id"] = req_id;
|
||||||
resp["resp"] = json::parse(result);
|
resp["resp"] = json::parse(local_result);
|
||||||
|
|
||||||
// 发送回服务器
|
|
||||||
c.send(hdl, resp.dump(), websocketpp::frame::opcode::text);
|
c.send(hdl, resp.dump(), websocketpp::frame::opcode::text);
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
printf("[Tunnel] JSON or HTTP error\n");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ----------------------- 建立连接 -----------------------
|
||||||
websocketpp::lib::error_code ec;
|
websocketpp::lib::error_code ec;
|
||||||
auto conn = c.get_connection(ws_url_, ec);
|
auto conn = c.get_connection(ws_url_, ec);
|
||||||
if (ec)
|
if (ec)
|
||||||
@ -104,13 +107,16 @@ void TunnelClient::run_loop()
|
|||||||
printf("[Tunnel] Connection init failed: %s\n", ec.message().c_str());
|
printf("[Tunnel] Connection init failed: %s\n", ec.message().c_str());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
c.connect(conn);
|
c.connect(conn);
|
||||||
|
|
||||||
|
// ----------------------- 主循环 -----------------------
|
||||||
while (running_)
|
while (running_)
|
||||||
{
|
{
|
||||||
c.run_once();
|
// 处理一次事件
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
c.run_one();
|
||||||
|
|
||||||
|
// 如果连接停止(断线、错误等)——退出 loop
|
||||||
|
if (c.stopped()) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("[Tunnel] Loop exit\n");
|
printf("[Tunnel] Loop exit\n");
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user