예제 #1
0
        internal override Task HandleResponse(Response response)
        {
            if (response.OpCode == (int)VoiceOpCodeTypes.Ready)
            {
                var ready = response.DeserializeDataPayload <VoiceReadyPayload>();
                readyResponseSource.SetResult(ready);
            }
            else if (response.OpCode == (int)VoiceOpCodeTypes.Heartbeat)
            {
                // server requesting a heartbeat
                heart.BeatRequested();
            }
            else if (response.OpCode == (int)VoiceOpCodeTypes.SessionDescription)
            {
                var sessionDescription = response.DeserializeDataPayload <VoiceSessionDescriptionPayload>();
                sessionDescriptionResponseSource.SetResult(sessionDescription);
            }
            else if (response.OpCode == (int)VoiceOpCodeTypes.Speaking)
            {
                // TODO: update speaking status / allow user handlers for this
            }
            else if (response.OpCode == (int)VoiceOpCodeTypes.Resumed)
            {
                // TODO: voice gateway resuming
            }
            else if (response.OpCode == (int)VoiceOpCodeTypes.Hello)
            {
                // should never happen
                throw new WebSocketException("Received 8 Hello after already connected.");
            }
            else if (response.OpCode == (int)VoiceOpCodeTypes.HeartbeatAck)
            {
                // forward on to the heart
                heart.AckReceived();
            }
            else if (response.OpCode == (int)VoiceOpCodeTypes.ClientDisconnect)
            {
                // API description: a client has disconnected from the voice channel
                // Not sure exactly what this kind of response is, though.
                // TODO
            }
            else
            {
                // invalid receiving opcode
                throw new WebSocketException($"Received unknown message from server: opcode {response.OpCode}");
            }

            return(Task.CompletedTask);
        }
예제 #2
0
        internal override Task HandleResponse(Response response)
        {
            if (response.SequenceNumber.HasValue)
            {
                sequenceNumber = response.SequenceNumber;
            }

            if (response.OpCode == (int)MainOpCodeTypes.Dispatch)
            {
                // event dispatch
                if (response.EventName == EventType.Ready.Name)
                {
                    var ready = response.DeserializeDataPayload <ReadyPayload>();
                    readyResponseSource.SetResult(ready);
                }
                else if (response.EventName == EventType.VoiceStateUpdate.Name)
                {
                    var voiceStateUpdate = response.DeserializeDataPayload <VoiceState>();
                    if (voiceStateUpdate.UserId == currentUser.Id && voiceGateway != null)
                    {
                        voiceGateway.InitialVoiceStateUpdateSource.SetResult(voiceStateUpdate);
                    }
                    else
                    {
                        _log.Info($"User {voiceStateUpdate.UserId} changed voice channel.");
                    }
                }
                else if (response.EventName == EventType.VoiceServerUpdate.Name)
                {
                    var voiceServerUpdate = response.DeserializeDataPayload <VoiceServerUpdatePayload>();
                    if (voiceGateway != null)
                    {
                        voiceGateway.InitialVoiceServerUpdateSource.SetResult(voiceServerUpdate);
                    }
                }
                else if (messageHandlers.Keys.Any(k => k.Name == response.EventName))
                {
                    // found a user-specified handler
                    var eventType = messageHandlers.Keys.Single(handledEventType => handledEventType.Name == response.EventName);
                    foreach (var handler in messageHandlers[eventType])
                    {
                        handler.Invoke(response.DeserializeDataPayload(eventType.PayloadType)).ConfigureAwait(continueOnCapturedContext: false);
                    }
                }
                else
                {
                    _log.Info($"Unhandled event type: {response.EventName}");
                }
            }
            else if (response.OpCode == (int)MainOpCodeTypes.HeartbeatMsg)
            {
                // server requesting a heartbeat
                heart.BeatRequested();
            }
            else if (response.OpCode == (int)MainOpCodeTypes.Reconnect)
            {
                // TODO: client must reconnect
            }
            else if (response.OpCode == (int)MainOpCodeTypes.InvalidSession)
            {
                // could receive this for a lot of reasons, TODO: some of them can be handled
                throw new WebSocketException("Received 9 Invalid Session");
            }
            else if (response.OpCode == (int)MainOpCodeTypes.Hello)
            {
                // should never happen
                throw new WebSocketException("Received 10 Hello after already connected.");
            }
            else if (response.OpCode == (int)MainOpCodeTypes.HeartbeatAck)
            {
                // forward on to the heart
                heart.AckReceived();
            }
            else
            {
                // invalid receiving opcode
                throw new WebSocketException($"Received unknown message from server: opcode {response.OpCode}");
            }

            return(Task.CompletedTask);
        }