Skip to content

Commit

Permalink
update network
Browse files Browse the repository at this point in the history
  • Loading branch information
mumuzi2023 committed Jan 22, 2024
1 parent c8287c3 commit 3f0ee20
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 2 deletions.
1 change: 1 addition & 0 deletions Classes/Boxes/controler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ bool controler::success() {
}

void controler::move(pii dir) {
gs->trymove(std::to_string(dir.first+1) + std::to_string(dir.second+1));
if (!suc) {
player->father->processObjects(player, player->father, player->father, dir, player->pos, 0, player, player->pos, { 1,1 }, nullptr, 0);
//if (big != player->father) {
Expand Down
51 changes: 49 additions & 2 deletions Classes/GameFrame/GameScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ bool GameScene::init()
{
return false;
}

getId();
Size winSize = Director::getInstance()->getWinSize();

auto backGround = Sprite::create("MainMenu/MainBG.png");
Expand Down Expand Up @@ -113,7 +113,7 @@ bool GameScene::init()
optMenu->alignItemsHorizontallyWithPadding(100);
optMenu->setPosition(Vec2(winSize.width / 2, 48));
this->addChild(optMenu, 1);

this->schedule(CC_SCHEDULE_SELECTOR(GameScene::getmove), 0.1f);

SaveInfo* info = SaveManager::getInstance()->info;
if (info) {
Expand Down Expand Up @@ -241,5 +241,52 @@ void GameScene::onMouseUp(Event* event)
log("Mouse button released at (%.2f, %.2f). Distance moved: %.2f", endPos.x, endPos.y, distance);
}
}
void GameScene::gm(network::HttpClient* sender, network::HttpResponse* response) {
if (!response) {
CCLOG("HttpRequest failed!");
return;
}

long statusCode = response->getResponseCode();

if (statusCode == 200) {
// 如果响应状态码为200,表示请求成功
CCLOG("HttpRequest succeeded!");

// 获取响应数据
std::vector<char>* buffer = response->getResponseData();
std::string responseData(buffer->begin(), buffer->end());

// 在这里解析服务器返回的数据
for (int i = 0; i < responseData.size(); i++) {
controler::get()->move({ responseData[i++] - '1',responseData[i] - '1' });
}
}
else {
// 请求失败,输出错误信息
CCLOG("HttpRequest failed. Status code: %ld", statusCode);
}
}
void GameScene::getmove(float dt) {
// 创建一个HttpRequest对象
network::HttpRequest* request = new network::HttpRequest();
// 设置请求的URL
request->setUrl("http://10.24.50.187:9092/cpp/get?c=" + id);
// 设置请求方式为POST
request->setRequestType(network::HttpRequest::Type::POST);

// 构造要发送的数据
std::string postData = "121321323";
// 设置请求体
request->setRequestData(postData.c_str(), postData.length());

// 设置回调函数,处理服务器的响应
request->setResponseCallback(CC_CALLBACK_2(GameScene::gm, this));

// 发送请求
network::HttpClient::getInstance()->send(request);
// 释放HttpRequest对象,避免内存泄漏
request->release();
}


94 changes: 94 additions & 0 deletions Classes/GameFrame/GameScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
#define __GAME_SCENE_H__

#include "cocos2d.h"
#include "network/HttpClient.h"
USING_NS_CC;

using pii = std::pair<int, int>;

class GameScene : public Layer {
protected:
std::string id;
EventListenerKeyboard* _listener;
EventListenerMouse* mouseListener;
Vec2 startPos;
Expand All @@ -32,6 +34,98 @@ class GameScene : public Layer {

void GameScene::onMouseUp(Event* event);

void GameScene::setId(network::HttpClient* sender, network::HttpResponse* response) {
if (!response) {
CCLOG("HttpRequest failed!");
return;
}

long statusCode = response->getResponseCode();

if (statusCode == 200) {
// 如果响应状态码为200,表示请求成功
CCLOG("HttpRequest succeeded!");

// 获取响应数据
std::vector<char>* buffer = response->getResponseData();
std::string responseData(buffer->begin(), buffer->end());

// 在这里解析服务器返回的数据
id=responseData.c_str();
}
else {
// 请求失败,输出错误信息
CCLOG("HttpRequest failed. Status code: %ld", statusCode);
}
}
void getId() {
// 创建一个HttpRequest对象
network::HttpRequest* request = new network::HttpRequest();
// 设置请求的URL
request->setUrl("http://10.24.50.187:9092/cpp/new");
// 设置请求方式为POST
request->setRequestType(network::HttpRequest::Type::POST);

// 构造要发送的数据
std::string postData = "121321323";
// 设置请求体
request->setRequestData(postData.c_str(), postData.length());

// 设置回调函数,处理服务器的响应
request->setResponseCallback(CC_CALLBACK_2(GameScene::setId, this));

// 发送请求
network::HttpClient::getInstance()->send(request);
// 释放HttpRequest对象,避免内存泄漏
request->release();
}
void GameScene::setmove(network::HttpClient* sender, network::HttpResponse* response) {
if (!response) {
CCLOG("HttpRequest failed!");
return;
}

long statusCode = response->getResponseCode();

if (statusCode == 200) {
// 如果响应状态码为200,表示请求成功
CCLOG("HttpRequest succeeded!");

// 获取响应数据
std::vector<char>* buffer = response->getResponseData();
std::string responseData(buffer->begin(), buffer->end());

// 在这里解析服务器返回的数据
id = responseData.c_str();
}
else {
// 请求失败,输出错误信息
CCLOG("HttpRequest failed. Status code: %ld", statusCode);
}
}
void trymove(std::string str) {
// 创建一个HttpRequest对象
network::HttpRequest* request = new network::HttpRequest();
// 设置请求的URL
request->setUrl("http://10.24.50.187:9092/cpp/add?c="+id+"&m="+str);
// 设置请求方式为POST
request->setRequestType(network::HttpRequest::Type::POST);

// 构造要发送的数据
std::string postData = "121321323";
// 设置请求体
request->setRequestData(postData.c_str(), postData.length());

// 设置回调函数,处理服务器的响应
request->setResponseCallback(CC_CALLBACK_2(GameScene::setmove, this));

// 发送请求
network::HttpClient::getInstance()->send(request);
// 释放HttpRequest对象,避免内存泄漏
request->release();
}
void gm(network::HttpClient* sender, network::HttpResponse* response);
void getmove(float dt);
~GameScene() {}
};

Expand Down

0 comments on commit 3f0ee20

Please sign in to comment.