Example #1
0
        /// <summary>
        /// Handles incoming messages from EBS.
        /// </summary>
        private void EBSOnMessage(object sender, MessageEventArgs e)
        {
            if (!e.IsText)
            {
                return;
            }

            logger.Debug(new LogReceivedMessage {
                Message = e.Data, Session = EBS_SESSION
            }.ToJson());

            string msgType = null;

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

                switch (msgType)
                {
                case "Authorization.Success":
                    // successful authorization
                    OnAuthorizationSuccess(AuthorizationSuccess.FromJson(e.Data));
                    break;

                case "PollError":
                    // Poll request got canceled
                    var pollError = PollError.FromJson(e.Data);

                    logger.Warn(new LogPollAbortedEBS {
                        Reason = pollError.Reason
                    }.ToJson());

                    pollActive = false;
                    SendMessage(Subscribers, new PollStopped().ToJson());
                    break;

                case "PollResult":
                    // poll results
                    OnPollResult(PollResult.FromJson(e.Data));

                    pollActive = false;
                    SendMessage(Subscribers, new PollStopped().ToJson());
                    break;

                case "PollStarted":
                    // poll successfully started from request
                    pollActive = true;

                    SendMessage(Subscribers, new PollStarted().ToJson());
                    break;

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

                    break;
                }
            }
            catch (Exception ex)
            {
                logger.Error(new LogMessageHandlingError
                {
                    Exception   = ex,
                    MessageType = msgType,
                    Session     = EBS_SESSION
                }.ToJson());
            }
        }