예제 #1
0
파일: IPC.cs 프로젝트: kant2002/WebWindow
 public void Send(string eventName, params object[] args)
 {
     try
     {
         _webWindow.Invoke(() =>
         {
             _webWindow.SendMessage($"{eventName}:{JsonSerializer.Serialize(args)}");
         });
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
 }
예제 #2
0
        public async void OnRequest(object sender, string message)
        {
            var request      = new Request(JObject.Parse(message));
            var response     = new Response();
            var isValidRoute = _routes.ContainsKey(request.GetPath());

            if (isValidRoute)
            {
                try
                {
                    var route = _routes[request.GetPath()];
                    foreach (var behaviour in route.Behaviours)
                    {
                        await behaviour.Handle(request);
                    }
                    response = await route.Handler(request);
                }
                catch (Exception e)
                {
                    int code = 502;
                    if (e.Data.Contains("code"))
                    {
                        code = (int)e.Data["code"];
                    }
                    response = request.RespondWithCode(code, e.Message);
                }
            }
            else
            {
                response = request.RespondWithCode(404, $"Path {request.GetPath()} not found.");
            }
            _webview.Invoke(() =>
            {
                _webview.SendMessage(response.ToString());
            });
        }