Stop() public method

public Stop ( ) : void
return void
示例#1
0
        static void Main(string[] args)
        {
            if (PerformanceCounters.CreatePerformanceCounters())
                return;

            // reseting peformance counter
            PerformanceCounters.Connected.RawValue = 0;

            // configuring logging
            log4net.Config.XmlConfigurator.Configure();
            _log.Info("Starting Echo Server");
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            // opening TLS certificate
            //X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
            //store.Open(OpenFlags.ReadOnly);
            //store.Certificates.Count.ToString();
            //var certificate = store.Certificates[1];
            //store.Close();
            
            CancellationTokenSource cancellation = new CancellationTokenSource();
            
            // local endpoint
            var endpoint = new IPEndPoint(IPAddress.Any, 8005);
            
            // starting the server
            WebSocketListener server = new WebSocketListener(endpoint, new WebSocketListenerOptions() 
            {
                SubProtocols = new []{"text"},
                PingTimeout = TimeSpan.FromSeconds(5),
                NegotiationTimeout = TimeSpan.FromSeconds(5),
                ParallelNegotiations = 16,
                NegotiationQueueCapacity = 256,
                TcpBacklog = 1000,
                BufferManager = BufferManager.CreateBufferManager((8192 + 1024)*1000, 8192 + 1024)
            });
            var rfc6455 = new vtortola.WebSockets.Rfc6455.WebSocketFactoryRfc6455(server);
            // adding the deflate extension
            rfc6455.MessageExtensions.RegisterExtension(new WebSocketDeflateExtension());
            server.Standards.RegisterStandard(rfc6455);
            // adding the WSS extension
            //server.ConnectionExtensions.RegisterExtension(new WebSocketSecureConnectionExtension(certificate));

            server.Start();

            Log("Echo Server started at " + endpoint.ToString());

            var acceptingTask = Task.Run(()=> AcceptWebSocketClients(server, cancellation.Token));

            Console.ReadKey(true);
            Log("Server stoping");
            server.Stop();
            cancellation.Cancel();
            acceptingTask.Wait();

            Console.ReadKey(true);
        }
示例#2
0
        static void Main(string[] args)
        {
            int port = args.Length > 0 ? int.Parse(args[0]) : 80;
            

            // reseting peformance counter
            PerformanceCounters.Connected = 0;
            PerformanceCounters.Accepted = 0;
            PerformanceCounters.Authenticated = 0;

            /* opening TLS certificate
            X509Certificate2 certificate = null;
            X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
            store.Open(OpenFlags.ReadOnly);
            if (store.Certificates.Count > 0)
            {
                certificate = store.Certificates.Cast<X509Certificate2>().FirstOrDefault(cert => cert.Issuer.Contains("CN=stef-org")) ?? store.Certificates[0];
            }
            store.Close();*/

            CancellationTokenSource cancellation = new CancellationTokenSource();

            // local endpoint
            var endpoint = new IPEndPoint(IPAddress.Any, port);

            // starting the server
            const int maxClient = 10000;
            Console.WriteLine("maxClient = " + maxClient);
            var server = new WebSocketListener(endpoint, new WebSocketListenerOptions
            {
                SubProtocols = new[] { "text" },
                PingTimeout = TimeSpan.FromSeconds(500),
                NegotiationTimeout = TimeSpan.FromSeconds(500),
                ParallelNegotiations = 256,
                NegotiationQueueCapacity = 256,
                TcpBacklog = 1000,
                BufferManager = BufferManager.CreateBufferManager((8192 + 1024) * maxClient, 8192 + 1024)
            });

            var rfc6455 = new WebSocketFactoryRfc6455(server);
            // adding the deflate extension
            rfc6455.MessageExtensions.RegisterExtension(new WebSocketDeflateExtension());
            server.Standards.RegisterStandard(rfc6455);

            /* adding the WSS extension (if possible)
            if (certificate != null)
            {
                server.ConnectionExtensions.RegisterExtension(new WebSocketSecureConnectionExtension(certificate));
            }*/

            server.Start();

            Log("Echo Server started at " + endpoint);

            var acceptingTask = Task.Run(() => AcceptWebSocketClients(server, cancellation.Token));

            Console.WriteLine("Press key to stop");

            Console.ReadKey(true);
            Log("Server stopping");
            server.Stop();
            cancellation.Cancel();
            acceptingTask.Wait();

            Console.ReadKey(true);
        }