internal WebSocketClosedEventArgs(WebSocketClosedEvent closedEvent)
 {
     GuildId  = closedEvent.GuildId;
     Code     = closedEvent.Code;
     Reason   = closedEvent.Reason;
     ByRemote = closedEvent.ByRemote;
 }
Пример #2
0
        private void ProcessEvent(ref Utf8JsonReader reader, out BaseEventResponse eventResponse)
        {
            //{"op":"event","reason":"FINISHED","type":"TrackEndEvent","track":"QAAAcwIADUxhdGUgRm9y...","guildId":"522440206494728203"}

            var dictionary = new Dictionary <string, string>();

            while (reader.Read())
            {
                if (reader.TokenType == JsonTokenType.EndObject)
                {
                    break;
                }

                if (reader.TokenType != JsonTokenType.PropertyName)
                {
                    continue;
                }

                var propName = reader.ValueSpan;
                reader.Read();
                var propValue = reader.ValueSpan;

                dictionary.Add(Encoding.UTF8.GetString(propName), Encoding.UTF8.GetString(propValue));
            }

            if (!dictionary.TryGetValue("type", out var type))
            {
                eventResponse = default;
                return;
            }

            switch (type)
            {
            case "TrackEndEvent":
                eventResponse = new TrackEndEvent {
                    GuildId = ulong.Parse(dictionary["guildId"]),
                    Reason  = (TrackEndReason)dictionary["reason"][0],
                    Hash    = dictionary["track"]
                };
                break;

            case "TrackExceptionEvent":
                eventResponse = new TrackExceptionEvent {
                    GuildId = ulong.Parse(dictionary["guildId"]),
                    Hash    = dictionary["track"],
                    Error   = dictionary["error"]
                };
                break;

            case "TrackStuckEvent":
                eventResponse = new TrackStuckEvent {
                    GuildId     = ulong.Parse(dictionary["guildId"]),
                    Hash        = dictionary["track"],
                    ThresholdMs = long.Parse(dictionary["thresholdMs"])
                };
                break;

            case "WebSocketClosedEvent":
                eventResponse = new WebSocketClosedEvent {
                    GuildId  = ulong.Parse(dictionary["guildId"]),
                    Code     = int.Parse(dictionary["code"]),
                    Reason   = dictionary["reason"],
                    ByRemote = bool.Parse(dictionary["byRemote"])
                };
                break;

            default:
                eventResponse = default;
                break;
            }
        }