1.Introduction

ezyfox-server-csharp-client is a client sdk of ezyfox-server written in csharp language, you can use it for any csharp application like Windows application, Xamarin application and Unity application

2.How to install?

ezyfox-server-csharp-client source code available on Github, so you can clone and import to any project, you also modify the source code to fit you purpose

3.Create an example

Let’s say you want to create a HelloWorld Console application to say hello everyone available on EzyFox Server Firstly, we need download ezyfox-server lastest version and run it, if you don’t have Java 8, you need download and install it, this tutorial will help you, you also need setup JAVA_HOME path variable. If you don’t know how to run ezyfox-server, please read this tutorial Secondly, we need create C# Console Application on Visual Studio with name: HelloWorld Thirdly, we need clone ezyfox-server-csharp-client to HelloWorld project folder After setup project, we have:

https://tvd12.com/wp-content/uploads/HelloWord-CSharp.png

3.1 Import package dependecies

Open Program.cs and add to head

using System;
using System.Threading;
using com.tvd12.ezyfoxserver.client;
using com.tvd12.ezyfoxserver.client.io;
using com.tvd12.ezyfoxserver.client.evt;
using com.tvd12.ezyfoxserver.client.request;
using com.tvd12.ezyfoxserver.client.config;
using com.tvd12.ezyfoxserver.client.setup;
using com.tvd12.ezyfoxserver.client.handler;
using com.tvd12.ezyfoxserver.client.constant;
using com.tvd12.ezyfoxserver.client.entity;
using com.tvd12.ezyfoxserver.client.factory;

3.2 Create a client

EzyClientConfig clientConfig = EzyClientConfig
                .builder()
                .clientName("first")
                .zoneName("example")
                .build();
EzyClients clients = EzyClients.getInstance();
EzyClient client = clients.newDefaultClient(clientConfig);

3.3 Setup the client

EzySetup setup = client.setup();
setup.addEventHandler(EzyEventType.CONNECTION_SUCCESS, new EzyConnectionSuccessHandler());
setup.addEventHandler(EzyEventType.CONNECTION_FAILURE, new EzyConnectionFailureHandler());
setup.addDataHandler(EzyCommand.HANDSHAKE, new ExHandshakeEventHandler());
setup.addDataHandler(EzyCommand.LOGIN, new ExLoginSuccessHandler());
setup.addDataHandler(EzyCommand.APP_ACCESS, new ExAccessAppHandler());

3.4 Setup an app

EzyAppSetup appSetup = setup.setupApp("hello-world");
appSetup.addDataHandler("broadcastMessage", new MessageResponseHandler());

3.5 Connect to server

client.connect("localhost", 3005);

3.6 Loop socket events

while (true) 
{
    Thread.Sleep(3);
    client.processEvents();
}

3.7 Create event and data handler classes

class ExHandshakeEventHandler : EzyHandshakeHandler
{
    protected override EzyRequest getLoginRequest()
    {
        return new EzyLoginRequest("example", "monkey", "123456");
    }
}

class ExLoginSuccessHandler : EzyLoginSuccessHandler
{
    protected override void handleLoginSuccess(EzyData responseData)
    {
        EzyRequest accessAppRequest = new EzyAppAccessRequest("hello-world");
        client.send(accessAppRequest);
    }
}

class ExAccessAppHandler : EzyAppAccessHandler
{
    protected override void postHandle(EzyApp app, EzyArray data)
    {
        broadcastMessage(app);
    }

    protected void broadcastMessage(EzyApp app)
    {
        EzyObject data = EzyEntityFactory.newObjectBuilder()
            .append("message", "hello everyone!")
            .build();
        app.send("broadcastMessage", data);
    }
}

class MessageResponseHandler : EzyAppDataHandler
{
    public void handle(EzyApp app, EzyData data)
    {
        EzyObject obj = (EzyObject)data;
        String message = obj.get<String>("message");
        Console.WriteLine("Received message: " + message);
    }
}

After all, we have the Program.cs with full content:

using System;
using System.Threading;
using com.tvd12.ezyfoxserver.client;
using com.tvd12.ezyfoxserver.client.io;
using com.tvd12.ezyfoxserver.client.evt;
using com.tvd12.ezyfoxserver.client.request;
using com.tvd12.ezyfoxserver.client.config;
using com.tvd12.ezyfoxserver.client.setup;
using com.tvd12.ezyfoxserver.client.handler;
using com.tvd12.ezyfoxserver.client.constant;
using com.tvd12.ezyfoxserver.client.entity;
using com.tvd12.ezyfoxserver.client.factory;

namespace HelloWorld
{
    class ExHandshakeEventHandler : EzyHandshakeHandler
    {
        protected override EzyRequest getLoginRequest()
        {
            return new EzyLoginRequest("example", "monkey", "123456");
        }
    }

    class ExLoginSuccessHandler : EzyLoginSuccessHandler
    {
        protected override void handleLoginSuccess(EzyData responseData)
        {
            EzyRequest accessAppRequest = new EzyAppAccessRequest("hello-world");
            client.send(accessAppRequest);
        }
    }

    class ExAccessAppHandler : EzyAppAccessHandler
    {
        protected override void postHandle(EzyApp app, EzyArray data)
        {
            broadcastMessage(app);
        }

        protected void broadcastMessage(EzyApp app)
        {
            EzyObject data = EzyEntityFactory.newObjectBuilder()
                .append("message", "hello everyone!")
                .build();
            app.send("broadcastMessage", data);
        }
    }

    class MessageResponseHandler : EzyAppDataHandler
    {
        public void handle(EzyApp app, EzyData data)
        {
            EzyObject obj = (EzyObject)data;
            String message = obj.get<String>("message");
            Console.WriteLine("Received message: " + message);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            EzyClientConfig clientConfig = EzyClientConfig
                .builder()
                .clientName("first")
                .zoneName("example")
                .build();
            EzyClients clients = EzyClients.getInstance();
            EzyClient client = clients.newDefaultClient(clientConfig);

            EzySetup setup = client.setup();
            setup.addEventHandler(EzyEventType.CONNECTION_SUCCESS, new EzyConnectionSuccessHandler());
            setup.addEventHandler(EzyEventType.CONNECTION_FAILURE, new EzyConnectionFailureHandler());
            setup.addDataHandler(EzyCommand.HANDSHAKE, new ExHandshakeEventHandler());
            setup.addDataHandler(EzyCommand.LOGIN, new ExLoginSuccessHandler());
            setup.addDataHandler(EzyCommand.APP_ACCESS, new ExAccessAppHandler());

            EzyAppSetup appSetup = setup.setupApp("hello-world");
            appSetup.addDataHandler("broadcastMessage", new MessageResponseHandler());

            client.connect("localhost", 3005);

            while (true)
            {
                Thread.Sleep(3);
                client.processEvents();
            }
        }
    }
}

Now, click run and you can see the result:

https://tvd12.com/wp-content/uploads/HelloWord-CSharp-result.png

4.Conclusion

ezyfox-server-csharp-client help you connect to ezyfox-server and you don’t need care about socket client and byte bit to send to server, everything is simple