Event driven design in a multiple project solution

178 views Asked by At

I try to do an iOS application with Xamarin.ios. I have a server where I can send events to my App using websockets. But the functions to handle this events are implemented in another project.

So if the server sends a new event I want to tell the iOS project that a new event is arrived. I can't return it because I can't reference both projects to each other.

How can I implement an event driven design for multiple projects in one solution?

Here is my current code:

iOS Project

    public partial class LobbyViewController : UIViewController
{


    public LobbyViewController (IntPtr handle) : base (handle)
    { 
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();                   
    }

    public void changePage()
    {
        UIViewController introViewController = Storyboard.InstantiateViewController("IntroViewController") as IntroViewController;
        introViewController.ModalTransitionStyle = UIModalTransitionStyle.CoverVertical;
        PresentViewController(introViewController, true, null);
    }
}

General Project(where my events arriving)

public static class Sockets
{
    public static Socket socket;
    static bool isConnected = false;

    public static void Main(string token)
    {
        socket = connect(token);
    }

    private static Socket connect(string Token)
    {
        var options = new IO.Options();
        options.Query = new Dictionary<string, string>()
        {
            { "token", Token }
        };

        var socket = IO.Socket(Resource.SocketsUrl, options);

        socket.On(Socket.EVENT_CONNECT, () =>
        {
            isConnected = true;
        });

        socket.On(Socket.EVENT_ERROR, (error) =>
        {
            appendLogMessage("ERROR! " + error.ToString(), true);
        });

        socket.On(Socket.EVENT_DISCONNECT, (data) =>
        {
            isConnected = false;
            appendLogMessage("Disconected");
        });

        socket.On(Socket.EVENT_MESSAGE, (data) =>
        {
            appendLogMessage(data.ToString(), true);
        });

        socket.On("lobbyParticipantAdded", (data) =>
        {
            appendLogMessage(data.ToString(), true);
        });

        socket.On("lobbyFlowStart", (data) =>
        {
            appendLogMessage(data.ToString(), true);
        });

        socket.On("quizQuestion", (data) =>
        {
            appendLogMessage(data.ToString(), true);
        });

        socket.On("gameEnd", (data) =>
        {
            appendLogMessage(data.ToString(), true);
        });

        return socket;
    }
 }
1

There are 1 answers

0
DerStarkeBaer On BEST ANSWER

I found a solution for my Problem... I create a new class where I can fire my results back to the other project... First you should add this to your Main project:

SocketsEventHandler.Add("eventname", callback);

This add the eventname to the Dictionary in the SocketsEventHandlerClass.

The SocketsEventHandlerClass will look like this:

    public class SocketEventHandler
{
    static SocketEventHandler()
    {
        callbackList = new Dictionary<string, List<Action<dynamic>>>();
    }

    public static Dictionary<string, List<Action<dynamic>>> callbackList;

    public static void Add(string eventName, Action<dynamic> callback)
    {
        if (callbackList.ContainsKey(eventName))
        {
            callbackList[eventName].Add(callback);
        }
        else
        {
            callbackList.Add(eventName, new List<Action<dynamic>> { callback });
        }
    }

    public static void Fire(string eventName, dynamic payload)
    {
        try
        {
            List<Action<dynamic>> actions = callbackList[eventName];

            foreach (var item in actions)
            {
                item(payload);
            }
        }
        catch (Exception e) { }
    }

And when you want to fire something to the main project you can simply say:

SocketEventHandler.Fire("eventname", payload);

I hope this helps some others!

Edit:
I just create a git repo with an example:

MultiProjectEventHandler

Enyoj it!