Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

EventSource Web API C#

using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Web.Mvc;
using Newtonsoft.Json;

namespace EventSourceTest2.Controllers {
    public class PingData {
        public int UserID { get; set; }
        public DateTime Date { get; set; } = DateTime.Now;
    }

    public class HomeController : Controller {
        public ActionResult Index() {
            return View();
        }

        static ConcurrentQueue<PingData> pings = new ConcurrentQueue<PingData>();

        public void Ping(int userID) {
            pings.Enqueue(new PingData { UserID = userID });
        }

        public void Message() {
            Response.ContentType = "text/event-stream";
            do {
                PingData nextPing;
                if (pings.TryDequeue(out nextPing)) {
                    Response.Write("data:" + JsonConvert.SerializeObject(nextPing, Formatting.None) + "

");
                }
                Response.Flush();
                Thread.Sleep(1000);
            } while (true);
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: verifyusertokenasync password reset token 
Csharp :: C# top down view player movement script 
Csharp :: top down view movement script 
Csharp :: unity basic public options 
Csharp :: how to get relative path in c# 
Csharp :: c# invokerequired wpf 
Csharp :: c# selenium xunit testing 
Csharp :: pick random point inside box collider unity 
Csharp :: unity how to find the largest value out of 2 numbers 
Csharp :: Advertisement code for unity 
Csharp :: ado net execute sql query 
Csharp :: c# enum get string value 
Csharp :: bind repeater to dictionary 
Csharp :: c# datagridview multiple row selection without control 
Csharp :: c# convert string to array 
Csharp :: unity vector3 initialization 
Csharp :: cread 2-dimensional array in c# 
Csharp :: c# winforms datagridview bind to list 
Csharp :: How to get selected item from Dropdown in GridView 
Csharp :: Severity Code Description Project File Line Suppression State Error MSB3021 
Csharp :: trygetvalue c# 
Csharp :: .net return result encoding as utf8 
Csharp :: ef core add OnModelCreating foreign key 
Csharp :: c# console.writeline 
Csharp :: reference to gameobject in different scene unity 
Csharp :: Triangle perimeter 
Csharp :: register all services microsoft .net core dependency injection container 
Csharp :: c# quick "is" "as" 
Csharp :: unity fast sin 
Csharp :: c# check multiple variables for null 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =