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;
}
}
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:
This add the eventname to the
Dictionary
in the SocketsEventHandlerClass.The SocketsEventHandlerClass will look like this:
And when you want to fire something to the main project you can simply say:
I hope this helps some others!
Edit:
I just create a git repo with an example:
MultiProjectEventHandler
Enyoj it!