Пример #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);
                }
            });
        }
Пример #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}");
                }
            });
        }
Пример #3
0
            protected override void OnMessage(MessageEventArgs e)
            {
                JObject data = null;

                try
                {
                    data = JObject.Parse(e.Data);
                }
                catch (JsonException ex)
                {
                    Log(LogLevel.Error, Resources.WSInvalidDataRecv, ex, e.Data);
                    return;
                }

                if (!data.ContainsKey("call"))
                {
                    return;
                }

                var msgType = data["call"].ToString();

                if (msgType == "subscribe")
                {
                    try
                    {
                        foreach (var item in data["events"].ToList())
                        {
                            EventDispatcher.Subscribe(item.ToString(), this);
                        }
                    } catch (Exception ex)
                    {
                        Log(LogLevel.Error, Resources.WSNewSubFail, ex);
                    }

                    return;
                }
                else if (msgType == "unsubscribe")
                {
                    try
                    {
                        foreach (var item in data["events"].ToList())
                        {
                            EventDispatcher.Unsubscribe(item.ToString(), this);
                        }
                    } catch (Exception ex)
                    {
                        Log(LogLevel.Error, Resources.WSUnsubFail, ex);
                    }
                    return;
                }

                Task.Run(() => {
                    try
                    {
                        var response = EventDispatcher.CallHandler(data);

                        if (response != null && response.Type != JTokenType.Object)
                        {
                            throw new Exception("Handler response must be an object or null");
                        }

                        if (response == null)
                        {
                            response            = new JObject();
                            response["$isNull"] = true;
                        }

                        if (data.ContainsKey("rseq"))
                        {
                            response["rseq"] = data["rseq"];
                        }

                        Send(response.ToString(Formatting.None));
                    } catch (Exception ex)
                    {
                        Log(LogLevel.Error, Resources.WSHandlerException, ex);
                    }
                });
            }
Пример #4
0
            protected override void OnMessage(MessageEventArgs e)
            {
                JObject data = null;

                try
                {
                    data = JObject.Parse(e.Data);
                }
                catch (JsonException ex)
                {
                    Log(LogLevel.Error, "Invalid data received: {0}; {1}", ex, e.Data);
                    return;
                }

                if (!data.ContainsKey("call"))
                {
                    return;
                }

                var msgType = data["call"].ToString();

                if (msgType == "subscribe")
                {
                    try
                    {
                        foreach (var item in data["events"].ToList())
                        {
                            EventDispatcher.Subscribe(item.ToString(), this);
                        }
                    } catch (Exception ex)
                    {
                        Log(LogLevel.Error, "Failed to process new subscription: {0}", ex);
                    }

                    return;
                }
                else if (msgType == "unsubscribe")
                {
                    try
                    {
                        foreach (var item in data["events"].ToList())
                        {
                            EventDispatcher.Unsubscribe(item.ToString(), this);
                        }
                    } catch (Exception ex)
                    {
                        Log(LogLevel.Error, "Failed to process unsubscription: {0}", ex);
                    }
                    return;
                }

                Task.Run(() => {
                    try
                    {
                        var response = EventDispatcher.CallHandler(data);

                        if (response == null)
                        {
                            response            = new JObject();
                            response["$isNull"] = true;
                        }

                        if (data.ContainsKey("rseq"))
                        {
                            response["rseq"] = data["rseq"];
                        }

                        Send(response.ToString(Formatting.None));
                    } catch (Exception ex)
                    {
                        Log(LogLevel.Error, "WS: Handler call failed: {0}", ex);
                    }
                });
            }