|
| 1 | +// |
| 2 | +// main.cpp |
| 3 | +// ezyfox-console |
| 4 | +// |
| 5 | +// Created by Dzung on 08/08/2021. |
| 6 | +// |
| 7 | + |
| 8 | +#include <iostream> |
| 9 | +#include "EzyHeaders.h" |
| 10 | + |
| 11 | +const char* ZONE_NAME = "example"; |
| 12 | +const char* APP_NAME = "hello-world"; |
| 13 | + |
| 14 | +class HandshakeHandler : public handler::EzyHandshakeHandler { |
| 15 | +protected: |
| 16 | + request::EzyRequest* getLoginRequest() { |
| 17 | + auto request = request::EzyLoginRequest::create(); |
| 18 | + request->setZoneName(ZONE_NAME); |
| 19 | + request->setUsername("dungtv"); |
| 20 | + request->setPassword("123456"); |
| 21 | + return request; |
| 22 | + }; |
| 23 | +}; |
| 24 | + |
| 25 | +class LoginSuccessHandler : public handler::EzyLoginSuccessHandler { |
| 26 | +protected: |
| 27 | + void handleLoginSuccess(entity::EzyValue* responseData) { |
| 28 | + auto appRequest = request::EzyAppAccessRequest::create(); |
| 29 | + appRequest->setAppName(APP_NAME); |
| 30 | + mClient->send(appRequest); |
| 31 | + } |
| 32 | +}; |
| 33 | + |
| 34 | +class AppAccessHandler : public handler::EzyAppAccessHandler { |
| 35 | +protected: |
| 36 | + void postHandle(entity::EzyApp* app, entity::EzyArray* data) { |
| 37 | + auto requestData = new entity::EzyObject(); |
| 38 | + requestData->setString("who", "C/C++ Developer"); |
| 39 | + app->send("greet", requestData); |
| 40 | + } |
| 41 | +}; |
| 42 | + |
| 43 | +class GreetResponseHandler : public handler::EzyAbstractAppDataHandler<entity::EzyObject> { |
| 44 | +protected: |
| 45 | + void process(entity::EzyApp* app, entity::EzyObject* data) { |
| 46 | + logger::log("recived message: %s", data->getString("message").c_str()); |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +int main(int argc, const char * argv[]) { |
| 51 | + auto config = config::EzyClientConfig::create(); |
| 52 | + config->setClientName(ZONE_NAME); |
| 53 | + auto mSocketClient = EzyUTClient::create(config); |
| 54 | + EzyClients::getInstance()->addClient(mSocketClient); |
| 55 | + auto setup = mSocketClient->setup(); |
| 56 | + setup->addDataHandler(constant::Handshake, new HandshakeHandler()); |
| 57 | + setup->addDataHandler(constant::Login, new LoginSuccessHandler()); |
| 58 | + setup->addDataHandler(constant::AppAccess, new AppAccessHandler()); |
| 59 | + |
| 60 | + auto appSetup = setup->setupApp("hello-world"); |
| 61 | + appSetup->addDataHandler("greet", new GreetResponseHandler()); |
| 62 | + |
| 63 | + mSocketClient->connect("tvd12.com", 3005); |
| 64 | + |
| 65 | + socket::EzyMainEventsLoop* eventsLoop = new socket::EzyMainEventsLoop(); |
| 66 | + eventsLoop->start(); |
| 67 | + EZY_SAFE_DELETE(eventsLoop); |
| 68 | +} |
| 69 | + |
0 commit comments