Esempio n. 1
0
        private void HandleMessage(object sender, DiscordWebSocketMessage <DiscordMediaOpcode> message)
        {
            switch (message.Opcode)
            {
            case DiscordMediaOpcode.Ready:
                DiscordMediaReady ready = message.Data.ToObject <DiscordMediaReady>();

                SSRC = new DiscordSSRC()
                {
                    Audio = ready.SSRC
                };

                ServerEndpoint = new IPEndPoint(IPAddress.Parse(ready.IP), ready.Port);

                UdpClient = new UdpClient();
                UdpClient.Connect(ServerEndpoint);

                Task.Run(() => StartListener());

                Holepunch();
                break;

            case DiscordMediaOpcode.SessionDescription:
                var description = message.Data.ToObject <DiscordSessionDescription>();

                SecretKey = description.SecretKey;

                State = MediaConnectionState.Ready;
                OnReady?.Invoke(this);
                break;

            case DiscordMediaOpcode.Hello:
                Send(DiscordMediaOpcode.Identify, new DiscordMediaIdentify()
                {
                    ServerId  = _serverId,
                    UserId    = _parentClient.User.Id,
                    SessionId = _parentClient.SessionId,
                    Token     = _server.Token,
                    Video     = true
                });

                StartHeartbeaterAsync(message.Data.Value <int>("heartbeat_interval"));
                break;

            default:
                OnMessage?.Invoke(this, message);
                break;
            }
        }
Esempio n. 2
0
        private void OnMessageReceived(object sender, DiscordWebSocketMessage <DiscordMediaOpcode> message)
        {
            try
            {
                switch (message.Opcode)
                {
                case DiscordMediaOpcode.Ready:
                    DiscordMediaReady ready = message.Data.ToObject <DiscordMediaReady>();

                    SSRC = new DiscordSSRC()
                    {
                        Audio = ready.SSRC
                    };

                    _serverEndpoint = new IPEndPoint(IPAddress.Parse(ready.IP), ready.Port);

                    UdpClient = new UdpClient();
                    UdpClient.Connect(_serverEndpoint);

                    Task.Run(() => StartListener());

                    Holepunch();
                    break;

                case DiscordMediaOpcode.SessionDescription:
                    var description = message.Data.ToObject <DiscordSessionDescription>();

                    if (ValidateCodecs(description))
                    {
                        if (description.EncryptionMode != Sodium.EncryptionMode)
                        {
                            Disconnect(DiscordMediaCloseCode.InvalidEncryptionMode, "Unexpected encryption mode: " + description.EncryptionMode);
                        }
                        else
                        {
                            SecretKey = description.SecretKey;
                            State     = MediaSessionState.Authenticated;

                            Log("Authenticated");

                            Task.Run(() => HandleConnect());
                        }
                    }
                    break;

                case DiscordMediaOpcode.ChangeCodecs:
                    // apparently this triggers whenever u switch channel
                    // im confused
                    //var codecs = message.Data.ToObject<MediaCodecSelection>();
                    break;

                case DiscordMediaOpcode.Hello:
                    WebSocket.Send(DiscordMediaOpcode.Identify, new DiscordMediaIdentify()
                    {
                        ServerId  = GetServerId(),
                        UserId    = Client.User.Id,
                        SessionId = Client.SessionId,
                        Token     = CurrentServer.Token,
                        Video     = true
                    });

                    WebSocket.StartHeartbeaterAsync(message.Data.Value <int>("heartbeat_interval"));
                    break;

                default:
                    HandleMessage(message);
                    break;
                }
            }
            catch (InvalidOperationException) { }
        }