Пример #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();
        }
Пример #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();
        }
Пример #3
0
        static void Main(string[] args)
        {
            //Create chat server instance
            var chatServer = new ChatServer();

            //Remote Hub Switch
            remoteHubSwitch = new RemoteHubSwitch();
            remoteHubSwitch.ConnectionErrorOccurred += RemoteHubSwitch_ConnectionErrorOccurred;
            remoteHubSwitch.AdapterRemoved          += RemoteHubSwitch_AdapterRemoved;

            //Tcp server
            ClientEntity.InitializeServerCertificate(serverIP, "TestServer");
            tcpListener = new TcpListener(serverIP, serverPort);
            tcpListener.Start();
            var accepting = Task.Run(() => TcpServerAcceptLinks(shutdownSignal.Token));

            //Local client on RemoteHub
            var siteId = ServerId.SiteId;

            localClient = new RemoteHubSwitchDirect <string>(siteId, OnMessageReceivedFromRemoteHub);
            localClient.RemoteClientRemoved += RemoteHubClient_RemoteClientRemoved;
            remoteHubSwitch.AddAdapter(localClient);

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

            //localClient.Start(); //started already. when SwitchDirect is added to switch, it will be started automatically.
            remoteAgency.Connect();

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

            shutdownSignal.Cancel();
            tcpListener.Stop();
            remoteAgency.Disconnect(false);
            remoteAgency.Dispose();
            localClient.Stop();
            localClient.Dispose();
            remoteHubSwitch.RemoveAllAdapters();
            foreach (var client in clients)
            {
                client.Dispose();
            }
            clients.Clear();
        }
Пример #4
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;
            }

            //Stream
            TcpClient tcpClient = new TcpClient();

            tcpClient.Connect(serverIP, serverPort);

            var clientCertificate           = BuildSelfSignedClientCertificate("localClient");
            var clientCertificateCollection = new X509CertificateCollection(new X509Certificate[] { clientCertificate });

            SslStream stream1 = new SslStream(tcpClient.GetStream(), false,
                                              (sender, certificate, chain, sslPolicyErrors) => true /*always return true in certificate test in this demo*/);

            stream1.AuthenticateAsClient(serverIP.ToString(), clientCertificateCollection, false);

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

            remoteHub = new RemoteHubOverStream <string>(siteId, stream1, stream1, 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();
            stream1.Close();
            stream1.Dispose();
            tcpClient.Close();
            tcpClient.Dispose();
        }