EzyFox Swift Client SDK

Updated at 1685685977000

1. Introduce

EzyFox Swift SDK is a Swift client library of EzyFox Server. It supports TCP and UDP protocol and use MessagePack for data transportation. You can use this SDK for every projects use Swift (i.e iOS, apple desktop apps). It's free available and open source on Github.

2. Installation

Clone ezyfox-server-swift-client to your project:

    git clone https://github.com/youngmonkeys/ezyfox-server-swift-client

pull submodule:

    git submodule update --init --recursive

change the name ezyfox-server-swift-client to client if you want Add ezyfox-client.xcodeproj to your project, you can find out it in ezyfox-server-swift-client/socket folder:

add ezyfox-server-swift-client (maybe you changed name to client) to xcode project (by drag and drop) and we have

3. Coding

Completed source code available on Free Chat example, class SocketClientProxy

3.1 Import

If you have already added ezyfox-server-swift-client to your project, you don't import anything

3.2 Create a configuration

Please take a look EzyClientConfig class if you want to see more configurable fields

    let config = NSMutableDictionary()
    config["clientName"] = ZONE_APP_NAME

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.

    let clients = EzyClients.getInstance()!
    client = clients.newClient(config: config)

3.3 Setup the Client

After create the client, let's setup it, 'setup' mean add event handlers and data handlers to the client . List of event handlers include:

  1. CONNECTION_SUCCESS: Fire when the client connected to server
  2. CONNECTION_FAILURE: Fire when the client connect to server failed
  3. DISCONNECTION: Fire when the client was disconnected from server
  4. LOST_PING: Fire when client send ping but didn't received pong command from server
  5. TRY_CONNECT: Fire when the client connect to server failed and retry to connect again

Take a look EzyConstants class to get more information. List of data commands include:

  1. ERROR: Fire when the client received and error from server
  2. HANDSHAKE: Client send and receive handshake command
  3. PING: Client send ping command to keep connection
  4. PONG: Fire when client received pong response from server
  5. LOGIN: Client send and receive login command if login successfully
  6. LOGIN_ERROR: Fire when received login error response from server
  7. APP_ACCESS: Client send this command to join to an application and receive when join successfully
  8. APP_REQUEST: Client send data to an application on server and receive if there is any response
  9. APP_EXIT: Client send to server to exit from an application
  10. APP_ACCESS_ERROR: Fire when received join an application failed from server,
  11. APP_REQUEST_ERROR: Fire when received an error response from application on server,
  12. PLUGIN_INFO: Client send to get information of a plugin and receive response from server,

Take a look EzyConstants file and EzyHandlers file to get more information and you can setup the client like this:

     let setup = client.setup!
        .addEventHandler(eventType: EzyEventType.CONNECTION_SUCCESS, handler: EzyConnectionSuccessHandler())
        .addEventHandler(eventType: EzyEventType.CONNECTION_FAILURE, handler: EzyConnectionFailureHandler())
        .addEventHandler(eventType: EzyEventType.DISCONNECTION, handler: ExDisconnectionHandler())
        .addDataHandler(cmd: EzyCommand.LOGIN, handler: ExLoginSuccessHandler())
        .addDataHandler(cmd: EzyCommand.APP_ACCESS, handler: ExAppAccessHandler())
        .addDataHandler(cmd: EzyCommand.HANDSHAKE, handler: ExHandshakeHandler())

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 custom to do everything you want, please take a look EzyHandlers file to see default event handlers and take a look to this file for full example.

    class ExDisconnectionHandler: EzyDisconnectionHandler {
        override func postHandle(event: NSDictionary) {
            // do something here

3.5 Custom a data handler

You must custom to handle list of commands:

  1. HANDSHAKE: to send Login command
  2. LOGIN: to send AppAccess or PluginInfo command

You should custom to handle list of commands:

  1. LOGIN_ERROR: Maybe to show a notification to user
  2. APP_ACCESS: Maybe to trigger something and allow user interact your application

Please take a look EzyHandles file to get default data handlers and this file for full example

    class ExHandshakeHandler : EzyHandshakeHandler {
        override func getLoginRequest() -> NSArray {
            let mvc = Mvc.getInstance();
            let connection = mvc.getModel().get(name: "connection") as! Model
            let array = NSMutableArray()
            array.add(connection.get(name: "username")!)
            array.add(connection.get(name: "password")!)
            return array
    class ExLoginSuccessHandler : EzyLoginSuccessHandler {
        override func handleLoginSuccess(responseData: NSObject) {
            let array = NSMutableArray()
            client!.send(cmd: EzyCommand.APP_ACCESS, data: array)

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:

    _ = setup.setupApp(appName: ZONE_APP_NAME)
        .addDataHandler(cmd: Commands.GET_CONTACTS, handler: GetContactsResponseHandler())
        .addDataHandler(cmd: Commands.SEARCH_EXISTED_CONTACTS, handler: SearchExistedContactsResponseHandler())
        .addDataHandler(cmd: Commands.SUGGEST_CONTACTS, handler: SuggestContactsResponseHandler())

3.7 Connect to server

After setup the client and an app, it's time to connect to server

    client.connect(host: host, port: 3005)

3.8 Run event loop

You need run even loop to take event and data from queue, don't forget this action.

    Thread.current.name = "main";

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:

    let app = EzyClients.getInstance()
    let requestData = NSMutableDictionary()
    requestData["keyword"] = keyword
    app?.send(cmd: Commands.SEARCH_CONTACTS, data: requestData)

And if you have accessed many application in the client, you must get the app you want to send a request

    let app = EzyClients.getInstance()
        .getAppByName(name: "appName")
    let requestData = NSMutableDictionary()
    requestData["keyword"] = keyword
    app?.send(cmd: Commands.SEARCH_CONTACTS, data: requestData)

Please take a look EzyApp class to see how an app send a request to server, look EzyAppManager to see how to get an app. For full example please take a look this file

4. Conclusion

EzyFox Swift client SDK is a convenient library to connect to EzyFox server. You can free to use and free to customer anything you want. For full source code and more information, please take a look to Github.