EzyFox Android Client SDK
Updated at 16856858800001. Introduce
EzyFox Android SDK is a java client library of EzyFox Server. It supports tcp protocol and use MessagePack for data transportation. You can use this SDK for every android projects and you can wrap it for React Native or Flutter. It's free available and open source on Github2. Installation
Clone ezyfox-server-android-client
to your project
git clone https://github.com/youngmonkeys/ezyfox-server-android-client.git
In your settings.gradle
file, add:
include ':socket'
In your build.gradle
file, add the dependency:
implementation project(path: ':socket')
3. Coding
Completed source code avalable on freetchat example.
3.1 Create a configuration
Please take a look EzyClientConfig file if you want to see more configurable fields
val config = EzyClientConfig.builder()
.zoneName(ZONE_NAME)
.build()
3.2 Create a Client
After create the configuration, you can create a client like this. Take a look EzyClient and EzyClients to get more information
val clients = EzyClients.getInstance() val client = clients.newClient(config)
3.3 Setup the Client
After create the Client, let's setup it, 'setup' mean add event handlers and data handlers. List of event handlers include:
- CONNECTION_SUCCESS: Fire when the client connected to server
- CONNECTION_FAILURE: Fire when the client connect to server failed
- DISCONNECTION: Fire when the client was disconnected from server
- LOST_PING: Fire when client send ping but didn't received pong command from server
- TRY_CONNECT: Fire when the client connect to server failed and retry to connect again
Take a look EzyEventType to get more information. List of data commands include:
- ERROR: Fire when the client received and error from server
- HANDSHAKE: Client send and receive handshake command
- PING: Client send ping command to keep connection
- PONG: Fire when client received pong response from server
- LOGIN: Client send and receive login command if login successfully
- LOGIN_ERROR: Fire when received login error response from server
- APP_ACCESS: Client send this command to join to an application and receive when join successfully
- APP_REQUEST: Client send data to an application on server and receive if there is any response
- APP_EXIT: Client send to server to exit from an application
- APP_ACCESS_ERROR: Fire when received join an application failed from server,
- APP_REQUEST_ERROR: Fire when received an error response from application on server,
- PLUGIN_INFO: Client send to get information of a plugin and receive response from server,
- PLUGIN_REQUEST: lient send data to an plugin on server and receive if there is any response
Take a look EzyCommand to get more information.
You can setup the client like this:
val setup = client.setup()
setup.addEventHandler(EzyEventType.CONNECTION_SUCCESS, ExConnectionSuccessHandler())
setup.addEventHandler(EzyEventType.CONNECTION_FAILURE, EzyConnectionFailureHandler())
setup.addEventHandler(EzyEventType.DISCONNECTION, ExDisconnectionHandler())
setup.addDataHandler(EzyCommand.HANDSHAKE, ExHandshakeHandler())
setup.addDataHandler(EzyCommand.LOGIN, ExLoginSuccessHandler())
setup.addDataHandler(EzyCommand.APP_ACCESS, ExAccessAppHandler())
setup.addEventHandler(EzyEventType.LOST_PING, LostPingEventHandler())
setup.addEventHandler(EzyEventType.TRY_CONNECT, TryConnectEventHandler())
For TCP we need follow by the authenticaiton flow:
For UDP we need follow by the authenticaiton flow:
3.4 Customer an event handler
You can customer to do everything you want, please take a look handler package to see default event handlers and take a look to this file for full example.
inner class ExDisconnectionHandler : EzyDisconnectionHandler() { override fun postHandle(event: EzyDisconnectionEvent) { val controller = mvc.getController("connection") val disconnectReason = event.reason if (disconnectReason == EzyDisconnectReason.CLOSE.id) { if (StateManager.getInstance().reconnnect) { client.reconnect() StateManager.getInstance().reconnnect = false } } else if(disconnectReason == EzyDisconnectReason.UNAUTHORIZED.id) { controller.updateViews("show-authentication", null) } else { controller.updateViews("show-loading", null) } } }
3.5 Custom a data handler
You must custom to handle list of commands:
- HANDSHAKE: to send LOGIN command
- LOGIN: to send APP_ACCESS or PLUGIN_INFO command
You should custom to handle list of commands:
- LOGIN_ERROR: Maybe to show a notification to user
- APP_ACCESS: Maybe to trigger something and allow user interact your application
Please take a look handler package to get default data handlers and this file for full example
inner class ExHandshakeHandler : EzyHandshakeHandler() { override fun getLoginRequest(): EzyRequest { val connectionData = Mvc.getInstance() .getModel() .get>("connection")!! return EzyLoginRequest( ZONE_NAME, connectionData["username"] as String, connectionData["password"] as String ) } } inner class ExLoginSuccessHandler : EzyLoginSuccessHandler() { override fun handleLoginSuccess(responseData: EzyData?) { val request = EzyAppAccessRequest(APP_NAME) client.send(request) } }
3.6 Setup an App
To listen and process response data of an application received from server you need setup to register handler mapped to command like this:
val appSetup = setup.setupApp(APP_NAME)
appSetup.addDataHandler(Commands.SUGGEST_CONTACTS, SuggestContactsResponseHandler())
appSetup.addDataHandler(Commands.SEARCH_CONTACTS, SearchContactsResponseHandler())
appSetup.addDataHandler(Commands.ADD_CONTACTS, AddContactsResponseHandler())
appSetup.addDataHandler(Commands.CHAT_GET_CONTACTS, ContactsResponseHandler())
appSetup.addDataHandler(Commands.CHAT_SYSTEM_MESSAGE, SystemMessageHandler())
appSetup.addDataHandler(Commands.CHAT_USER_MESSAGE, ReceivedMessageHandler())
3.7 Connect to server
After setup the client and an app, it's time to connect to server
client.connect(host, 3005)
3.8 Send a request to server
After connect to server and access an app successfully (you need ensure by handle APP_ACCESS response), you can allow interact and send a request to server:
val data = EzyEntityFactory.newObjectBuilder() .append("keyword", keyword) .build() app.send(Commands.SEARCH_CONTACTS, data)
And if you have accessed many application in the client, you must get the app you want to send a request
val app = getClient()?.zone?.appManager?.getAppByName("appName") if(app != null) { val data = EzyEntityFactory.newObjectBuilder() .append("keyword", keyword) .build() app.send(Commands.SEARCH_CONTACTS, data) }
Please take a look EzyApp to see how an app send a request to server, look EzySimpleAppManager to see how to get an app. For full example please take a look this file