Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var tcs = new TaskCompletionSource <bool>();
            var activeConnections = new List <HttpListenerContext>();

            int counter      = 0;
            var clientId     = 0;
            var httpListener = new HttpListenerServer
            {
                OnRequest = ctx =>
                {
                    var bytes = System.Text.Encoding.UTF8.GetBytes(string.Format("id: {0}\ndata: cmd.onConnect {{\"id\":\"CLIENT_{1}\"}}\n\n", ++counter, ++clientId));
                    ctx.Response.OutputStream.Write(bytes, 0, bytes.Length);
                    activeConnections.Add(ctx);
                    return(tcs.Task);
                }
            }.Start(Url);

            Timer timer = null;

            timer = new Timer(_ =>
            {
                var bytes = System.Text.Encoding.UTF8.GetBytes(string.Format("id: {0}\ndata: cmd.onMessage MESSAGE {0}\n\n", ++counter));
                foreach (var ctx in activeConnections)
                {
                    ctx.Response.OutputStream.Write(bytes, 0, bytes.Length);
                }
                timer.Change((int)TimeSpan.FromSeconds(1).TotalMilliseconds, Timeout.Infinite);
            },
                              null,
                              (int)TimeSpan.FromSeconds(1).TotalMilliseconds, Timeout.Infinite);

            //CreateBasicHttpClients(NoOfClients);
            CreateServerEventsClient(NoOfClients);

            Console.ReadLine();
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            var tcs = new TaskCompletionSource<bool>();
            var activeConnections = new List<HttpListenerContext>();

            int counter = 0;
            var clientId = 0;
            var httpListener = new HttpListenerServer
            {
                OnRequest = ctx =>
                {
                    var bytes = System.Text.Encoding.UTF8.GetBytes(string.Format("id: {0}\ndata: cmd.onConnect {{\"id\":\"CLIENT_{1}\"}}\n\n", ++counter, ++clientId));
                    ctx.Response.OutputStream.Write(bytes, 0, bytes.Length);
                    activeConnections.Add(ctx);
                    return tcs.Task;
                }
            }.Start(Url);

            Timer timer = null;
            timer = new Timer(_ =>
                {
                    var bytes = System.Text.Encoding.UTF8.GetBytes(string.Format("id: {0}\ndata: cmd.onMessage MESSAGE {0}\n\n", ++counter));
                    foreach (var ctx in activeConnections)
                    {
                        ctx.Response.OutputStream.Write(bytes, 0, bytes.Length);
                    }
                    timer.Change((int) TimeSpan.FromSeconds(1).TotalMilliseconds, Timeout.Infinite);
                },
                null, 
                (int)TimeSpan.FromSeconds(1).TotalMilliseconds, Timeout.Infinite);

            //CreateBasicHttpClients(NoOfClients);
            CreateServerEventsClient(NoOfClients);

            Console.ReadLine();
        }