/// <summary> /// Runs the websocket connection for the client hooking up the appropriate events. /// </summary> /// <param name="useDotNetWebsocket">If true, DiscordSharp will connect using the .Net Framework's built-in WebSocketClasses. /// Please do not use this on Mono or versions of Windows below 8/8.1</param> public void Connect(bool useDotNetWebsocket = false) { CurrentGatewayURL = GetGatewayUrl(); if (string.IsNullOrEmpty(CurrentGatewayURL)) { DebugLogger.Log("Gateway URL was null or empty?!", MessageLevel.Critical); return; } DebugLogger.Log("Gateway retrieved: " + CurrentGatewayURL); if (useDotNetWebsocket) { ws = new NetWebSocket(CurrentGatewayURL); DebugLogger.Log("Using the built-in .Net websocket.."); } else { ws = new WebSocketSharpSocket(CurrentGatewayURL); DebugLogger.Log("Using WebSocketSharp websocket.."); } ws.MessageReceived += (sender, e) => { var message = new JObject(); try { message = JObject.Parse(e.Message); } catch(Exception ex) { DebugLogger.Log($"MessageReceived Error: {ex.Message}\n\n```{e.Message}\n```\n", MessageLevel.Error); } if (EnableVerboseLogging) if (message["t"].ToString() != "READY") DebugLogger.Log(message.ToString(), MessageLevel.Unecessary); if (!message["t"].IsNullOrEmpty()) //contains a t parameter used for client events. ClientPacketReceived(message); else MiscellaneousOpcodes(message); if (!message["s"].IsNullOrEmpty()) Sequence = message["s"].ToObject<int>(); }; ws.SocketOpened += (sender, e) => { SendIdentifyPacket(); SocketOpened?.Invoke(this, null); }; ws.SocketClosed += (sender, e) => { DiscordSocketClosedEventArgs scev = new DiscordSocketClosedEventArgs(); scev.Code = e.Code; scev.Reason = e.Reason; scev.WasClean = e.WasClean; SocketClosed?.Invoke(this, scev); if (Autoconnect && !e.WasClean) { PerformReconnection(); } }; ws.Connect(); DebugLogger.Log("Connecting.."); }
/// <summary> /// Disposes. /// </summary> public void Dispose() { try { KeepAliveTaskTokenSource.Cancel(); ws.Close(); ws = null; ServersList = null; PrivateChannels = null; Me = null; token = null; this.ClientPrivateInformation = null; } catch { /*already been disposed elsewhere */ } }
/// <summary> /// Runs the websocket connection for the client hooking up the appropriate events. /// </summary> public void Connect() { CurrentGatewayURL = GetGatewayUrl(); if (string.IsNullOrEmpty(CurrentGatewayURL)) { DebugLogger.Log("Gateway URL was null or empty?!", MessageLevel.Critical); return; } DebugLogger.Log("Gateway retrieved: " + CurrentGatewayURL); ws = new WebSocketSharpSocket(CurrentGatewayURL); DebugLogger.Log("Using WebSocketSharp websocket.."); //catch (PlatformNotSupportedException) //Win7 doesn't support this. //{ // ws = new NetWebSocket(CurrentGatewayURL); // DebugLogger.Log("Using .Net's built in WebSocket.."); //} ws.MessageReceived += (sender, e) => { var message = JObject.Parse(e.Message); if (EnableVerboseLogging) if (message["t"].ToString() != "READY") DebugLogger.Log(message.ToString(), MessageLevel.Unecessary); if (!message["t"].IsNullOrEmpty()) //contains a t parameter used for client events. ClientPacketReceived(message); else MiscellaneousOpcodes(message); if (!message["s"].IsNullOrEmpty()) Sequence = message["s"].ToObject<int>(); }; ws.SocketOpened += (sender, e) => { SendIdentifyPacket(); SocketOpened?.Invoke(this, null); }; ws.SocketClosed += (sender, e) => { DiscordSocketClosedEventArgs scev = new DiscordSocketClosedEventArgs(); scev.Code = e.Code; scev.Reason = e.Reason; scev.WasClean = e.WasClean; SocketClosed?.Invoke(this, scev); if (Autoconnect && !e.WasClean) { PerformReconnection(); } }; ws.Connect(); DebugLogger.Log("Connecting.."); }