protected virtual void Dispose(bool disposing) { if (!_disposed) { if (disposing) { // Clear all property values that maybe have been set // when the class was instantiated if (LinkClient.Connected) { LinkClient.Stop(); } LinkClient.Dispose(); } // Indicate that the instance has been disposed. _disposed = true; } }
public bool Initialize() { if (!WebsocketConfiguration.IsAvailable()) { WebsocketConfiguration.Setup(); } WebsocketConfiguration.Load(); if (!PingServer()) { return(false); } if (client != null) { client.Dispose(); } client = new WatsonWsClient(WebsocketConfiguration.GetWebsocketUri()); client.MessageReceived += OnReceive; client.Start(); return(true); }
private void ServerDisconnected(object sender, EventArgs e) { if (_client != null) { _client.MessageReceived -= MessageReceived; _client.ServerConnected -= ServerConnected; _client.ServerDisconnected -= ServerDisconnected; } if (_pingTimer != null && _pingTimer.Enabled) { _pingTimer.Stop(); _pingTimer.Dispose(); } _lastServerMessage = DateTime.MinValue; _unlocked = false; _client.Dispose(); OnDisconnect?.Invoke(); }
static void Main(string[] args) { _ServerIp = InputString("Server IP:", "localhost", true); _ServerPort = InputInteger("Server port:", 9000, true, true); _Ssl = InputBoolean("Use SSL:", false); InitializeClient(); bool runForever = true; while (runForever) { Console.Write("Command [? for help]: "); string userInput = Console.ReadLine(); if (String.IsNullOrEmpty(userInput)) { continue; } switch (userInput) { case "?": Console.WriteLine("Available commands:"); Console.WriteLine(" ? help (this menu)"); Console.WriteLine(" q quit"); Console.WriteLine(" cls clear screen"); Console.WriteLine(" send text send text to the server"); Console.WriteLine(" send bytes send binary data to the server"); Console.WriteLine(" stats display client statistics"); Console.WriteLine(" status show if client connected"); Console.WriteLine(" dispose dispose of the connection"); Console.WriteLine(" connect connect to the server if not connected"); Console.WriteLine(" reconnect disconnect if connected, then reconnect"); Console.WriteLine(" close close the connection"); break; case "q": runForever = false; break; case "cls": Console.Clear(); break; case "send text": Console.Write("Data: "); userInput = Console.ReadLine(); if (String.IsNullOrEmpty(userInput)) { break; } if (!_Client.SendAsync(userInput).Result) { Console.WriteLine("Failed"); } else { Console.WriteLine("Success"); } break; case "send bytes": Console.Write("Data: "); userInput = Console.ReadLine(); if (String.IsNullOrEmpty(userInput)) { break; } if (!_Client.SendAsync(Encoding.UTF8.GetBytes(userInput)).Result) { Console.WriteLine("Failed"); } break; case "stats": Console.WriteLine(_Client.Stats.ToString()); break; case "status": if (_Client == null) { Console.WriteLine("Connected: False (null)"); } else { Console.WriteLine("Connected: " + _Client.Connected); } break; case "dispose": _Client.Dispose(); break; case "connect": if (_Client != null && _Client.Connected) { Console.WriteLine("Already connected"); } else { InitializeClient(); } break; case "reconnect": InitializeClient(); break; case "close": _Client.Stop(); break; default: break; } } }
public static async Task <string> SetupNexusLogin(Action <string> updateStatus) { // open a web socket to receive the api key var guid = Guid.NewGuid(); WatsonWsClient client = new WatsonWsClient(new Uri("wss://sso.nexusmods.com")); string api_key = null; object lockobj = new object(); object serverConnectedLockObj = new object(); client.ServerConnected += ServerConnected; client.ServerDisconnected += ServerDisconnected; client.MessageReceived += MessageReceived; client.Start(); void MessageReceived(object sender, MessageReceivedEventArgs args) { Debug.WriteLine("Message from server: " + Encoding.UTF8.GetString(args.Data)); api_key = Encoding.UTF8.GetString(args.Data); lock (lockobj) { Monitor.Pulse(lockobj); } //client. } void ServerConnected(object sender, EventArgs args) { Debug.WriteLine("Server connected"); lock (serverConnectedLockObj) { Monitor.Pulse(serverConnectedLockObj); } } void ServerDisconnected(object sender, EventArgs args) { Debug.WriteLine("Server disconnected"); } //await Task.Delay(1000, cancel); lock (serverConnectedLockObj) { Monitor.Wait(serverConnectedLockObj, new TimeSpan(0, 0, 0, 15)); } if (client.Connected) { await client.SendAsync( Encoding.UTF8.GetBytes("{\"id\": \"" + guid + "\", \"appid\": \"me3tweaks\"}")); //do not localize Thread.Sleep(1000); //?? Utilities.OpenWebpage($"https://www.nexusmods.com/sso?id={guid}&application=me3tweaks"); lock (lockobj) { Monitor.Wait(lockobj, new TimeSpan(0, 0, 1, 0)); } client.Dispose(); } return(api_key); }
static void Main(string[] args) { if (args.Length > 0) { if (args[0] == "-d") { DEBUG = true; } } if (Environment.OSVersion.Platform == PlatformID.Unix) { pico_path = DEFAULT_PICO_PATH_OSX; } while (!File.Exists(pico_path)) { Console.Error.WriteLine("Pico 8 Executable not found. Please specify a path to pico8.exe."); pico_path = Console.ReadLine(); } Console.Error.WriteLine("Press enter to connect, or specify a custom server url."); server_address = Console.ReadLine(); if (server_address == "") { server_address = DEBUG ? DEFAULT_SERVER_ADDRESS_DEBUG : DEFAULT_SERVER_ADDRESS; } Console.Error.WriteLine("Connecting..."); string sock_addr = "wss://" + server_address; if (server_address.Contains("localhost")) { sock_addr = "ws://" + server_address; } connection = new WatsonWsClient(new System.Uri(sock_addr)); connection.ServerConnected += OnOpen; connection.ServerDisconnected += OnClose; connection.MessageReceived += OnMessage; connection.Start(); waitForConnnection.WaitOne(); Process pico = new Process(); //Console.Error.WriteLine("Launching pico8 at: " + pico_path); string cart_path = "classicnet.p8"; if (DEBUG) { cart_path = "../../../../../../" + cart_path; } pico.StartInfo.FileName = pico_path; pico.StartInfo.Arguments = "-run " + AppDomain.CurrentDomain.BaseDirectory + cart_path; pico.StartInfo.UseShellExecute = false; pico.StartInfo.RedirectStandardInput = true; pico.StartInfo.StandardInputEncoding = Encoding.UTF8; pico.StartInfo.RedirectStandardOutput = true; pico.OutputDataReceived += PicoInterface_Output; pico.Start(); pico.BeginOutputReadLine(); picoin = pico.StandardInput; picoin.WriteLine(); pico.WaitForExit(); picoin.Dispose(); pico.Dispose(); if (connection != null) { connection.Dispose(); } }
private static async Task MessageReceived(object sender, MessageReceivedEventArgs args) { try { if (Encoding.UTF8.GetString(args.Data) == "ack") { _ack = Now(); return; } var socketData = JsonConvert.DeserializeObject <SocketData>(Encoding.UTF8.GetString(args.Data)); switch (socketData.Event) { case "Close": _forceClosed = true; _client.Dispose(); CommandWindow.LogError("Connection Disposed Successfully"); break; case "Authorization": var authorizationToken = new SocketData("Authorization", Main.Config.AuthorizationToken); Emit(authorizationToken); break; case "JoinResponse": var response = JsonConvert.DeserializeObject <JoinResponse>(socketData.Data); if (response.Banned) { PlayerManager.BanPlayer(PlayerTool.getSteamPlayer(Provider.server), response.SteamId); } else { UnityThread.executeCoroutine(BrowserRequest(response.SteamId)); } break; case "ServerInfo": var data = new ServerInfo(Provider.serverName, Provider.port, Provider.map, Provider.APP_VERSION, Provider.clients.ToList()); var newSocketData = new SocketData("ServerInfo", JsonConvert.SerializeObject(data)); Emit(newSocketData); break; case "RemoteCommand": var remoteCommand = JsonConvert.DeserializeObject <RemoteCommand>(socketData.Data); UnityThread.executeCoroutine(ExecuteRemoteCommand(remoteCommand)); break; case "ChatMessage": var remoteChat = JsonConvert.DeserializeObject <ChatMessage>(socketData.Data); ChatManager.DiscordToServer(remoteChat); break; case "LinkResponse": var link = JsonConvert.DeserializeObject <Link>(socketData.Data); if (!PlayerTool.tryGetSteamPlayer(link.Steam, out var steamPlayer)) { return; } ChatManager.SendServerMessage( link.Discord != "" ? "You have already linked your account" : $"Use ?link {link.Code} in discord!", steamPlayer, EChatMode.SAY); break; default: CommandWindow.LogError($"Unexpected Event Received: {socketData.Event}"); break; } } catch (Exception ex) { CommandWindow.LogError(ex); } }
static void Main(string[] args) { Console.Write("Server IP : "); serverIp = Console.ReadLine(); Console.Write("Server Port : "); serverPort = Convert.ToInt32(Console.ReadLine()); Console.Write("SSL (true/false) : "); ssl = Convert.ToBoolean(Console.ReadLine()); WatsonWsClient client = new WatsonWsClient(serverIp, serverPort, ssl, true, ServerConnected, ServerDisconnected, MessageReceived, true); bool runForever = true; while (runForever) { Console.Write("Command [? for help]: "); string userInput = Console.ReadLine(); if (String.IsNullOrEmpty(userInput)) { continue; } switch (userInput) { case "?": Console.WriteLine("Available commands:"); Console.WriteLine(" ? help (this menu)"); Console.WriteLine(" q quit"); Console.WriteLine(" cls clear screen"); Console.WriteLine(" send send message to server"); Console.WriteLine(" status show if client connected"); Console.WriteLine(" dispose dispose of the connection"); Console.WriteLine(" connect connect to the server if not connected"); Console.WriteLine(" reconnect disconnect if connected, then reconnect"); break; case "q": runForever = false; break; case "cls": Console.Clear(); break; case "send": Console.Write("Data: "); userInput = Console.ReadLine(); if (String.IsNullOrEmpty(userInput)) { break; } client.SendAsync(Encoding.UTF8.GetBytes(userInput)); break; case "status": if (client == null) { Console.WriteLine("Connected: False (null)"); } else { Console.WriteLine("Connected: " + client.IsConnected()); } break; case "dispose": client.Dispose(); break; case "connect": if (client != null && client.IsConnected()) { Console.WriteLine("Already connected"); } else { client = new WatsonWsClient(serverIp, serverPort, ssl, true, ServerConnected, ServerDisconnected, MessageReceived, true); } break; case "reconnect": if (client != null) { client.Dispose(); } client = new WatsonWsClient(serverIp, serverPort, ssl, true, ServerConnected, ServerDisconnected, MessageReceived, true); break; default: break; } } }