コード例 #1
0
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                JObject json          = JObject.Load(reader);
                string  dataJson      = json["d"].ToString(),
                        opcodeJson    = json["op"].ToString(),
                        sequenceJson  = json["s"].ToString(),
                        eventNameJson = json["t"].ToString(),
                        dataTypeFullName;
                Opcode opcode = ParseOpcode(opcodeJson);

                if (opcode == Opcode.Dispatch)
                {
                    dataTypeFullName = $"{objectType.Namespace}.Payload.DataObjects.Dispatch.{opcode}"; // TODO : откровенная писька
                }
                else if (opcode == Opcode.InvalidSession)
                {
                    IGatewayDataObject data = new InvalidSession(bool.Parse(dataJson));
                    return(new GatewayPayload(opcode, data, eventNameJson)
                    {
                        Sequence = 0
                    });
                }
                else
                {
                    dataTypeFullName = $"{objectType.Namespace}.Payload.DataObjects.{opcode}";
                }
                Type dataType = objectType.Assembly.GetType(dataTypeFullName);
                IGatewayDataObject dataObject = JsonConvert.DeserializeObject(dataJson, dataType) as IGatewayDataObject;

                int.TryParse(sequenceJson, out int sequence);
                return(new GatewayPayload(opcode, dataObject, eventNameJson)
                {
                    Sequence = sequence
                });
            }
コード例 #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GatewayInvalidSessionMessage"/> class.
 /// </summary>
 /// <param name="packet">Invalid session data.</param>
 public GatewayInvalidSessionMessage(InvalidSession packet)
 {
     EventData = packet;
 }
コード例 #3
0
ファイル: GatewayEvent.cs プロジェクト: hujgup/DiscordApi
        public static object Create(string eventName, object data)
        {
            object res;

            if (Ready.EventIsThis(eventName))
            {
                res = new Ready((JObject)data);
            }
            else if (Resumed.EventIsThis(eventName))
            {
                res = new Resumed((JObject)data);
            }
            else if (InvalidSession.EventIsThis(eventName))
            {
                res = (bool)((JValue)data).Value;
            }
            else if (Channel.EventIsThis(eventName))
            {
                res = new Channel((JObject)data);
            }
            else if (ChannelPinsUpdate.EventIsThis(eventName))
            {
                res = new ChannelPinsUpdate((JObject)data);
            }
            else if (EvtServer.EventIsThis(eventName))
            {
                res = new EvtServer((JObject)data);
            }
            else if (ServerDelete.EventIsThis(eventName))
            {
                res = ((JObject)data).ToObject <UaServerJson>().id;
            }
            else if (ServerUser.EventIsThis(eventName))
            {
                res = new ServerUser((JObject)data, eventName == ServerUser.MemberRemoveId);
            }
            else if (ServerUserSet.EventIsThis(eventName))
            {
                res = new ServerUserSet((JObject)data);
            }
            else if (ServerUserUpdate.EventIsThis(eventName))
            {
                res = new ServerUserUpdate((JObject)data);
            }
            else if (ServerEmojiUpdate.EventIsThis(eventName))
            {
                res = new ServerEmojiUpdate((JObject)data);
            }
            else if (RawServer.EventIsThis(eventName))
            {
                res = new RawServer((JObject)data);
            }
            else if (ServerRole.EventIsThis(eventName))
            {
                res = new ServerRole((JObject)data);
            }
            else if (ServerRoleDelete.EventIsThis(eventName))
            {
                res = new ServerRoleDelete((JObject)data);
            }
            else if (Message.EventIsThis(eventName))
            {
                res = new Message((JObject)data, eventName == Message.UpdateId);
            }
            else if (RawMessage.EventIsThis(eventName))
            {
                res = new RawMessage((JObject)data, eventName == RawMessage.BulkDeleteId);
            }
            else if (Reaction.EventIsThis(eventName))
            {
                res = new Reaction((JObject)data);
            }
            else if (ReactionRemoveAll.EventIsThis(eventName))
            {
                res = new ReactionRemoveAll((JObject)data);
            }
            else if (Webhook.EventIsThis(eventName))
            {
                res = new Webhook((JObject)data);
            }
            else if (EvtUser.EventIsThis(eventName))
            {
                res = new EvtUser((JObject)data);
            }
            else if (Unimplemented.EventIsThis(eventName))
            {
                res = Unimplemented.Instance;
            }
            else
            {
                throw new UnknownEventException("Gateway event \"" + eventName + "\" is unknown.");
            }
            DiscordDebug.WriteLine("Heard event: " + eventName + ", handling w/ object " + res.GetType().Name);
            return(res);
        }
コード例 #4
0
        private void Callback(string response, string eventName, string opCode)
        {
            if (!string.IsNullOrEmpty(response))
            {
                if (!string.IsNullOrEmpty(eventName))
                {
                    switch (eventName)
                    {
                    case Globals.Events.GuildCreate:
                        socket.Handler.GuildCreated(response);
                        break;

                    case Globals.Events.GuildMemberAdd:
                        socket.Handler.GuildMemberAdded(response);
                        break;

                    case Globals.Events.GuildMemberRemove:
                        socket.Handler.GuildMemberRemoved(response);
                        break;

                    case Globals.Events.GuildMemberUpdate:
                        socket.Handler.GuildMemberUpdated(response);
                        break;

                    case Globals.Events.MessageCreate:
                        socket.Handler.MessageReceived(response);
                        break;

                    case Globals.Events.PresenceUpdate:
                        socket.Handler.PresenceUpdated(response);
                        break;

                    case Globals.Events.Ready:
                        socket.Handler.IsReady(response);
                        // Update socket's cache:
                        socket.Cache.ReadyResponse = serializer.Deserialize <ReadyResponse>(response);
                        break;

                    case Globals.Events.Resumed:
                        socket.Handler.Resumed(response);
                        break;

                    case Globals.Events.TypingStart:
                        socket.Handler.TypingStarted(response);
                        break;
                    }
                }

                int  op;
                bool parsed = int.TryParse(opCode, out op);
                if (!string.IsNullOrEmpty(opCode) && parsed)
                {
                    switch (op)
                    {
                    // Heartbeat request from WebSocket server:
                    case ((int)OpCodes.Heartbeat):
                        socket.Handler.HeartbeatRequested(response);
                        break;

                    case ((int)OpCodes.HeartbeatAcknowledged):
                        socket.Handler.HeartbeatAcknowledged(response);
                        break;

                    case ((int)OpCodes.Reconnect):
                            #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                        socket.Resume();
                            #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                        break;

                    case ((int)OpCodes.InvalidSession):
                        InvalidSession session = serializer.Deserialize <InvalidSession>(response);
                        if (session.Resumable)
                        {
                                #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                            socket.Resume();
                                #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                        }
                        else
                        {
                                #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                            // TODO: Remove hard-coded shard info:
                            socket.Identify("", 0, 1);
                                #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                        }
                        break;
                    }
                }
            }
        }