多项目解决方案中的事件驱动设计

时间:2017-09-13 13:09:45

标签: c# .net websocket event-driven event-driven-design

我尝试用iOS.Xamarin做一个iOS应用程序。我还有一个服务器,我用websockets将事件发送到我的应用程序。但是websockets函数在另一个项目中。

因此,如果服务器发送新事件,我想告诉iOS项目新事件已到达。我无法退货,因为我无法相互引用这两个项目。

如何在一个解决方案中为多个项目实施事件驱动设计?

这是我目前的代码:

iOS项目

    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);
    }
}

一般项目(我的活动到达的地方)

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 个答案:

答案 0 :(得分:1)

我为我的问题找到了一个解决方案......我创建了一个新类,我可以将结果发送回另一个项目......首先,你应该将它添加到你的主项目中:

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req).map(event => {
        if (event instanceof HttpResponse && shouldBeIntercepted(event)) {
            event = event.clone({ body: resolveReferences(event.body) })
        }         
        return event;
    });
}

这将事件名添加到SocketsEventHandlerClass中的SocketsEventHandler.Add("eventname", callback);

SocketsEventHandlerClass将如下所示:

Dictionary

当你想向主项目发射东西时,你可以简单地说:

    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) { }
    }

我希望这有助于其他人!

修改
我只是用一个例子创建一个git repo:

MultiProjectEventHandler

Enyoj它!