update network sample

This commit is contained in:
Nomango 2019-03-31 13:56:05 +08:00 committed by Nomango
parent e2b5953a75
commit 0b7ca8670f
3 changed files with 150 additions and 20 deletions

View File

@ -901,7 +901,7 @@ namespace easy2d
case JsonType::String:
{
out->write('\"');
out->write(json.value_.data.string->c_str());
dump_string(*json.value_.data.string);
out->write('\"');
return;
}
@ -986,6 +986,31 @@ namespace easy2d
out->write(number_buffer.data());
}
void dump_string(const string_type & val)
{
for (const auto& ch : val)
{
switch (ch)
{
case '\t':
out->write('\\');
out->write('t');
break;
case '\r':
out->write('\\');
out->write('r');
break;
case '\n':
out->write('\\');
out->write('n');
break;
default:
out->write(ch);
break;
}
}
}
private:
output_adapter<char_type>* out;
char_type indent_char;
@ -1247,10 +1272,20 @@ namespace easy2d
{
string_buffer.clear();
bool must_read_next = false;
while (true)
{
const auto ch = read_next();
if (ch == '\"')
if (must_read_next)
{
must_read_next = false;
}
else if (ch == '\\')
{
must_read_next = true;
}
else if (ch == '\"')
{
// skip last \"
read_next();

View File

@ -554,7 +554,7 @@ namespace easy2d
{
discard_const_data();
if (cstr)
if (cstr && count)
{
if (count > capacity_)
{

View File

@ -15,10 +15,11 @@ public:
Demo5()
{
// 添加按键监听
AddListener(Event::KeyDown, Closure(this, &Demo5::OnKeyDown));
// 创建说明文字
TextPtr text = new Text(L"空格键发送测试请求");
TextPtr text = new Text(L"G发送GET请求\n按P发送POST请求\n按U发送PUT请求\n按D发送DELETE请求");
// 设置节点大小为文字布局大小
text->SetSize(text->GetLayoutSize());
// 设置文字位置
@ -31,30 +32,107 @@ public:
void OnEnter() override
{
// 进入场景时打开控制台
Application::ShowConsole(true);
SendRequest();
}
void OnExit() override
{
// 退出场景时关闭控制台
Application::ShowConsole(false);
}
void OnKeyDown(Event const& e)
{
if (e.key.code == KeyCode::Space)
// 按不同键发送不同请求
if (e.key.code == KeyCode::G)
{
SendRequest();
SendGetRequest();
}
else if (e.key.code == KeyCode::P)
{
SendPostRequest();
}
else if (e.key.code == KeyCode::U)
{
SendPutRequest();
}
else if (e.key.code == KeyCode::D)
{
SendDeleteRequest();
}
}
void SendRequest()
void SendGetRequest()
{
Logger::Instance().Println(L"Start to send request...");
// 发送 GET 请求
Logger::Instance().Println(L"Start to send GET request...");
HttpRequestPtr request = new HttpRequest;
// 设置请求 URL
request->SetUrl(L"http://httpbin.org/get");
// 设置请求类型为 GET
request->SetType(HttpRequest::Type::Get);
// 设置请求完成后的回调函数
request->SetResponseCallback(Closure(this, &Demo5::Complete));
// 发送 HTTP 请求
HttpClient::Instance().Send(request);
}
void SendPostRequest()
{
// 发送 POST 请求
Logger::Instance().Println(L"Start to send POST request...");
// 创建 JSON 格式的 POST 数据
Json request_data = {
{"String", "StringTest"},
{"Boolean", true},
{"Integer", 12},
{"Float", 3.125},
{"Array", {1, 2, 3, 4, 4.5 }},
{"Object", {"Key", "Value"}},
};
HttpRequestPtr request = new HttpRequest;
request->SetUrl(L"http://httpbin.org/post");
request->SetType(HttpRequest::Type::Post);
// 设置 POST 请求的数据
request->SetData(request_data.dump());
request->SetResponseCallback(Closure(this, &Demo5::Complete));
HttpClient::Instance().Send(request);
}
void SendPutRequest()
{
// 发送 PUT 请求
Logger::Instance().Println(L"Start to send PUT request...");
// 创建 JSON 格式的 PUT 数据
Json request_data = {
{"user", {{"id", 1}, {"phone", "12345678"}}},
};
HttpRequestPtr request = new HttpRequest;
request->SetUrl(L"http://httpbin.org/put");
request->SetType(HttpRequest::Type::Put);
// 设置 PUT 请求的数据
request->SetData(request_data.dump());
request->SetResponseCallback(Closure(this, &Demo5::Complete));
HttpClient::Instance().Send(request);
}
void SendDeleteRequest()
{
// 发送 DELETE 请求
Logger::Instance().Println(L"Start to send DELETE request...");
HttpRequestPtr request = new HttpRequest;
request->SetUrl(L"http://httpbin.org/delete");
request->SetType(HttpRequest::Type::Delete);
request->SetResponseCallback(Closure(this, &Demo5::Complete));
HttpClient::Instance().Send(request);
@ -62,22 +140,39 @@ public:
void Complete(HttpRequestPtr request, HttpResponsePtr response)
{
// 判断请求是否成功
if (response->IsSucceed())
{
try
{
// 将获取到的数据解析成 JSON 格式
Json response_data = Json::parse(response->GetData());
Json result = {
{L"HttpCode", response->GetResponseCode()},
{L"Data", response_data},
};
std::wcout << L"Response: " << std::endl << result.dump(4) << std::endl;
}
catch (json_exception& e)
{
E2D_ERROR_LOG(L"Parse JSON failed: %s", e.what());
}
}
else
{
// 请求失败时打印错误信息
OutputError(response);
}
}
void OutputError(HttpResponsePtr response)
{
// 打印 HTTP 响应结果的状态码和错误信息
Json result = {
{L"HttpCode", response->GetResponseCode()},
{L"Error", response->GetError()},
};
std::wcout << L"Response: " << std::endl << result.dump(4) << std::endl;
}
}
};