Exemple #1
0
        // Also handles (un)subscription to make switching between this and WS easier.
        public void callHandler(string data, object callback)
        {
            Task.Run(() => {
                try
                {
                    // Tell the overlay that the page is using the modern API.
                    receiver.InitModernAPI();

                    var message = JObject.Parse(data);
                    if (!message.ContainsKey("call"))
                    {
                        PluginMain.Logger.Log(LogLevel.Error, Resources.OverlayApiInvalidHandlerCall, data);
                        return;
                    }

                    var handler = message["call"].ToString();
                    if (handler == "subscribe")
                    {
                        if (!message.ContainsKey("events"))
                        {
                            PluginMain.Logger.Log(LogLevel.Error, Resources.OverlayApiMissingEventsField, data);
                            return;
                        }

                        foreach (var name in message["events"].ToList())
                        {
                            EventDispatcher.Subscribe(name.ToString(), (IEventReceiver)receiver);
                            PluginMain.Logger.Log(LogLevel.Debug, Resources.OverlayApiSubscribed, receiver.Name, name.ToString());
                        }
                        return;
                    }
                    else if (handler == "unsubscribe")
                    {
                        if (!message.ContainsKey("events"))
                        {
                            PluginMain.Logger.Log(LogLevel.Error, Resources.OverlayApiMissingEventsFieldUnsub, data);
                            return;
                        }

                        foreach (var name in message["events"].ToList())
                        {
                            EventDispatcher.Unsubscribe(name.ToString(), (IEventReceiver)receiver);
                        }
                        return;
                    }

                    var result = EventDispatcher.CallHandler(message);
                    if (result != null && result.Type != JTokenType.Object)
                    {
                        throw new Exception("Handler response must be an object or null");
                    }
                    Renderer.ExecuteCallback(callback, result == null ? null : result.ToString(Newtonsoft.Json.Formatting.None));
                }
                catch (Exception e)
                {
                    PluginMain.Logger.Log(LogLevel.Error, Resources.JsHandlerCallException, e);
                }
            });
        }
Exemple #2
0
        // Also handles (un)subscription to make switching between this and WS easier.
        public void callHandler(string data, object callback)
        {
            Task.Run(() => {
                try
                {
                    // Tell the overlay that the page is using the modern API.
                    receiver.InitModernAPI();

                    var message = JObject.Parse(data);
                    if (!message.ContainsKey("call"))
                    {
                        PluginMain.Logger.Log(LogLevel.Error, $"Received invalid handler call: {data}");
                        return;
                    }

                    var handler = message["call"].ToString();
                    if (handler == "subscribe")
                    {
                        if (!message.ContainsKey("events"))
                        {
                            PluginMain.Logger.Log(LogLevel.Error, $"Missing events field in subscribe call: {data}!");
                            return;
                        }

                        foreach (var name in message["events"].ToList())
                        {
                            EventDispatcher.Subscribe(name.ToString(), (IEventReceiver)receiver);
                            PluginMain.Logger.Log(LogLevel.Debug, "{0}: Subscribed to {1}", receiver.Name, name.ToString());
                        }
                        return;
                    }
                    else if (handler == "unsubscribe")
                    {
                        if (!message.ContainsKey("events"))
                        {
                            PluginMain.Logger.Log(LogLevel.Error, $"Missing events field in unsubscribe call: {data}!");
                            return;
                        }

                        foreach (var name in message["events"].ToList())
                        {
                            EventDispatcher.Unsubscribe(name.ToString(), (IEventReceiver)receiver);
                        }
                        return;
                    }

                    var result = EventDispatcher.CallHandler(message);
                    Renderer.ExecuteCallback(callback, result == null ? null : result.ToString(Newtonsoft.Json.Formatting.None));
                }
                catch (Exception e)
                {
                    PluginMain.Logger.Log(LogLevel.Error, $"JS Handler call failed: {e}");
                }
            });
        }