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; } }
internal void OnConnection(IGatewayDataObject data) { Hello hello = data as Hello; heartbeatRate = hello.HeartbeatInterval; heart.Start(); }
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 }); }
private async void OnInvalidSessionRecieved(IGatewayDataObject data) { // В документации дискорда сказано, что при получении InvaidSession необходимо // подождать 2-5 секунд и отправить Identify. await Task.Delay(5000); await AuthorizeAsync(); }
internal void OnInvalidSessionReceived(IGatewayDataObject payload) { }
internal void OnReconnectRequested(IGatewayDataObject payload) { Reconnect(); }
internal void OnHeartbeatAck(IGatewayDataObject data) => heartbeatAckReceived = true;
internal GatewayPayload(Opcode opcode, IGatewayDataObject data, string eventName = null) { Opcode = opcode; Data = data; EventName = eventName; }