private static void HandleClose() { if (shutDown) { return; } if (hasConnected) { notifier.Notify(NotificationType.Warning, "Lost connection", "Attempting to reconnect..."); hasConnected = false; } while (!shutDown) { SetupClient(); try { client.Open(); break; } catch { Wait(10); } } }
public ServerConnection(ServerManager manager) { this.manager = manager; SetupClient(); try { client.Open(); } catch (Exception ex) { log.Error("Can't connect."); AttemptReconnect(); } }
static void Main(string[] args) { Console.WriteLine("Hit enter to start connecting 4000 clients..."); Console.ReadLine(); var server = 0; for (var i = 0; i < 4000; i++, server++) { if (server > 3) { server = 0; } var c = new XSocketClient("ws://127.0.0.1:" + servers[server], "http://localhost", "foo"); c.Open(); c.Controller("foo").On("m", () => { Interlocked.Increment(ref messagecounter); }); } Task.Factory.StartNew(() => { while (true) { Console.WriteLine(messagecounter); Thread.Sleep(10000); } }); Console.WriteLine("All clients connected. Hit enter to quit..."); Console.ReadLine(); }
private static void HandleClose() { if (hasConnected) { notifier.Notify(3, "Lost connection", "Attempting to reconnect..."); //icon.DisplayBubble("Disconnected, attempting to reconnect..."); hasConnected = false; } SetupClient(); Thread.Sleep(5000); try { client.Open(); } catch (Exception ex) { HandleClose(); } }
public async void Start() { await _client.Open(); Debug.WriteLine("Connection Open"); await _client.Bind("allStocks", OnAllStocks); await _client.Bind("tick", OnTick); Debug.WriteLine("Listening for ticks"); }
static void Main(string[] args) { //System.Threading.Thread.Sleep(3000); var c = new XSocketClient("ws://127.0.0.1:4502", "http://localhost", "generic"); c.OnConnected += (sender, eventArgs) => Console.WriteLine("Connected"); c.Controller("generic").OnOpen += (sender, connectArgs) => Console.WriteLine("Generic Open"); c.Open(); c.Controller("generic").Invoke("CallAllClients"); c.Controller("generic").On("test", () => Console.WriteLine("Syntaxerror did it!!! ")); Console.ReadLine(); }
static void Main(string[] args) { var conn = new XSocketClient("ws://localhost:4502", "http://localhost", "IoT"); conn.Controller("IoT").OnOpen += (sender, connectArgs) => Console.WriteLine("IoT Controller Connected"); conn.Open(); //Set client type conn.Controller("IoT").SetEnum("ClientType", "Native"); //Listen for messages from the Netduino conn.Controller("IoT").On <ChatMessage>("chatmessage", message => Console.WriteLine(message.Text)); Console.WriteLine("Listening for messages from the Netduino, hit enter to quit"); Console.ReadLine(); }
static void Main(string[] args) { //Connect to xsockets windows service (installed from https://chocolatey.org/packages/XSockets.Windows.Service/) var c = new XSocketClient("ws://127.0.0.1:4509/Generic", "http://*"); AsyncContext.Run(async() => { await c.Bind("foo", textArgs => Console.WriteLine("foo {0}", textArgs.data)); c.OnOpen += (sender, eventArgs) => Console.WriteLine("OPEN"); c.OnClose += (sender, eventArgs) => Console.WriteLine("CLOSED"); await c.Open(); Console.ReadLine(); await c.Send("Bar", "foo"); Console.ReadLine(); }); }
static void Main(string[] args) { using (var container = XSockets.Plugin.Framework.Composable.GetExport <IXSocketServerContainer>()) { //Start a server container.Start(); Console.WriteLine("Hit enter to start a client that will show Serilog.Sinks.XSockets"); Console.ReadLine(); //Start a client and hook up only to the log controller... var client = new XSocketClient("ws://localhost:4502", "http://localhost", "log"); client.Open(); client.Controller("log").On <LogEventWrapper>("logEvent", logEvent => { Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("Message: {0}", logEvent.RenderedMessage); Console.ResetColor(); }); //Change the LogEventLevel the client listens to every 8 seconds Task.Factory.StartNew(() => { while (true) { var newLevel = ((LogEventLevel) new Random().Next(0, 5)).ToString(); client.Controller("log").SetEnum("LogEventLevel", newLevel); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("New LogEventLevel set to {0}", newLevel); Console.ResetColor(); Thread.Sleep(8000); } }); Console.WriteLine("Hit enter to quit"); Console.ReadLine(); } }
static void Main(string[] args) { var c = new XSocketClient("ws://127.0.0.1:4502", "http://localhost", "demo"); c.Controller("demo").OnOpen += (sender, connectArgs) => Console.WriteLine("Demo OPEN"); c.Controller("demo").On <MyModel>("addorupdated", model => Console.WriteLine("Updated " + model.Name)); c.Controller("demo").On <MyModel>("removed", model => Console.WriteLine("Removed " + model.Name)); c.Open(); for (var i = 0; i < 10; i++) { var m = new MyModel() { Id = Guid.NewGuid(), Name = "Person Nr" + i, Age = i }; c.Controller("demo").Invoke("AddOrUpdateModel", m); Thread.Sleep(2000); c.Controller("demo").Invoke("RemoveModel", m); Thread.Sleep(2000); } Console.ReadLine(); }
static void Main(string[] args) { //Connect to the XSockets instance hosted in Owin var c = new XSocketClient("ws://localhost:12345", "http://localhost", "stock"); c.OnConnected += (sender, eventArgs) => Console.WriteLine("CONNECTED"); c.Open(); //Set the 'mystocks' property on the controller to 'MSFT' and 'XNET' c.Controller("stock").SetProperty("MyStocks", new List <string>() { "MSFT", "XNET" }); //Listen for 'tick', but we use dynamic since we have no reference to the class Stock c.Controller("stock").On <Stock>("tick", s => { Console.WriteLine("{0}:{1}", s.Symbol, s.Price); }); Console.ReadLine(); }
public void joinRoom(string n, string rn) { nick = n; roomName = rn; c.Open(); }
private static void SetupClient() { client = new XSocketClient(server, "*"); client.OnOpen += (sender, args) => { notifier.Notify(1, hasConnected ? "Reconnected" : "Connected", hasConnected ? "Connection to the server has been reestablished" : "Client has been connected to the server."); //icon.DisplayBubble(hasConnected // ? "Reconnected!" // : "Connected and ready to begin installing mods."); hasConnected = true; log.Debug("Sending init, version: " + ClientCommon.Version.ClientVersion); var init = new Init { SteamIDs = steamids.ToArray(), Version = ClientCommon.Version.ClientVersion, Mods = mods.ToArray() }; var json = JObject.FromObject(init).ToString(Formatting.None); Send(json); }; client.Bind("commands", e => { log.Debug("server: " + e.data); JObject msg = JObject.Parse(e.data); switch (msg["msg"].Value <string>()) { case Shutdown.Msg: log.Debug("Shutting down due to server request."); shutDown = true; return; case ClientCommon.Methods.Uninstall.Msg: log.Debug("Uninstalling due to server request..."); Uninstall(); shutDown = true; return; case ClientCommon.Methods.InstallMod.Msg: ThreadPool.QueueUserWorkItem(InstallMod, msg.ToObject <InstallMod>()); break; case ClientCommon.Methods.DeleteMod.Msg: ThreadPool.QueueUserWorkItem(DeleteMod, msg.ToObject <DeleteMod>()); break; case ClientCommon.Methods.SetMod.Msg: ThreadPool.QueueUserWorkItem(SetMod, msg.ToObject <SetMod>()); break; case ClientCommon.Methods.ConnectDota.Msg: ThreadPool.QueueUserWorkItem(ConnectDota, msg.ToObject <ConnectDota>()); break; case ClientCommon.Methods.LaunchDota.Msg: ThreadPool.QueueUserWorkItem(LaunchDota, msg.ToObject <LaunchDota>()); break; case ClientCommon.Methods.ConnectDotaSpectate.Msg: ThreadPool.QueueUserWorkItem(SpectateGame, msg.ToObject <ConnectDotaSpectate>()); break; default: log.Error("Command not recognized."); break; } }); client.OnClose += (sender, args) => { if (hasConnected) { notifier.Notify(3, "Lost connection", "Attempting to reconnect..."); //icon.DisplayBubble("Disconnected, attempting to reconnect..."); hasConnected = false; } SetupClient(); Thread.Sleep(5000); client.Open(); }; }
public static void main() { log.Debug("D2MP starting..."); var notifyThread = new Thread(delegate() { using (notifier = new notificationForm()) { notifier.Visible = true; Application.Run(); } }); notifyThread.SetApartmentState(ApartmentState.STA); notifyThread.Start(); ourDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); File.WriteAllText(Path.Combine(ourDir, "version.txt"), ClientCommon.Version.ClientVersion); var iconThread = new Thread(delegate() { using (icon = new ProcessIcon()) { icon.Display(); icon.showNotification = delegate { notifier.Invoke(new MethodInvoker(delegate { notifier.Fade(1); notifier.hideTimer.Start(); })); }; Application.Run(); } }); iconThread.SetApartmentState(ApartmentState.STA); iconThread.Start(); try { var steam = new SteamFinder(); if (!Directory.Exists(Settings.steamDir) || !Directory.Exists(Settings.dotaDir)) { Settings.steamDir = steam.FindSteam(true); Settings.dotaDir = steam.FindDota(true); } if (Settings.steamDir == null || Settings.dotaDir == null) { log.Fatal("Steam/dota was not found!"); return; } log.Debug("Steam found: " + Settings.steamDir); log.Debug("Dota found: " + Settings.dotaDir); addonsDir = Path.Combine(Settings.dotaDir, @"dota\addons\"); d2mpDir = Path.Combine(Settings.dotaDir, @"dota\d2moddin\"); modDir = Path.Combine(addonsDir, "d2moddin"); if (!Directory.Exists(addonsDir)) { Directory.CreateDirectory(addonsDir); } if (!Directory.Exists(d2mpDir)) { Directory.CreateDirectory(d2mpDir); } if (!Directory.Exists(modDir)) { Directory.CreateDirectory(modDir); } { string[] dirs = Directory.GetDirectories(d2mpDir); int i = 0; foreach (string modName in dirs.Select(Path.GetFileName)) { log.Debug("Found mod: " + modName + " detecting version..."); string infoPath = Path.Combine(d2mpDir, modName + @"\addoninfo.txt"); string versionFile = ""; if (File.Exists(infoPath)) { versionFile = File.ReadAllText(infoPath); } Match match = Regex.Match(versionFile, @"(addonversion)(\s+)(\d+\.)?(\d+\.)?(\d+\.)?(\*|\d+)", RegexOptions.IgnoreCase); if (match.Success) { string version = match.Groups.Cast <Group>() .ToList() .Skip(3) .Aggregate("", (current, part) => current + part.Value); log.Debug(modName + "=" + version); mods.Add(new ClientMod { name = modName, version = version }); } else { log.Error("Can't find version info for mod: " + modName + ", not including"); } i++; } } //Detect user string config = File.ReadAllText(Path.Combine(Settings.steamDir, @"config\config.vdf")); MatchCollection matches = Regex.Matches(config, "\"\\d{17}\""); string steamid; steamids = new List <string>(); if (matches.Count > 0) { foreach (Match match in matches) { steamid = match.Value.Substring(1).Substring(0, match.Value.Length - 2); log.Debug("Steam ID detected: " + steamid); steamids.Add(steamid); } } else { log.Fatal("Could not detect steam ID."); return; } //Modify gameinfo.txt ModGameInfo(); log.Debug("Starting shutdown file watcher..."); string pathToShutdownFile = Path.Combine(ourDir, "d2mp.pid"); File.WriteAllText(pathToShutdownFile, "Delete this file to shutdown D2MP."); var watcher = new FileSystemWatcher(); watcher.Path = ourDir; watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName; watcher.Filter = "d2mp.pid"; watcher.Deleted += (sender, args) => { shutDown = true; }; watcher.EnableRaisingEvents = true; try { SetupClient(); client.Open(); } catch (Exception ex) { notifier.Notify(4, "Server error", "Can't connect to the lobby server!"); //icon.DisplayBubble("Can't connect to the lobby server!"); } while (!shutDown) { Thread.Sleep(100); } client.Close(); } catch (Exception ex) { log.Fatal("Overall error in the program: " + ex); } //UnmodGameInfo(); shutDown = true; Application.Exit(); }
public static void main() { log.Debug("D2MP starting..."); ourDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); File.WriteAllText(Path.Combine(ourDir, "version.txt"), ClientCommon.Version.ClientVersion); var iconThread = new Thread(delegate() { using (icon = new ProcessIcon()) { icon.Display(); Application.Run(); } }); iconThread.SetApartmentState(ApartmentState.STA); iconThread.Start(); try { var steam = new SteamFinder(); string steamDir = steam.FindSteam(true); dotaDir = steam.FindDota(true); if (steamDir == null || dotaDir == null) { log.Fatal("Steam/dota was not found!"); return; } log.Debug("Steam found: " + steamDir); log.Debug("Dota found: " + dotaDir); addonsDir = Path.Combine(dotaDir, "dota/addons/"); d2mpDir = Path.Combine(dotaDir, "dota/d2moddin/"); modDir = Path.Combine(addonsDir, "d2moddin"); if (!Directory.Exists(addonsDir)) { Directory.CreateDirectory(addonsDir); } if (!Directory.Exists(d2mpDir)) { Directory.CreateDirectory(d2mpDir); } if (!Directory.Exists(modDir)) { Directory.CreateDirectory(modDir); } { string[] dirs = Directory.GetDirectories(d2mpDir); int i = 0; foreach (string dir in dirs) { string modName = Path.GetFileName(dir); log.Debug("Found mod: " + modName + " detecting version..."); string infoPath = Path.Combine(d2mpDir, modName + "/addoninfo.txt"); string versionFile = ""; if (File.Exists(infoPath)) { versionFile = File.ReadAllText(infoPath); } Match match = Regex.Match(versionFile, @"(addonversion)(\s+)(\d+\.)?(\d+\.)?(\d+\.)?(\*|\d+)", RegexOptions.IgnoreCase); if (match.Success) { string version = match.Groups.Cast <Group>() .ToList() .Skip(3) .Aggregate("", (current, part) => current + part.Value); log.Debug(modName + "=" + version); mods.Add(new ClientMod { name = modName, version = version }); } else { log.Error("Can't find version info for mod: " + modName + ", not including"); } i++; } } //Detect user string config = File.ReadAllText(Path.Combine(steamDir, @"config\config.vdf")); MatchCollection matches = Regex.Matches(config, "\"\\d{17}\""); string steamid; var steamids = new List <string>(); if (matches.Count > 0) { foreach (Match match in matches) { steamid = match.Value.Substring(1).Substring(0, match.Value.Length - 2); log.Debug("Steam ID detected: " + steamid); steamids.Add(steamid); } } else { log.Fatal("Could not detect steam ID."); return; } //Modify gameinfo.txt ModGameInfo(); log.Debug("Starting shutdown file watcher..."); string pathToShutdownFile = Path.Combine(ourDir, "d2mp.pid"); File.WriteAllText(pathToShutdownFile, "Delete this file to shutdown D2MP."); var watcher = new FileSystemWatcher(); watcher.Path = ourDir; watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName; watcher.Filter = "d2mp.pid"; watcher.Deleted += (sender, args) => { shutDown = true; }; watcher.EnableRaisingEvents = true; client = new XSocketClient(server, "*"); bool hasConnected = false; client.OnOpen += (sender, args) => { icon.DisplayBubble(hasConnected ? "Reconnected!" : "Connected and ready to begin installing mods."); hasConnected = true; log.Debug("Sending init, version: " + ClientCommon.Version.ClientVersion); var init = new Init { SteamIDs = steamids.ToArray(), Version = ClientCommon.Version.ClientVersion, Mods = mods.ToArray() }; var json = JObject.FromObject(init).ToString(Formatting.None); Send(json); }; client.Bind("commands", e => { log.Debug("server: " + e.data); JObject msg = JObject.Parse(e.data); switch (msg["msg"].Value <string>()) { case Shutdown.Msg: log.Debug("Shutting down due to server request."); shutDown = true; return; case ClientCommon.Methods.Uninstall.Msg: log.Debug("Uninstalling due to server request..."); Uninstall(); shutDown = true; return; case ClientCommon.Methods.InstallMod.Msg: ThreadPool.QueueUserWorkItem(InstallMod, msg.ToObject <InstallMod>()); break; case ClientCommon.Methods.DeleteMod.Msg: ThreadPool.QueueUserWorkItem(DeleteMod, msg.ToObject <DeleteMod>()); break; case ClientCommon.Methods.SetMod.Msg: ThreadPool.QueueUserWorkItem(SetMod, msg.ToObject <SetMod>()); break; case ClientCommon.Methods.ConnectDota.Msg: ThreadPool.QueueUserWorkItem(ConnectDota, msg.ToObject <ConnectDota>()); break; case ClientCommon.Methods.LaunchDota.Msg: ThreadPool.QueueUserWorkItem(LaunchDota, msg.ToObject <LaunchDota>()); break; case ClientCommon.Methods.ConnectDotaSpectate.Msg: ThreadPool.QueueUserWorkItem(SpectateGame, msg.ToObject <ConnectDotaSpectate>()); break; default: log.Error("Command not recognized."); break; } }); client.OnClose += (sender, args) => { if (hasConnected) { icon.DisplayBubble("Disconnected, attempting to reconnect..."); hasConnected = false; } try { client.Open(); } catch (Exception ex) { icon.DisplayBubble("Can't connect to the lobby server!"); } }; try { client.Open(); } catch (Exception ex) { icon.DisplayBubble("Can't connect to the lobby server!"); } while (!shutDown) { Thread.Sleep(100); } client.Close(); } catch (Exception ex) { log.Fatal("Overall error in the program: " + ex); } //UnmodGameInfo(); shutDown = true; Application.Exit(); }