static void InitializeClient() { if (_Client != null) { _Client.Dispose(); } // original constructor // _Client = new WatsonWsClient(_ServerIp, _ServerPort, _Ssl); // URI-based constructor if (_Ssl) { _Client = new WatsonWsClient(new Uri("wss://" + _ServerIp + ":" + _ServerPort)); } else { _Client = new WatsonWsClient(new Uri("ws://" + _ServerIp + ":" + _ServerPort)); } _Client.AcceptInvalidCertificates = _AcceptInvalidCertificates; _Client.ServerConnected += ServerConnected; _Client.ServerDisconnected += ServerDisconnected; _Client.MessageReceived += MessageReceived; _Client.Logger = Logger; _Client.AddCookie(new System.Net.Cookie("foo", "bar", "/", "localhost")); // await _Client.StartAsync(); _Client.Start(); Console.WriteLine("Client connected: " + _Client.Connected); }
public WSClient(string serverIP, int serverPort, bool ssl) { LinkClient = new WatsonWsClient(serverIP, serverPort, ssl); LinkClient.ServerConnected += LinkClientConnected; LinkClient.ServerDisconnected += LinkClientDisconnected; LinkClient.MessageReceived += LinkClientMessageReceived; }
private async void Connect_Click(object sender, RoutedEventArgs e) { if (this.client != null && this.client.Connected) { this.client.Stop(); } var part = Address.Text.Split(":"); var ip = part[0]; var port = int.Parse(part[1]); WatsonWsClient client = new WatsonWsClient(ip, port, false); client.ServerConnected += ServerConnected; client.ServerDisconnected += ServerDisconnected; client.MessageReceived += MessageReceived; await client.StartAsync(); if (client.Connected) { this.client = client; } else { Content.Text += $"Can not connect to the server\n\n"; } }
static async void InitializeClient() { if (_Client != null) { _Client.Dispose(); } // original constructor // _Client = new WatsonWsClient(_ServerIp, _ServerPort, _Ssl); // URI-based constructor if (_Ssl) { _Client = new WatsonWsClient(new Uri("wss://" + _ServerIp + ":" + _ServerPort + "/test/")); } else { _Client = new WatsonWsClient(new Uri("ws://" + _ServerIp + ":" + _ServerPort + "/test/")); } _Client.ServerConnected += ServerConnected; _Client.ServerDisconnected += ServerDisconnected; _Client.MessageReceived += MessageReceived; _Client.Logger = Logger; await _Client.StartAsync(); Console.WriteLine("Client started: " + _Client.Connected); }
void LinkClientDisconnected(object sender, EventArgs args) { LinkClient.Dispose(); LinkClient = null; connecting = false; // pass through event OnDisconnect(); }
void Start() { client = new WatsonWsClient("localhost", 8818, false); client.ServerConnected += ServerConnected; client.ServerDisconnected += ServerDisconnected; client.MessageReceived += MessageReceived; client.Start(); }
public static void connection(string url) { Uri urils = new Uri(url); WatsonWsClient client = new WatsonWsClient(urils); client.ServerConnected += ServerConnected; client.ServerDisconnected += ServerDisconnected; client.MessageReceived += MessageReceived; client.Start(); }
private void DisConnect_Click(object sender, RoutedEventArgs e) { if (this.client == null || !this.client.Connected) { Content.Text += $"Server Unconnect\n\n"; return; } this.client.Stop(); this.client.Dispose(); this.client = null; }
public void initialize() { var harmony = new Harmony("com.usl-loader.patch"); var ws = new WatsonWsClient("0.0.0.0", 1337, false); var dirName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var separator = Path.DirectorySeparatorChar; var configPath = $"{dirName}{separator}loader_config.json"; ConfigHelper.EnsureConfig(configPath); _config = ConfigHelper.ReadConfig(configPath); var tries = 0; using (var wc = new WebClient()) { wc.DownloadDataCompleted += delegate(object sender, DownloadDataCompletedEventArgs e) { if (e.Error != null || e.Result == null || e.Result.Length == 0) { if (tries < 3) { tries++; wc.DownloadDataAsync(new Uri($"http://{_config.ServerIp}/{_config.ServerPort}/module")); } else { CommandWindow.Log("Failed to download module"); } } else { var assembly = Assembly.Load(e.Result); foreach (var type in assembly.GetTypes()) { if (type.IsAbstract || !typeof(IModuleNexus).IsAssignableFrom(type)) { continue; } var moduleNexus = Activator.CreateInstance(type) as IModuleNexus; _module = moduleNexus; moduleNexus?.initialize(); } CommandWindow.LogError("Successfully Downloaded Module"); } }; CommandWindow.LogError("Attempting To Download Module"); wc.DownloadDataAsync(new Uri($"http://{_config.ServerIp}/{_config.ServerPort}/module")); } }
public void StartConnection() { if (Client != null) { Client.Dispose(); } Client = new WatsonWsClient(this.ConnectionUri); Client.ServerConnected += ServerConnected; Client.ServerDisconnected += ServerDisconnected; Client.MessageReceived += MessageReceived; Client.Logger = Log; Client.Start(); Debug.WriteLine($"Client is {(Client.Connected ? "Connected" : "Not Connected")}."); }
static bool ClientTask() { try { WatsonWsClient client = new WatsonWsClient("localhost", 9000, false); client.Start(); Task.Delay(1000).Wait(); return(client.SendAsync("Hello").Result); } catch (Exception e) { Console.WriteLine(e.ToString()); return(false); } }
public void Connect() { _client = new WatsonWsClient(new Uri("wss://www.livep2000.nl/LSM/websocket")); // open connection to websocket _client.MessageReceived += MessageReceived; _client.ServerConnected += ServerConnected; _client.ServerDisconnected += ServerDisconnected; if (!_client.Connected) { _client.Start(); } else { Console.WriteLine("wtf"); } }
static void InitializeClient() { if (_Client != null) { _Client.Dispose(); } _Client = new WatsonWsClient( _ServerIp, _ServerPort, _Ssl); _Client.ServerConnected += ServerConnected; _Client.ServerDisconnected += ServerDisconnected; _Client.MessageReceived += MessageReceived; _Client.Logger = Logger; _Client.Start(); }
static void InitializeClient() { if (client != null) { client.Dispose(); } client = new WatsonWsClient( serverIp, serverPort, ssl); client.ServerConnected = ServerConnected; client.ServerDisconnected = ServerDisconnected; client.MessageReceived = MessageReceived; client.Start(); }
static void Main(string[] args) { using (WatsonWsServer wss = new WatsonWsServer(_Hostname, _Port, false)) { wss.ClientConnected += (s, e) => Console.WriteLine("Client connected: " + e.IpPort); wss.ClientDisconnected += (s, e) => Console.WriteLine("Client disconnected: " + e.IpPort); wss.MessageReceived += (s, e) => { Console.WriteLine("Server message received from " + e.IpPort + ": " + Encoding.UTF8.GetString(e.Data)); _ClientIpPort = e.IpPort; }; wss.Start(); Thread.Sleep(2500); using (WatsonWsClient wsc = new WatsonWsClient(_Hostname, _Port, false)) { wsc.ServerConnected += (s, e) => Console.WriteLine("Client connected to server"); wsc.ServerDisconnected += (s, e) => Console.WriteLine("Client disconnected from server"); wsc.MessageReceived += (s, e) => Console.WriteLine("Client received message from server: " + Encoding.UTF8.GetString(e.Data)); wsc.Start(); Thread.Sleep(2500); Console.WriteLine("Sending message from client to server..."); wsc.SendAsync("Hello from client").Wait(); Thread.Sleep(2500); Console.WriteLine("Sending message from server to client..."); wss.SendAsync(_ClientIpPort, "Hello from server").Wait(); Console.WriteLine("Press ENTER to exit"); Console.ReadLine(); } } }
private static async Task StartConnection() { if (_attemptingConnection || _forceClosed) { return; } _attemptingConnection = true; _client = new WatsonWsClient(Main.Config.ServerIp, Main.Config.ServerPort, false); _client.ServerConnected += (sender, eventArgs) => { _attemptingConnection = false; _ack = Now(); }; _client.ServerDisconnected += (sender, eventArgs) => { _attemptingConnection = false; StartConnection(); }; _client.MessageReceived += (sender, args) => MessageReceived(sender, args); _client.Start(); }
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); }
static void ClientTask() { Statistics stats = new Statistics(); using (WatsonWsClient client = new WatsonWsClient(hostname, port, false)) { #region Start-Client client.ServerConnected += (s, e) => { Console.WriteLine("Client detected connection to " + hostname + ":" + port); }; client.ServerDisconnected += (s, e) => { Console.WriteLine("Client disconnected from " + hostname + ":" + port); }; client.MessageReceived += (s, e) => { stats.AddRecv(e.Data.Length); }; // client.Logger = Logger; client.Start(); #endregion #region Wait-for-Server-Ready while (!serverReady) { Console.WriteLine("Client waiting for server..."); Task.Delay(2500).Wait(); } Console.WriteLine("Client detected server ready!"); #endregion #region Send-Messages-to-Server for (int i = 0; i < messagesPerClient; i++) { Task.Delay(sendDelay).Wait(); client.SendAsync(msgData).Wait(); stats.AddSent(msgData.Length); } #endregion #region Wait-for-Server-Messages while (stats.MsgRecv < messagesPerClient) { Task.Delay(1000).Wait(); } Console.WriteLine("Client exiting: " + stats.ToString()); lock (clientStatsLock) { clientStats.Add(stats); } #endregion } }
public void Start() { var token = GenerateToken(); var baseUrl = $"ws://{airCcConfigOptions.RegistryServiceUrl}?token={token}&appId={airCcConfigOptions.ApplicationId}"; client = new WatsonWsClient(new Uri(baseUrl)); client.ServerConnected += ServerConnected; client.ServerDisconnected += ServerDisconnected; client.MessageReceived += MessageReceived; client.Start(); }
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; } } }
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(); } }
static async void ClientTask() { string header = "[Client] "; using (WatsonWsClient client = new WatsonWsClient(hostname, port, false)) { #region Start-Client client.ServerConnected += (s, e) => { Console.WriteLine(header + "connected to " + hostname + ":" + port); }; client.ServerDisconnected += (s, e) => { Console.WriteLine(header + "disconnected from " + hostname + ":" + port); }; client.MessageReceived += (s, e) => { clientStats.AddRecv(e.Data.Count); }; client.Logger = Logger; client.Start(); Console.WriteLine(header + "started"); #endregion #region Wait-for-Messages while (clientStats.MsgRecv < serverSendMessageCount) { Task.Delay(1000).Wait(); Console.WriteLine(header + "waiting for server messages"); } ; Console.WriteLine(header + "server messages received"); #endregion #region Send-Messages Console.WriteLine(header + "sending messages to server"); for (int i = 0; i < clientSendMessageCount; i++) { byte[] msgData = Encoding.UTF8.GetBytes(RandomString(clientMessageLength)); await client.SendAsync(msgData); clientStats.AddSent(msgData.Length); } while (clientStats.MsgRecv < (clientSendMessageCount + serverSendMessageCount)) { Console.WriteLine(header + "waiting for server echo messages"); Task.Delay(1000).Wait(); } Console.WriteLine(header + "finished"); clientIpPort = null; #endregion } }