c++ client for ezyfox server
https://youngmonkeys.org/ezyfox-cpp-client-sdk/
For full example you can find out at space game
1. Import
#include "EzyHeaders.h"
using namespace EZY_NAMESPACE;
2. Create a TCP Client
auto config = config::EzyClientConfig::create(); config->setClientName("first"); config->setZoneName("example"); auto client = EzyClients::getInstance()->newDefaultClient(config);
3. Setup client
auto setup = client->setup(); setup->addEventHandler(event::ConnectionSuccess, new handler::EzyConnectionSuccessHandler()); setup->addEventHandler(event::ConnectionFailure, new handler::EzyConnectionFailureHandler()); setup->addDataHandler(constant::Handshake, new ExHandshakeHandler()); setup->addDataHandler(constant::Login, new ExLoginSuccessHandler()); setup->addDataHandler(constant::AppAccess, new ExAppAccessHandler());
4. Setup app
auto appSetup = setup->setupApp("hello-world"); appSetup->addDataHandler("broadcastMessage", new MessageResponseHandler());
5. Connect to server
client->connect("localhost", 3005);
6. Handle socket's events on main thread
auto mainEventsLoop = new socket::EzyMainEventsLoop(); mainEventsLoop->start();
7. Custom event handler
class ExHandshakeHandler : public handler::EzyHandshakeHandler { protected: request::EzyRequest* getLoginRequest() { auto request = request::EzyLoginRequest::create(); request->setZoneName("example"); request->setUsername("yourname"); request->setPassword("123456"); return request; }; }; class ExLoginSuccessHandler : public handler::EzyLoginSuccessHandler { protected: void handleLoginSuccess(entity::EzyArray* joinedApps, entity::EzyValue* responseData) { auto request = request::EzyAppAccessRequest::create(); request->setAppName("hello-world"); request->setData(new entity::EzyArray()); mClient->send(request); } }; class ExAppAccessHandler : public handler::EzyAppAccessHandler { protected: void postHandle(entity::EzyApp* app, entity::EzyArray* data) { auto obj = new entity::EzyObject(); obj->setString("message", "Hi EzyFox, I'm from C++ client"); app->send("broadcastMessage", obj); } };
8. Custom app's data handler
class MessageResponseHandler : public handler::EzyAbstractAppDataHandler<entity::EzyValue> { protected: void process(entity::EzyApp* app, entity::EzyValue* data) { logger::log("recived message: "); #ifdef EZY_DEBUG data->printDebug(); #endif } };