https://github.com/youngmonkeys/ezyfox-server-cpp-client
c++ client for ezyfox server
https://github.com/youngmonkeys/ezyfox-server-cpp-client
c cpp cpp-sockets socket-client tcp-socket
Last synced: 5 months ago
JSON representation
c++ client for ezyfox server
- Host: GitHub
- URL: https://github.com/youngmonkeys/ezyfox-server-cpp-client
- Owner: youngmonkeys
- Created: 2017-11-29T03:36:27.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2024-06-24T01:01:44.000Z (over 1 year ago)
- Last Synced: 2025-04-29T20:39:37.744Z (8 months ago)
- Topics: c, cpp, cpp-sockets, socket-client, tcp-socket
- Language: C++
- Size: 716 KB
- Stars: 7
- Watchers: 2
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ezyfox-server-cpp-client
c++ client for ezyfox server
# Official Documentation
[https://youngmonkeys.org/ezyfox-cpp-client-sdk/](https://youngmonkeys.org/ezyfox-cpp-client-sdk/)
# Code Example
For full example you can find out at [space game](https://github.com/tvd12/space-game)
**1. Import**
```cpp
#include "EzyHeaders.h"
```
```cpp
using namespace EZY_NAMESPACE;
```
**2. Create a TCP Client**
```cpp
auto config = config::EzyClientConfig::create();
config->setClientName("first");
config->setZoneName("example");
auto client = EzyClients::getInstance()->newDefaultClient(config);
```
**3. Setup client**
```cpp
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**
```cpp
auto appSetup = setup->setupApp("hello-world");
appSetup->addDataHandler("broadcastMessage", new MessageResponseHandler());
```
**5. Connect to server**
```cpp
client->connect("localhost", 3005);
```
**6. Handle socket's events on main thread**
```cpp
auto mainEventsLoop = new socket::EzyMainEventsLoop();
mainEventsLoop->start();
```
**7. Custom event handler**
```cpp
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**
```cpp
class MessageResponseHandler : public handler::EzyAbstractAppDataHandler {
protected:
void process(entity::EzyApp* app, entity::EzyValue* data) {
logger::log("recived message: ");
#ifdef EZY_DEBUG
data->printDebug();
#endif
}
};
```