Exemplo n.º 1
0
        static void Main(string[] args)
        {
            //Create chat server instance
            var chatServer = new ChatServer();

            //Remote Hub
            var siteId = ServerId.SiteId;

            remoteHub = new RemoteHubOverRedis <string>(siteId, connectionString, OnMessageReceivedFromRemoteHub);
            remoteHub.RemoteClientRemoved += RemoteHub_RemoteClientRemoved;

            //Remote Agency
            remoteAgency = new RemoteAgencyManagerEncapsulated(false, true, siteId);
            remoteAgency.MessageForSendingPrepared += OnMessageForSendingPrepared;
            remoteAgency.AddServiceWrapper <IChatServer>(chatServer, ServerId.ServiceId);

            remoteHub.Start();
            remoteAgency.Connect();

            Console.WriteLine("Chat server is started. Press any key to quit.");
            Console.ReadKey(true);

            remoteAgency.Disconnect(false);
            remoteHub.Stop();
            remoteAgency.Dispose();
            remoteHub.Dispose();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            //Init
            Console.Write("Enter your name and press enter <Empty = Exit>: ");
            var name = Console.ReadLine();

            if (string.IsNullOrWhiteSpace(name))
            {
                return;
            }

            //Remote Hub
            var siteId = Guid.NewGuid();

            remoteHub = new RemoteHubOverRedis <string>(siteId, connectionString, OnMessageReceivedFromHub);

            //Remote Agency
            remoteAgency = new RemoteAgencyManagerEncapsulated(true, false, siteId);
            remoteAgency.MessageForSendingPrepared += OnMessageForSendingPrepared;
            remoteAgency.DefaultTargetSiteId        = ServerId.SiteId;
            var chatServer = remoteAgency.AddProxy <IChatServer>(ServerId.ServiceId, out var instanceId);

            remoteHub.Start();
            remoteAgency.Connect();

            chatServer.MessageReceived += ChatServer_MessageReceived;

            Console.WriteLine("Started. Please chat and press enter. Empty = Exit.");

            while (true)
            {
                var text = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(text))
                {
                    break;
                }
                chatServer.SendMessage(name, text);
            }


            ((IDisposable)chatServer).Dispose();

            remoteAgency.Disconnect(false);
            remoteHub.Stop();
            remoteAgency.Dispose();
            remoteHub.Dispose();
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            Guid client1Id = Guid.NewGuid();
            Guid client2Id = Guid.NewGuid();
            Guid client3Id = Guid.NewGuid();
            RemoteHubOverRedis <string> client1 = new RemoteHubOverRedis <string>(client1Id, connectionString, Received);
            RemoteHubOverRedis <string> client2 = new RemoteHubOverRedis <string>(client2Id, connectionString, Received);
            RemoteHubOverRedis <string> client3 = new RemoteHubOverRedis <string>(client3Id, connectionString, Received);

            //client1.RemoteClientUpdated += RemoteClientUpdated;
            //client2.RemoteClientUpdated += RemoteClientUpdated;
            //client3.RemoteClientUpdated += RemoteClientUpdated;

            //client1.RemoteClientRemoved += RemoteClientRemoved;
            //client2.RemoteClientRemoved += RemoteClientRemoved;
            //client3.RemoteClientRemoved += RemoteClientRemoved;


            //Console.WriteLine(string.Format("ClientId: {0} {1} {2}", client1Id, client2Id, client3Id));
            clientNames.Add(client1Id, "Client1");
            clientNames.Add(client2Id, "Client2");
            clientNames.Add(client3Id, "Client3");

            client1.Start();
            client2.Start();
            client3.Start();

            Guid virtualHostId = Guid.NewGuid();

            client1.ApplyVirtualHosts(new KeyValuePair <Guid, VirtualHostSetting>(virtualHostId, new VirtualHostSetting(0, 5)));
            client2.ApplyVirtualHosts(new KeyValuePair <Guid, VirtualHostSetting>(virtualHostId, new VirtualHostSetting(0, 3)));
            client3.ApplyVirtualHosts(new KeyValuePair <Guid, VirtualHostSetting>(virtualHostId, new VirtualHostSetting(0, 2)));


            bool shouldContinue = true;

            while (shouldContinue)
            {
                Console.WriteLine(@"Press:
1: Send from client 1 to client 1
2: Send from client 1 to client 2
3: Send from client 1 to client 3
4: Send from client 2 to client 1
5: Send from client 2 to client 2
6: Send from client 2 to client 3
7: Send from client 3 to client 1
8: Send from client 3 to client 2
9: Send from client 3 to client 3
0: Send from client 1 to virtual host (1:50%/2:30%/3:20%)
Other: Shutdown.");

                var key = Console.ReadKey(true);
                switch (key.Key)
                {
                case ConsoleKey.D1:
                    client1.SendMessage(client1Id, "From 1 to 1");
                    break;

                case ConsoleKey.D2:
                    client1.SendMessage(client2Id, "From 1 to 2");
                    break;

                case ConsoleKey.D3:
                    client1.SendMessage(client3Id, "From 1 to 3");
                    break;

                case ConsoleKey.D4:
                    client2.SendMessage(client1Id, "From 2 to 1");
                    break;

                case ConsoleKey.D5:
                    client2.SendMessage(client2Id, "From 2 to 2");
                    break;

                case ConsoleKey.D6:
                    client2.SendMessage(client3Id, "From 2 to 3");
                    break;

                case ConsoleKey.D7:
                    client3.SendMessage(client1Id, "From 3 to 1");
                    break;

                case ConsoleKey.D8:
                    client3.SendMessage(client2Id, "From 3 to 2");
                    break;

                case ConsoleKey.D9:
                    client3.SendMessage(client3Id, "From 3 to 3");
                    break;

                case ConsoleKey.D0:
                    if (client1.TryResolveVirtualHost(virtualHostId, out var hostId))
                    {
                        client1.SendMessage(hostId, "From 1 to virtual host (1:50%/2:30%/3:20%)");
                    }
                    break;

                default:
                    shouldContinue = false;
                    break;
                }
            }

            client1.Stop();
            client2.Stop();
            client3.Stop();

            Console.WriteLine("Done. Press any key to quit...");
            Console.ReadKey(true);
        }