using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Net.Http.Headers; using System.Net.WebSockets; using System.Security.Policy; using System.Text; using System.Text.Json; using System.Text.Json.Nodes; namespace Discord_Client_Custom.Connections { internal class ConnectionOld { private static string gateWayUrl = "wss://gateway.discord.gg/?v=10&encoding=json"; private static string baseUrl = "discord.com/api/gateway"; public int ReceiveBufferSize { get; set; } = 8192; private static int heartBeatSequence = 0; int heartBeatCounter = 0; private ClientWebSocket WS; private CancellationTokenSource CTS; private async void heartBeat(object o) { if (heartBeatCounter == 3) { Debug.WriteLine("\n\n\njDHsfKJdshfLK SendMessageAsync(RequestType message) { // TODO: handle serializing requests and deserializing responses, handle matching responses to the requests. }*/ private async void ResponseReceived(Stream inputStream) { var responseJson = JsonNode.Parse(inputStream); int opCode = (int)responseJson["op"]; Debug.WriteLine("=======================================================\nREADY\n"); Debug.WriteLine(responseJson); Debug.WriteLine("======================================================="); //Heartbeat stuff if (opCode == 0) { Debug.WriteLine("=======================================================\nREADY\n"); Debug.WriteLine(responseJson); Debug.WriteLine("======================================================="); } else if (opCode == 11) { int hinterval = (int)responseJson["d"]; var toSend = "op: 1\r\nd: " + responseJson["d"]; Debug.WriteLine(responseJson); byte[] data = Encoding.ASCII.GetBytes(toSend); await WS.SendAsync(data, WebSocketMessageType.Text, false, CancellationToken.None); } else if (opCode == 10) { Debug.WriteLine("======================================================="); Debug.WriteLine("CONNECTION ESTABLISHED"); Debug.WriteLine("======================================================="); int heartBeatInterval = (int)responseJson["d"]["heartbeat_interval"]; var autoEvent = new AutoResetEvent(false); var stateTimer = new System.Threading.Timer(heartBeat, autoEvent, 0, heartBeatInterval); autoEvent.WaitOne(); } else { Debug.WriteLine("======================================================="); Debug.WriteLine(responseJson); Debug.WriteLine("======================================================="); } // TODO: handle deserializing responses and matching them to the requests. // IMPORTANT: DON'T FORGET TO DISPOSE THE inputStream! inputStream.DisposeAsync(); } public ConnectionOld() { } public void Dispose() => DisconnectAsync().Wait(); } }