Esempio n. 1
0
        protected override void OnMessage(MessageEventArgs e)
        {
            if (!e.IsText)
            {
                return;
            }

            var msg = e.Data;

            logger.Debug(new LogReceivedMessage
            {
                Message = msg,
                Session = ID
            }.ToJson());

            string msgType = null;

            try
            {
                msgType = JObject.Parse(msg)["type"]?.ToObject <string>();

                switch (msgType)
                {
                case "Authorization":
                    OnAuthorization?.Invoke(this, Authorization.FromJson(msg));
                    return;

                case "Client.Initialized":
                    OnClientInitialized?.Invoke(this, ClientInitialized.FromJson(msg));
                    return;

                case "Client.Register":
                    OnClientRegister?.Invoke(this, ClientRegister.FromJson(msg));
                    return;

                case "Config.Change":
                    if (Configurer)
                    {
                        OnConfigChange?.Invoke(this, ConfigChange.FromJson(msg));
                    }

                    return;

                case "Config.Register":
                    Configurer = true;
                    Subscriber = true;
                    OnConfigRegister?.Invoke(this);
                    return;

                case "Config.Start":
                    if (Configurer)
                    {
                        OnConfigStart?.Invoke(this);
                    }

                    return;

                case "Config.Stop":
                    if (Configurer)
                    {
                        OnConfigStop?.Invoke(this);
                    }

                    return;

                case "Execution.StartRequest":
                    OnExecutionRequest?.Invoke(this, ExecutionStartRequest.FromJson(msg));
                    return;

                case "Execution.Started":
                    OnExecutionStarted?.Invoke(this, ExecutionStarted.FromJson(msg));
                    return;

                case "Execution.Stopped":
                    OnExecutionStopped?.Invoke(this, ExecutionStopped.FromJson(msg));
                    return;

                case "Info.Message":
                    OnInfo?.Invoke(this, InfoMessage.FromJson(msg));
                    return;

                case "Info.Subscribe":
                    if (!Subscriber)
                    {
                        Subscriber = true;
                        OnSubscribe?.Invoke(this);
                    }

                    return;

                case "Info.Unsubscribe":
                    Subscriber = false;
                    return;

                default:
                    logger.Info(new LogReceivedUnknownMessageType
                    {
                        MessageType = msgType,
                        Session     = ID
                    }.ToJson());

                    return;
                }
            }
            catch (Exception ex)
            {
                //logger.Error()
                logger.Error(new LogMessageHandlingError
                {
                    Exception   = ex,
                    MessageType = msgType,
                    Session     = ID
                }.ToJson());
            }
        }