protected override void InitConnection() { SetStatus(DisplayStatus.Connecting); ServerDiscoverer discoverer = new ServerDiscoverer(); IpAddress address = null; do { address = discoverer.discover(); } while (address == null && Status != DisplayStatus.Closing); if (Status != DisplayStatus.Closing) { websocket = new WebsocketClient(new Uri($"ws://{address}")); websocket.IsReconnectionEnabled = true; websocket.ReconnectionHappened.Subscribe(info => { ExternalEventArgs args = new ExternalEventArgs(); args.Action = ExternalAction.ConnectionChanged; args.Command = DisplayStatus.Connected; EventReceived(args); Console.WriteLine($"Reconnection happened, type: {info.Type}"); }); websocket.ReconnectTimeout = null; websocket.DisconnectionHappened.Subscribe(info => { ExternalEventArgs args = new ExternalEventArgs(); args.Action = ExternalAction.ConnectionChanged; args.Command = DisplayStatus.Connecting; EventReceived(args); Console.WriteLine($"Disconnect happened, type: {info.Type}"); }); websocket.MessageReceived.Subscribe(msg => MessageReceived(msg.Text)); websocket.StartOrFail().Wait(); SetStatus(DisplayStatus.Connected); } }
static void Main(string[] args) { var url = new Uri("ws://localhost:9010"); Directory.CreateDirectory(OUTPUT_FOLDER); var factory = new Func <ClientWebSocket>(() => { var client = new ClientWebSocket(); client.Options.UseDefaultCredentials = false; client.Options.SetRequestHeader("Origin", "file://"); client.Options.SetRequestHeader("Pragma", "no-cache"); client.Options.SetRequestHeader("Cache-Control", "no-cache"); client.Options.SetRequestHeader("Sec-WebSocket-Extensions", "permessage-deflate; client_max_window_bits"); client.Options.SetRequestHeader("Sec-WebSocket-Protocol", "json"); return(client); }); using (var ws = new WebsocketClient(url, factory)) { ws.MessageReceived.Subscribe(msg => MessageParse(ws, msg)); Console.WriteLine($"Trying to connect to LGHUB_agent, at {url}"); try { ws.StartOrFail().Wait(); } catch (Exception) { Console.WriteLine("Failed to connect to LGHUB_agent, is Logitech G HUB running?"); Console.WriteLine("Press any key to quit."); Console.ReadKey(); return; } Console.WriteLine("LGHUB_agent connected."); Console.WriteLine(""); ws.Send(JsonConvert.SerializeObject(new { msgId = "", verb = "GET", path = "/updates/info" })); startTime = DateTime.Now; while ((DateTime.Now - startTime).Milliseconds < 500) { Thread.Sleep(100); } Console.WriteLine(""); Console.WriteLine("Found all .xml files"); Console.WriteLine("Press any key to quit."); Console.ReadKey(); } }
public async Task <IObservable <PwRpc> > EnterWorld(string serverAddr, Account account) { var cmd = api.EnterWorld(serverAddr, account); Console.WriteLine(cmd); await client.StartOrFail(); await client.SendInstant(cmd); return(client.MessageReceived.Select(msg => api.ParseEvent(msg.Text))); }
private async Task InitialiseWS() { var url = new Uri("ws://localhost:9010"); var factory = new Func <ClientWebSocket>(() => { var client = new ClientWebSocket(); client.Options.UseDefaultCredentials = false; client.Options.SetRequestHeader("Origin", "file://"); client.Options.SetRequestHeader("Pragma", "no-cache"); client.Options.SetRequestHeader("Cache-Control", "no-cache"); client.Options.SetRequestHeader("Sec-WebSocket-Extensions", "permessage-deflate; client_max_window_bits"); client.Options.SetRequestHeader("Sec-WebSocket-Protocol", "json"); client.Options.AddSubProtocol("json"); return(client); }); _ws?.Dispose(); _ws = new WebsocketClient(url, factory); _ws.MessageReceived.Subscribe(ParseSocketMsg); _ws.ErrorReconnectTimeout = TimeSpan.FromMilliseconds(500); _ws.ReconnectTimeout = null; Debug.WriteLine($"Trying to connect to LGHUB_agent, at {url}"); try { await _ws.StartOrFail(); } catch (Websocket.Client.Exceptions.WebsocketException) { Debug.WriteLine("Failed to connect to LGHUB_agent"); _ws?.Dispose(); _ws = null; return; } Debug.WriteLine($"Connected to LGHUB_agent"); _ws.Send(JsonConvert.SerializeObject(new { msgId = "", verb = "SUBSCRIBE", path = "/devices/state/changed" })); _ws.Send(JsonConvert.SerializeObject(new { msgId = "", verb = "SUBSCRIBE", path = "/battery/state/changed" })); }
public async Task <bool> Start() { try { await _client.StartOrFail(); return(true); } catch { Log($"Failed to start [{Query}][{League}]"); return(false); } }