Skip to content

Commit

Permalink
api: upgrade audio client to VoiceWS v3
Browse files Browse the repository at this point in the history
  • Loading branch information
foxbot committed May 28, 2018
1 parent 64d8938 commit 9ba38d7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
10 changes: 10 additions & 0 deletions src/Discord.Net.WebSocket/API/Voice/HelloEvent.cs
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace Discord.API.Voice
{
internal class HelloEvent
{
[JsonProperty("heartbeat_interval")]
public int HeartbeatInterval { get; set; }
}
}
4 changes: 3 additions & 1 deletion src/Discord.Net.WebSocket/API/Voice/ReadyEvent.cs
@@ -1,5 +1,6 @@
#pragma warning disable CS1591
#pragma warning disable CS1591
using Newtonsoft.Json;
using System;

namespace Discord.API.Voice
{
Expand All @@ -14,6 +15,7 @@ internal class ReadyEvent
[JsonProperty("modes")]
public string[] Modes { get; set; }
[JsonProperty("heartbeat_interval")]
[Obsolete("This field is errorneous and should not be used", true)]
public int HeartbeatInterval { get; set; }
}
}
16 changes: 12 additions & 4 deletions src/Discord.Net.WebSocket/API/Voice/VoiceOpCode.cs
@@ -1,4 +1,4 @@
#pragma warning disable CS1591
#pragma warning disable CS1591
namespace Discord.API.Voice
{
internal enum VoiceOpCode : byte
Expand All @@ -11,11 +11,19 @@ internal enum VoiceOpCode : byte
Ready = 2,
/// <summary> C→S - Used to keep the connection alive and measure latency. </summary>
Heartbeat = 3,
/// <summary> C←S - Used to reply to a client's heartbeat. </summary>
HeartbeatAck = 3,
/// <summary> C←S - Used to provide an encryption key to the client. </summary>
SessionDescription = 4,
/// <summary> C↔S - Used to inform that a certain user is speaking. </summary>
Speaking = 5
Speaking = 5,
/// <summary> C←S - Used to reply to a client's heartbeat. </summary>
HeartbeatAck = 6,
/// <summary> C→S - Used to resume a connection. </summary>
Resume = 7,
/// <summary> C←S - Used to inform the client the heartbeat interval. </summary>
Hello = 8,
/// <summary> C←S - Used to acknowledge a resumed connection. </summary>
Resumed = 9,
/// <summary> C←S - Used to notify that a client has disconnected. </summary>
ClientDisconnect = 13,
}
}
14 changes: 10 additions & 4 deletions src/Discord.Net.WebSocket/Audio/AudioClient.cs
@@ -1,4 +1,4 @@
using Discord.API.Voice;
using Discord.API.Voice;
using Discord.Audio.Streams;
using Discord.Logging;
using Discord.Net.Converters;
Expand Down Expand Up @@ -107,7 +107,7 @@ public async Task StopAsync()
private async Task OnConnectingAsync()
{
await _audioLogger.DebugAsync("Connecting ApiClient").ConfigureAwait(false);
await ApiClient.ConnectAsync("wss://" + _url).ConfigureAwait(false);
await ApiClient.ConnectAsync("wss://" + _url + "?v=3").ConfigureAwait(false);
await _audioLogger.DebugAsync("Listening on port " + ApiClient.UdpPort).ConfigureAwait(false);
await _audioLogger.DebugAsync("Sending Identity").ConfigureAwait(false);
await ApiClient.SendIdentityAsync(_userId, _sessionId, _token).ConfigureAwait(false);
Expand Down Expand Up @@ -216,6 +216,14 @@ private async Task ProcessMessageAsync(VoiceOpCode opCode, object payload)
{
switch (opCode)
{
case VoiceOpCode.Hello:
{
await _audioLogger.DebugAsync("Received Hello").ConfigureAwait(false);
var data = (payload as JToken).ToObject<HelloEvent>(_serializer);

_heartbeatTask = RunHeartbeatAsync(data.HeartbeatInterval, _connection.CancelToken);
}
break;
case VoiceOpCode.Ready:
{
await _audioLogger.DebugAsync("Received Ready").ConfigureAwait(false);
Expand All @@ -225,8 +233,6 @@ private async Task ProcessMessageAsync(VoiceOpCode opCode, object payload)

if (!data.Modes.Contains(DiscordVoiceAPIClient.Mode))
throw new InvalidOperationException($"Discord does not support {DiscordVoiceAPIClient.Mode}");

_heartbeatTask = RunHeartbeatAsync(data.HeartbeatInterval, _connection.CancelToken);

ApiClient.SetUdpEndpoint(data.Ip, data.Port);
await ApiClient.SendDiscoveryAsync(_ssrc).ConfigureAwait(false);
Expand Down

0 comments on commit 9ba38d7

Please sign in to comment.