@@ -6,24 +6,25 @@ namespace Discord | |||||
{ | { | ||||
public partial class DiscordClient | public partial class DiscordClient | ||||
{ | { | ||||
public Task JoinVoiceServer(string channelId) | |||||
=> JoinVoiceServer(_channels[channelId]); | |||||
public async Task JoinVoiceServer(Channel channel) | |||||
public Task JoinVoiceServer(Channel channel) | |||||
=> JoinVoiceServer(channel.ServerId, channel.Id); | |||||
public async Task JoinVoiceServer(string serverId, string channelId) | |||||
{ | { | ||||
CheckReady(checkVoice: true); | CheckReady(checkVoice: true); | ||||
if (channel == null) throw new ArgumentNullException(nameof(channel)); | |||||
if (serverId == null) throw new ArgumentNullException(nameof(serverId)); | |||||
if (channelId == null) throw new ArgumentNullException(nameof(channelId)); | |||||
await LeaveVoiceServer().ConfigureAwait(false); | await LeaveVoiceServer().ConfigureAwait(false); | ||||
_dataSocket.SendJoinVoice(channel); | |||||
_voiceSocket.SetServer(serverId); | |||||
_dataSocket.SendJoinVoice(serverId, channelId); | |||||
//await _voiceSocket.WaitForConnection().ConfigureAwait(false); | //await _voiceSocket.WaitForConnection().ConfigureAwait(false); | ||||
//TODO: Add another ManualResetSlim to wait on here, base it off of DiscordClient's setup | //TODO: Add another ManualResetSlim to wait on here, base it off of DiscordClient's setup | ||||
} | } | ||||
public async Task LeaveVoiceServer() | public async Task LeaveVoiceServer() | ||||
{ | { | ||||
CheckReady(checkVoice: true); | CheckReady(checkVoice: true); | ||||
if (_voiceSocket.CurrentVoiceServerId != null) | |||||
if (_voiceSocket.State != Net.WebSockets.WebSocketState.Disconnected) | |||||
{ | { | ||||
await _voiceSocket.Disconnect().ConfigureAwait(false); | await _voiceSocket.Disconnect().ConfigureAwait(false); | ||||
await TaskHelper.CompletedTask.ConfigureAwait(false); | await TaskHelper.CompletedTask.ConfigureAwait(false); | ||||
@@ -43,7 +44,6 @@ namespace Discord | |||||
_voiceSocket.SendPCMFrames(data, count); | _voiceSocket.SendPCMFrames(data, count); | ||||
} | } | ||||
/// <summary> Clears the PCM buffer. </summary> | /// <summary> Clears the PCM buffer. </summary> | ||||
public void ClearVoicePCM() | public void ClearVoicePCM() | ||||
{ | { | ||||
@@ -121,7 +121,7 @@ namespace Discord | |||||
}; | }; | ||||
_voiceSocket.IsSpeaking += (s, e) => | _voiceSocket.IsSpeaking += (s, e) => | ||||
{ | { | ||||
if (_voiceSocket.CurrentVoiceServerId != null) | |||||
if (_voiceSocket.State == WebSocketState.Connected) | |||||
{ | { | ||||
var member = _members[e.UserId, _voiceSocket.CurrentVoiceServerId]; | var member = _members[e.UserId, _voiceSocket.CurrentVoiceServerId]; | ||||
bool value = e.IsSpeaking; | bool value = e.IsSpeaking; | ||||
@@ -586,11 +586,14 @@ namespace Discord | |||||
case "VOICE_SERVER_UPDATE": | case "VOICE_SERVER_UPDATE": | ||||
{ | { | ||||
var data = e.Payload.ToObject<Events.VoiceServerUpdate>(_serializer); | var data = e.Payload.ToObject<Events.VoiceServerUpdate>(_serializer); | ||||
var server = _servers[data.GuildId]; | |||||
if (_config.EnableVoice) | |||||
if (data.GuildId == _voiceSocket.CurrentVoiceServerId) | |||||
{ | { | ||||
_voiceSocket.Host = "wss://" + data.Endpoint.Split(':')[0]; | |||||
await _voiceSocket.Login(data.GuildId, _currentUserId, _dataSocket.SessionId, data.Token, _cancelToken).ConfigureAwait(false); | |||||
var server = _servers[data.GuildId]; | |||||
if (_config.EnableVoice) | |||||
{ | |||||
_voiceSocket.Host = "wss://" + data.Endpoint.Split(':')[0]; | |||||
await _voiceSocket.Login(_currentUserId, _dataSocket.SessionId, data.Token, _cancelToken).ConfigureAwait(false); | |||||
} | |||||
} | } | ||||
} | } | ||||
break; | break; | ||||
@@ -29,7 +29,7 @@ namespace Discord | |||||
private int _messageQueueInterval = 100; | private int _messageQueueInterval = 100; | ||||
/// <summary> Gets or sets the max buffer length (in milliseconds) for outgoing voice packets. This value is the target maximum but is not guaranteed, the buffer will often go slightly above this value. </summary> | /// <summary> Gets or sets the max buffer length (in milliseconds) for outgoing voice packets. This value is the target maximum but is not guaranteed, the buffer will often go slightly above this value. </summary> | ||||
public int VoiceBufferLength { get { return _voiceBufferLength; } set { SetValue(ref _voiceBufferLength, value); } } | public int VoiceBufferLength { get { return _voiceBufferLength; } set { SetValue(ref _voiceBufferLength, value); } } | ||||
private int _voiceBufferLength = 3000; | |||||
private int _voiceBufferLength = 1000; | |||||
//Experimental Features | //Experimental Features | ||||
#if !DNXCORE50 | #if !DNXCORE50 | ||||
@@ -112,11 +112,11 @@ namespace Discord.Net.WebSockets | |||||
return new Commands.KeepAlive(); | return new Commands.KeepAlive(); | ||||
} | } | ||||
public void SendJoinVoice(Channel channel) | |||||
public void SendJoinVoice(string serverId, string channelId) | |||||
{ | { | ||||
var joinVoice = new Commands.JoinVoice(); | var joinVoice = new Commands.JoinVoice(); | ||||
joinVoice.Payload.ServerId = channel.ServerId; | |||||
joinVoice.Payload.ChannelId = channel.Id; | |||||
joinVoice.Payload.ServerId = serverId; | |||||
joinVoice.Payload.ChannelId = channelId; | |||||
QueueMessage(joinVoice); | QueueMessage(joinVoice); | ||||
} | } | ||||
public void SendLeaveVoice() | public void SendLeaveVoice() | ||||
@@ -53,16 +53,19 @@ namespace Discord.Net.WebSockets | |||||
_targetAudioBufferLength = client.Config.VoiceBufferLength / 20; //20 ms frames | _targetAudioBufferLength = client.Config.VoiceBufferLength / 20; //20 ms frames | ||||
} | } | ||||
public async Task Login(string serverId, string userId, string sessionId, string token, CancellationToken cancelToken) | |||||
public void SetServer(string serverId) | |||||
{ | { | ||||
if (_serverId == serverId && _userId == userId && _sessionId == sessionId && _token == token) | |||||
_serverId = serverId; | |||||
} | |||||
public async Task Login(string userId, string sessionId, string token, CancellationToken cancelToken) | |||||
{ | |||||
if ((WebSocketState)_state != WebSocketState.Disconnected) | |||||
{ | { | ||||
//Adjust the host and tell the system to reconnect | //Adjust the host and tell the system to reconnect | ||||
await DisconnectInternal(new Exception("Server transfer occurred."), isUnexpected: false); | await DisconnectInternal(new Exception("Server transfer occurred."), isUnexpected: false); | ||||
return; | return; | ||||
} | } | ||||
_serverId = serverId; | |||||
_userId = userId; | _userId = userId; | ||||
_sessionId = sessionId; | _sessionId = sessionId; | ||||
_token = token; | _token = token; | ||||
@@ -135,7 +138,6 @@ namespace Discord.Net.WebSockets | |||||
ClearPCMFrames(); | ClearPCMFrames(); | ||||
if (!_wasDisconnectUnexpected) | if (!_wasDisconnectUnexpected) | ||||
{ | { | ||||
_serverId = null; | |||||
_userId = null; | _userId = null; | ||||
_sessionId = null; | _sessionId = null; | ||||
_token = null; | _token = null; | ||||