コード例 #1
0
        internal void OnNewSystemEventReceived(Opcode opcode, IGatewayDataObject data)
        {
            Console.WriteLine($"System: {opcode}");
            switch (opcode)
            {
            case Opcode.Heartbeat:
                Heartbeat(data);
                break;

            case Opcode.Reconnect:
                ReconnectRequested(data);
                break;

            case Opcode.InvalidSession:
                InvalidSession(data);
                break;

            case Opcode.Hello:
                Connected(data);
                break;

            case Opcode.HeartbeatACK:
                HeartbeatACK(data);
                break;

            default:
                // TODO : интрсумент логирования ($"Unhandled system event opcode. /n Event name: {opcode}");
                break;
            }
        }
コード例 #2
0
        internal void OnConnection(IGatewayDataObject data)
        {
            Hello hello = data as Hello;

            heartbeatRate = hello.HeartbeatInterval;
            heart.Start();
        }
コード例 #3
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
                });
            }
コード例 #4
0
        private async void OnInvalidSessionRecieved(IGatewayDataObject data)
        {
            // В документации дискорда сказано, что при получении InvaidSession необходимо
            // подождать 2-5 секунд и отправить Identify.
            await Task.Delay(5000);

            await AuthorizeAsync();
        }
コード例 #5
0
 internal void OnInvalidSessionReceived(IGatewayDataObject payload)
 {
 }
コード例 #6
0
 internal void OnReconnectRequested(IGatewayDataObject payload)
 {
     Reconnect();
 }
コード例 #7
0
 internal void OnHeartbeatAck(IGatewayDataObject data) => heartbeatAckReceived = true;
コード例 #8
0
 internal GatewayPayload(Opcode opcode, IGatewayDataObject data, string eventName = null)
 {
     Opcode    = opcode;
     Data      = data;
     EventName = eventName;
 }