C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C# SSE服务端和客户端

C#实现SSE(Server-Sent Events)服务端和客户端的示例代码

作者:j10n

这篇文章主要为大家详细介绍了C#实现SSE(Server-Sent Events)服务端和客户端的相关知识,文中的示例代码讲解详细,有需要的小伙伴可以参考下

效果图

服务端不停发送当前时间。

服务端代码: 

using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Threading;
 
namespace SSEserver
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpListener listener = new HttpListener();
            listener.Prefixes.Add("http://localhost:8080/");
            listener.Start();
 
            Console.WriteLine("Server is running...");
 
            while (true)
            {
                HttpListenerContext context = listener.GetContext();
                ThreadPool.QueueUserWorkItem((_) =>
                {
                    SendEvent(context);
                });
            }
        }
 
        static void SendEvent(HttpListenerContext context)
        {
            try
            {
                context.Response.ContentType = "text/event-stream";
                context.Response.StatusCode = 200;
                context.Response.Headers.Add("Cache-Control", "no-cache");
                context.Response.Headers.Add("Connection", "keep-alive");
 
                while (true)
                {
                    string message = $"data: {DateTime.Now.ToString("HH:mm:ss")}\n\n";
                    byte[] bytes = Encoding.UTF8.GetBytes(message);
                    context.Response.OutputStream.Write(bytes, 0, bytes.Length);
                    context.Response.OutputStream.Flush();
                    Thread.Sleep(1000); // 每秒发送一次消息
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
            finally
            {
                context.Response.Close();
            }
        }
    }
}

客户端不停接收服务端发送的时间。

客户端代码: 

using System;
using System.IO;
using System.Net;
using System.Text;
namespace SSEclient
{
    class Program
    {
        static async Task Main(string[] args)
        {
            string url = "http://localhost:8080/sse_endpoint"; // SSE endpoint URL
 
            using (HttpClient client = new HttpClient())
            {
                var request = new HttpRequestMessage(HttpMethod.Get, url);
                request.Headers.Add("Accept", "text/event-stream");
 
                using (HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead))
                {
                    response.EnsureSuccessStatusCode();
 
                    using (var responseStream = await response.Content.ReadAsStreamAsync())
                    using (var reader = new System.IO.StreamReader(responseStream))
                    {
                        while (!reader.EndOfStream)
                        {
                            string line = await reader.ReadLineAsync();
 
                            if (!string.IsNullOrWhiteSpace(line))
                            {
                                Console.WriteLine(line);
                                // Handle the received SSE event here
                            }
                        }
                    }
                }
            }
        }
    }
}

方法补充

除了上文的方法,小编还为大家整理了其他C#实现SSE(Server-Sent Events)的方法,希望对大家有所帮助

C# MVC 实现Server-Sent Events(ApiController案例)

(浏览器端) JS代码:

// 建立ServerSentEvents(服务器向浏览器推送信息,简称SSE)
    $(function () {
        if (typeof (EventSource) !== "undefined") {
            // 展示服务器推送内容的DOM
            var container = document.getElementById("SseContainer");
            // 建立SSE通道
            var es = new EventSource("/api/ServerSentEvents/BuildingSse");
            // 监听SSE通道open事件
            es.onopen = function (event) {
                container.innerHTML += "open<br/>";
            }
            // 监听SSE接收到的服务端消息
            es.onmessage = function (event) {
                container.innerHTML += event.data + "<br/>";
            };
            // 监听SSE通道error事件
            es.onerror = function (event) {
                container.innerHTML += "error<br/>";
                es.close();
            }
        }
        else {
            console.log("浏览器不支持ServerSentEvents接口!");
        }
    })

(服务器端)C# MVC Controller代码:

using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Http;

namespace CabinetMS.Controllers.WebApi
{

    /// <summary>
    /// 自定义的SSE消息对象实体
    /// </summary>
    public class SseMessageObject
    {
        public string MsgId { get; set; }
        public string MsgData { get; set; }
    }



    /// <summary>
    /// 以请求方式建立SSE通道
    /// </summary>
    [RoutePrefix("api/ServerSentEvents")]
    public class ServerSentEventController : ApiController
    {

        // 接收浏览器请求,建立ServerSentEvents通道
        [HttpGet, Route("BuildingSse")]
        public System.Net.Http.HttpResponseMessage BuildingSse(HttpRequestMessage request)
        {
            System.Net.Http.HttpResponseMessage response = request.CreateResponse();
            response.Content = new System.Net.Http.PushStreamContent((Action<Stream, HttpContent, TransportContext>)WriteToStream, new MediaTypeHeaderValue("text/event-stream"));
            return response;
        }

        private static readonly ConcurrentDictionary<StreamWriter, StreamWriter> _streammessage = new ConcurrentDictionary<StreamWriter, StreamWriter>();
        public void WriteToStream(Stream outputStream, HttpContent content, TransportContext context)
        {
            StreamWriter streamwriter = new StreamWriter(outputStream);
            _streammessage.TryAdd(streamwriter, streamwriter);
        }

        // 建立SSE通道后,其他Controller或程序调用此方法,可以向浏览器端主动推送消息
        public static void SendSseMsg(SseMessageObject sseMsg)
        {
            MessageCallback(sseMsg);
        }

        // 设置向浏览器推送的消息内容
        private static void MessageCallback(SseMessageObject sseMsg)
        {
            foreach (var subscriber in _streammessage.ToArray())
            {
                try
                {
                    subscriber.Value.WriteLine(string.Format("id: {0}\n", sseMsg.MsgId));
                    subscriber.Value.WriteLine(string.Format("data: {0}\n\n", sseMsg.MsgData));
                    subscriber.Value.Flush();
                }
                catch
                {
                    StreamWriter streamWriter;
                    _streammessage.TryRemove(subscriber.Value, out streamWriter);
                }
            }
        }
    }
}

(服务器端)成功建立SSE通道后,向浏览器推送消息:

// 服务端向网页端推送告警信息
var sseMsg = new SseMessageObject();
sseMsg.MsgId = "1101";
sseMsg.MsgData = "自定义告警消息";
ServerSentEventController.SendSseMsg(sseMsg);

到此这篇关于C#实现SSE(Server-Sent Events)服务端和客户端的示例代码的文章就介绍到这了,更多相关C# SSE服务端和客户端内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文