コード例 #1
0
        public static ResponseMessage Parse(WsAPI context, JObject jsonObj)
        {
            string tp = jsonObj.GetValue <string>("type");

            if (string.IsNullOrEmpty(tp))
            {
                throw new ArgumentException($"invalid response format");
            }

            MessageType msgTp = (MessageType)Enum.Parse(typeof(MessageType), tp, true);

            ResponseMessage responseMsg;

            if (msgTp == MessageType.Auth_invalid ||
                msgTp == MessageType.Auth_ok ||
                msgTp == MessageType.Auth_required)
            {
                responseMsg           = new AuthResponseMessage(msgTp);
                responseMsg.IsSuccess = msgTp == MessageType.Auth_invalid;
            }
            else
            {
                int?id = jsonObj.GetValue <int?>("id");

                responseMsg = new ResponseMessage(id, msgTp);

                responseMsg.IsSuccess = jsonObj.GetValue <bool?>("success");

                JObject errObj = jsonObj.GetValue <JObject>("error");

                if (errObj != null)
                {
                    responseMsg.Error = new Error(
                        errObj.GetValue <int>("error"),
                        errObj.GetValue <string>("message"));
                }
            }

            responseMsg.RequestMessage = context?.FindRequestMessageOrDefault(responseMsg.Id.GetValueOrDefault());

            if (responseMsg.RequestMessage?.Type == RequestMessage.MessageType.Get_states)
            {
                responseMsg.States = jsonObj
                                     .GetValue <JObject[]>("result")
                                     ?.Select(state => StateResult.Parse(state))
                                     .ToArray();

                responseMsg.Result = responseMsg.States;
            }
            else if (responseMsg.RequestMessage?.Type == RequestMessage.MessageType.Subscribe_events)
            {
                responseMsg.Event  = EventResult.Parse(jsonObj.GetValue <JObject>("event"));
                responseMsg.Result = responseMsg.Event;
            }
            else
            {
                responseMsg.Result = jsonObj.GetValue <object>("result");
            }

            return(responseMsg);
        }
コード例 #2
0
 private void OnStateChanged(EventResult eventResult)
 {
     StateChanged?.Invoke(this, new StateChangedEventArgs(eventResult));
 }
コード例 #3
0
 public StateChangedEventArgs(EventResult @event)
 {
     Event = @event;
 }