#pragma warning disable 1998 public async Task Initiate() { VoiceDebugLogger.LogMessageReceived += (sender, e) => { if (DebugMessageReceived != null) DebugMessageReceived(this, e); }; VoiceWebSocket = new WebSocket("wss://" + VoiceEndpoint.Replace(":80", "")); VoiceWebSocket.OnClose += (sender, e) => { if (e.WasClean) return; //for now, till events are hooked up VoiceDebugLogger.Log($"VoiceWebSocket Closed: (Code: {e.Code}) {e.Reason}", MessageLevel.Critical); Dispose(); }; VoiceWebSocket.OnError += (sender, e) => { VoiceDebugLogger.Log($"VoiceWebSocket Error: {e.Message}", MessageLevel.Error); Dispose(); }; VoiceWebSocket.OnMessage += async (sender, e) => { VoiceDebugLogger.Log(e.Data); JObject message = JObject.Parse(e.Data); if (message["op"].Value<int>() == 2) { Params = new VoiceConnectionParameters(); Params.ssrc = message["d"]["ssrc"].Value<int>(); Params.port = message["d"]["port"].Value<int>(); JArray __modes = (JArray)message["d"]["modes"]; List<string> dynModes = new List<string>(); foreach (var mode in __modes) { dynModes.Add(mode.ToString()); } Params.modes = dynModes.ToArray(); Params.heartbeat_interval = message["d"]["heartbeat_interval"].Value<int>(); await InitialConnection().ConfigureAwait(false); } else if (message["op"].Value<int>() == 4) { string speakingJson = JsonConvert.SerializeObject(new { op = 5, d = new { speaking = true, delay = 0 } }); VoiceDebugLogger.Log("Sending initial speaking json..(" + speakingJson + ")"); VoiceWebSocket.Send(speakingJson); Connected = true; keepAliveTask = new Thread(() => { if (Connected) { while (true) { //cancelToken.ThrowIfCancellationRequested(); SendKeepAlive().ConfigureAwait(false); Thread.Sleep(Params.heartbeat_interval); } } }); udpKeepAliveTask = new Thread(() => { while (true) { SendUDPKeepAlive().ConfigureAwait(false); Thread.Sleep(5000); //5 seconds } }); udpReceiveTask = new Thread(async () => { try { while (_udp.Available > 0) { //byte[] packet = new byte[1920]; VoiceDebugLogger.Log("Received packet!!!!!! Length: " + _udp.Available); //UdpReceiveResult d = await _udp.ReceiveAsync(); //packet = d.Buffer; //VoiceDebugLogger.Log("sending speaking.."); //DiscordAudioPacket echo = DiscordAudioPacket.EchoPacket(packet, Params.ssrc); //await _udp.SendAsync(echo.AsRawPacket(), echo.AsRawPacket().Length).ConfigureAwait(false); } } catch (Exception ex) { Console.Beep(32767, 1000); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } }); keepAliveTask.Start(); //udpKeepAliveTask.Start(); udpReceiveTask.Start(); } else if (message["op"].Value<int>() == 5) { if(Connected) //if not connected, don't worry about it { DiscordVoiceUserSpeakingEventArgs __args = new DiscordVoiceUserSpeakingEventArgs { Guild = _parent.GetServersList().Find(x=>x.id == this.Guild.id)}; __args.UserSpeaking = __args.Guild.members.Find(x => x.ID == message["d"]["user_id"].ToString()); __args.Speaking = message["d"]["speaking"].Value<bool>(); if (UserSpeaking != null) UserSpeaking(this, __args); } } }; VoiceWebSocket.OnOpen += (sender, e) => { VoiceDebugLogger.Log("VoiceWebSocket opened, sending initial json."); string initMsg = JsonConvert.SerializeObject(new { op = 0, d = new { user_id = Me.ID, server_id = Guild.id, session_id = SessionID, token = Token } }); VoiceWebSocket.Send(initMsg); }; VoiceWebSocket.Connect(); }
private void OpCode5(JObject message) { DiscordVoiceUserSpeakingEventArgs e = new DiscordVoiceUserSpeakingEventArgs(); e.Channel = Channel; e.UserSpeaking = Guild.members.Find(x => x.ID == message["d"]["user_id"].ToString()); e.Speaking = message["d"]["speaking"].ToObject<bool>(); e.ssrc = message["d"]["ssrc"].ToObject<int>(); if(e.UserSpeaking != null) { if (!SsrcDictionary.ContainsKey(e.UserSpeaking)) SsrcDictionary.Add(e.UserSpeaking, e.ssrc); } LastSpoken = e.UserSpeaking; if (UserSpeaking != null) UserSpeaking(this, e); }