Horje
EventSource Web API C# Code Example
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) + "\n\n");
                }
                Response.Flush();
                Thread.Sleep(1000);
            } while (true);
        }
    }
}




Csharp

Related
unity screentoworldpoint Code Example unity screentoworldpoint Code Example
ASP.net ApplicationUser referance not found Code Example ASP.net ApplicationUser referance not found Code Example
.net core change localhost port Code Example .net core change localhost port Code Example
xamarin set environment variables Code Example xamarin set environment variables Code Example
c# code skripte kommunizieren Code Example c# code skripte kommunizieren Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
11