private void publishbutton_Click(object sender, EventArgs e)
        {
            _timer = new System.Timers.Timer(5000);
            client = new XSocketClient("ws://127.0.0.1:4503/MyController", "*");
            client.Bind("foo2", OnFoo);
            if(client.IsConnected == false)
            {
                _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
                _timer.Enabled = true;
            }

            server = XSockets.Plugin.Framework.Composable.GetExport<IXSocketServerContainer>();

            server.OnServerClientConnection += SocketOnOnOpen;
            server.StartServers();
            XSocketJsonSerializer json = new XSocketJsonSerializer();

            Random r = new Random();
            EquityPrice ep = new EquityPrice();
            ep.open = 14;
            ep.high = 356;
            ep.low = 257;
            ep.close = 23;
            //Console.WriteLine("Started, hit enter to quit");
            //Console.ReadLine();
            while(true)
            {
                ep.code = "0005.HK";
                ep.open = ep.open + Convert.ToDecimal(Math.Round(r.NextDouble(), 2));
                ep.high = ep.high + Convert.ToDecimal(Math.Round(r.NextDouble(), 2));
                ep.low = ep.low + Convert.ToDecimal(Math.Round(r.NextDouble(), 2));
                ep.close = ep.close + Convert.ToDecimal(Math.Round(r.NextDouble(), 2));

                SendEventMessageToAllClients(json.SerializeToString(ep));
                //SendEventMessageToAllClients("hello "+i.ToString());
                //Thread.Sleep(2000);
            }
            server.StopServers();
            //.Send(new { SomeString = "Demo", SomeNumber = 123 }, "foo");
        }
Example #2
0
        private static void SetupClient()
        {
            client = new XSocketClient(server, "*");
            client.OnOpen += (sender, args) =>
            {
                notifier.Notify(NotificationType.Success, hasConnected ? "Reconnected" : "Connected", hasConnected ? "Connection to the server has been reestablished" : "Client has been connected to the server.");
                hasConnected = true;
                SendInit();
            };

            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. Client not up to date.");
                        notifier.Notify(NotificationType.Info, "Outdated version", "Updating to new version...");
                        updateClient();
                        shutDown = true;
                        return;
                    case ClientCommon.Methods.Uninstall.Msg:
                        log.Debug("Uninstalling due to server request...");
                        Uninstall();
                        shutDown = true;
                        return;
                    case ClientCommon.Methods.InstallMod.Msg:
                        SendPing(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:
                        SendPing(ClientCommon.Methods.SetMod.Msg);
                        ThreadPool.QueueUserWorkItem(SetMod, msg.ToObject<SetMod>());
                        break;
                    case ClientCommon.Methods.ConnectDota.Msg:
                        SendPing(ClientCommon.Methods.ConnectDota.Msg);
                        ThreadPool.QueueUserWorkItem(ConnectDota, msg.ToObject<ConnectDota>());
                        break;
                    case ClientCommon.Methods.LaunchDota.Msg:
#if !DEBUG
                        ThreadPool.QueueUserWorkItem(LaunchDota, msg.ToObject<LaunchDota>());
#endif
                        break;
                    case ClientCommon.Methods.ConnectDotaSpectate.Msg:
                        ThreadPool.QueueUserWorkItem(SpectateGame,
                            msg.ToObject<ConnectDotaSpectate>());
                        break;
                    case ClientCommon.Methods.NotifyMessage.Msg:
                        ThreadPool.QueueUserWorkItem(NotifyMessage, msg.ToObject<NotifyMessage>());
                        break;
                    case ClientCommon.Methods.UpdateMods.Msg:
                        ThreadPool.QueueUserWorkItem(UpdateMods, msg.ToObject<UpdateMods>());
                        break;
                    default:
                        log.Error("Command not recognized.");
                        break;
                }
            });

            client.OnError += (sender, args) => log.Error(string.Format("Controller [{0}] sent us error [{1}] on event [{2}].", args.controller, args.data, args.@event));

            client.OnClose += (sender, args) =>
            {
                log.Info("Disconnected from the server.");
                HandleClose();
            };
        }
Example #3
0
        static void Main(string[] args)
        {
            Thread.Sleep(10000);   //To allow the console to be attached to the visual studio debugger.
            // Will throw here.
            string catalog = "";
            string filter = "";
            try
            {
                catalog = RoleEnvironment.GetConfigurationSettingValue("XSockets.PluginCatalog").ToString();
                filter = RoleEnvironment.GetConfigurationSettingValue("XSockets.PluginFilter").ToString();
            }
            catch (Exception)
            {
            }

            try
            {
                XSocketClient ws = new XSocketClient(@"ws://127.0.0.1:4502/Hello", "http://*", true);
                ws.OnOpen += (s, e) =>
                {
                    Console.WriteLine("Connected");
                    // Start sending messages
                    while (true)
                    {
                        try
                        {
                            if (ws.IsConnected)
                            {
                                ws.Send("", "SendHello");
                            }
                        }
                        catch { }
                        Thread.Sleep(5000);

                    }
                };

                ws.OnClose += (s, e) =>
                {
                    Console.WriteLine("Disconnected");
                };

                ws.OnError += (s, e) =>
                {
                    Console.WriteLine("Error: " + e.data.ToString());
                };

                ws.Bind("SendHello", s =>
                {
                    try
                    {
                        Console.WriteLine("Hello received");
                        ws.Send("", "HelloConfirmation");

                    }
                    catch
                    {
                        Console.WriteLine("Could not send ");
                    }
                });

                ws.Bind("HelloConfirmation", s =>
                {
                    try
                    {
                        Console.WriteLine("Hello confirmed");
                    }
                    catch
                    {
                        Console.WriteLine("Could not send ");
                    }
                });

                ws.Open();

                // Keep console alive
                while (true)
                {
                    Thread.Sleep(10000);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(" - Exception connecting to XSockets: " + ex.ToString());
            }
        }
Example #4
0
        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 = modController.clientMods.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. Client not up to date.");
                        notifier.Notify(2, "Outdated version", "Updating to new version...");
                        updateClient();
                        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:
            #if !DEBUG
                        ThreadPool.QueueUserWorkItem(LaunchDota, msg.ToObject<LaunchDota>());
            #endif
                        break;
                    case ClientCommon.Methods.ConnectDotaSpectate.Msg:
                        ThreadPool.QueueUserWorkItem(SpectateGame,
                            msg.ToObject<ConnectDotaSpectate>());
                        break;
                    default:
                        log.Error("Command not recognized.");
                        break;
                }
            });

            client.OnError += (sender, args) => log.Error(string.Format("Controller [{0}] sent us error [{1}] on event [{2}].", args.controller, args.data, args.@event));

            client.OnClose += (sender, args) =>
            {
                log.Info("Disconnected from the server.");
                HandleClose();
            };
        }
Example #5
0
        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();
        }