单击按钮后使用WebSocket发送推送通知

问题描述 投票:0回答:1

我非常接近用C#实现一个系统,该系统使我可以通过单击按钮将推送通知从ASP.net编写的界面发送到充当客户端的C#控制台应用程序。全部使用WebSockets。

阅读了大量教程并重用在线找到的代码后,我已经能够成功建立WebSocket连接。我还无法实际发送通知。

我苦苦挣扎的部分是单击按钮后立即触发的功能:

//Close ticket and send push-notification over websocket
public void Close(int id) {
    //Ticket ticket = mgr.GetTicket(id);

    //Create a new notification
    Notification notif = new Notification();
    notif.message = "Rofl test123 Notification lol";

    //Initialize WebSocketMiddleware here??
    //WebSocketsMiddleware wsm = new WebSocketsMiddleware(what parameter??);
    //wsm.Invoke(what HttpContext parameter???)

    NotificationManager notifMgr;
    //notifMgr.AddSubscriber(wsm);
    //notifMgr.SendNotificationAsync(notif);


    return;
}

我遇到的具体问题/问题是:

  1. 如何初始化WebSocketsMiddleware类?是否需要初始化,如果是,类型为RequestDelegate的参数是什么?我该如何传递给该参数?
  2. WebSocketsMiddleware具有参数类型为HttpContext的Invoke函数。我只需要传递new HttpContext()即可吗?够了吗?
  3. 有人创建了一个NotificationManager类,该类使用中间件实际发送通知。我是否只需要将初始化的WebSocketsMiddleware变量作为NotificationManager.AddSubscriber()的参数传递?那么,每个客户的通知会很好地分开吗?
  4. 之后我可以只使用SendNotificationAsync()发送通知吗?
  5. 奖金问题:假设每个客户都有自己的按钮。当我单击客户的按钮时,只有该客户可能会收到推送通知。如何确保所有其他客户端也不会收到相同的通知?

为了能够帮助我解决这些问题,您需要以下课程。问题仅与WebSockets有关,而与如何初始化和使用我从教程中收集的类有关。

Notification.cs-表示通知的类(通知文本,发送日期等):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SC.UI.MVC.Models
{
    public class Notification
    {
        public Guid? notificationId { get; set; }
        public int id { get; set; }
        public DateTime timestamp { get; set; }
        public string message { get; set; }
        public string type { get; set; }

        public Notification()
        {
            // add a new guid as a unique identifier for the notification in the db
            notificationId = Guid.NewGuid();
        }
    }
}

WebSocketsMiddleware.cs-已处理WebSockets的低级部分,调用连接等:

using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;

namespace NotificationsApi.Notifications
{
    public class WebSocketsMiddleware
    {
        // private variable to track the next delegate to call in the request chain
        private readonly RequestDelegate _next;

        public WebSocketsMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task Invoke(HttpContext context)
        {
            CancellationToken ct = context.RequestAborted;
            string currentSubscriberId = null;
            WebSocket currentSocket = null;

            // we want to listen on a specific path for websocket communications
            if (context.Request.Path == "/notifications/ws")
            {
                // make sure the request is a websocket request
                if (context.WebSockets.IsWebSocketRequest)
                {
                    currentSocket = await context.WebSockets.AcceptWebSocketAsync();
                    currentSubscriberId = NotificationManager.Instance.AddSubscriber(currentSocket);

                    // keep the socket open until we get a cancellation request
                    while (true)
                    {
                        if (ct.IsCancellationRequested)
                        {
                            break;
                        }
                    }
                }
                else // return an HTTP bad request status code if anything other a web socket request is made on this URI
                { 
                    context.Response.StatusCode = 400;
                }
            }

            // clean up the socket
            if (!string.IsNullOrWhiteSpace(currentSubscriberId))
            {
                NotificationManager.Instance.RemoveSubscriber(currentSubscriberId);
                if (currentSocket != null)
                {
                    await currentSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "Closing", CancellationToken.None);
                    currentSocket.Dispose();
                }
            }

            // call the next delegate in the pipeline
            await _next(context);
            return;
        }
    }
}

NotificationManager.cs-具有三个功能的接口/类,用于添加和删除订户,以及实际发送通知。使用WebSocket中间件来实现此目的:

using SC.UI.MVC.Models;
//using NotificationsApi.Persistence;
using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace NotificationsApi.Notifications
{
    // interface for NotificationManager for dependency injection
    public interface INotificationManager
    {
        string AddSubscriber(WebSocket subscriber);
        void RemoveSubscriber(string subscriberId);
        Task SendNotificationAsync(Notification notification);
    }

    public class NotificationManager : INotificationManager 
    {
        // static instance of the NotificationManager class
        private static INotificationManager _instance;
        public static INotificationManager Instance { get { return _instance ?? (_instance = new NotificationManager()); } set { _instance = value; } }

        // static dictionary to keep track of all notification subscribers
        private static ConcurrentDictionary<string, WebSocket> _subscribers = new ConcurrentDictionary<string, WebSocket>();

        // adds a subscriber to receive notifications
        public string AddSubscriber(WebSocket subscriber)
        {
            var subscriberId = Guid.NewGuid().ToString();
            _subscribers.TryAdd(subscriberId, subscriber);
            return subscriberId.ToString();
        }

        // removes a notifications subscriber
        public void RemoveSubscriber(string subscriberId)
        {
            WebSocket empty;
            _subscribers.TryRemove(subscriberId, out empty);
        }

        // sends a notification to all subscribers
        public async Task SendNotificationAsync(Notification notification)
        {
            // add the notification to the persistence store
            //await PersistenceManager.Instance.AddNotificationAsync(notification);

            // send the notification to all subscribers
            foreach (var s in _subscribers)
            {
                if (s.Value.State == WebSocketState.Open)
                {
                    var jsonNotification = JsonConvert.SerializeObject(notification);
                    await SendStringAsync(s.Value, jsonNotification);
                }
            }
        }

        // sends a string via web socket communication
        private async Task SendStringAsync(WebSocket socket, string data, CancellationToken ct = default(CancellationToken))
        {
            var buffer = Encoding.UTF8.GetBytes(data);
            var segment = new ArraySegment<byte>(buffer);
            await socket.SendAsync(segment, WebSocketMessageType.Text, true, ct);
        }
    }
}

Client.cs-客户端收到推送通知。我猜这里真的不是问题:

    /* WEBSOCKET PART */
    //Variables for websocket
    private static object consoleLock = new object();
    private const int sendChunkSize = 256;
    private const int receiveChunkSize = 256;
    private const bool verbose = true;
    private static readonly TimeSpan delay = TimeSpan.FromMilliseconds(30000);

    //Function to check if a ticket from this client is closed/solved
    public void checkTicketSolved() {
        Thread.Sleep(1000);
        Connect("ws://localhost:5050/notifications/ws").Wait();
        Console.WriteLine("Press any key to exit...");
    }

    public static async Task Connect(string uri)
    {
        ClientWebSocket webSocket = null;

        try
        {
            webSocket = new ClientWebSocket();
            await webSocket.ConnectAsync(new Uri(uri), CancellationToken.None);
            await Task.WhenAll(Receive(webSocket), Send(webSocket));
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception: {0}", ex);
        }
        finally
        {
            if (webSocket != null)
                webSocket.Dispose();
            Console.WriteLine();

            lock (consoleLock)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("WebSocket closed.");
                Console.ResetColor();
            }
        }
    }
   static UTF8Encoding encoder = new UTF8Encoding();

    private static async Task Send(ClientWebSocket webSocket)
    {

        //byte[] buffer = encoder.GetBytes("{\"op\":\"blocks_sub\"}"); //"{\"op\":\"unconfirmed_sub\"}");
        byte[] buffer = encoder.GetBytes("{\"op\":\"unconfirmed_sub\"}");
        await webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Text, true, CancellationToken.None);

      while (webSocket.State == WebSocketState.Open)
        {
            LogStatus(false, buffer, buffer.Length);
            await Task.Delay(delay);
        }
    }

    private static async Task Receive(ClientWebSocket webSocket)
    {
        byte[] buffer = new byte[receiveChunkSize];
        while (webSocket.State == WebSocketState.Open)
        {
            var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
            if (result.MessageType == WebSocketMessageType.Close)
            {
                await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
            }
            else
            {
                LogStatus(true, buffer, result.Count);
            }
        }
    }

    private static void LogStatus(bool receiving, byte[] buffer, int length)
    {
        lock (consoleLock)
        {
            Console.ForegroundColor = receiving ? ConsoleColor.Green : ConsoleColor.Gray;
            //Console.WriteLine("{0} ", receiving ? "Received" : "Sent");

            if (verbose)
                Console.WriteLine(encoder.GetString(buffer));

            Console.ResetColor();
        }
    }
}

您也可以在Github上找到此代码。代码的相关部分位于:

  • WebServer / UI-MVC / Controllers / TicketController.cs->包含单击按钮时触发的功能。
  • WebServer / UI-MVC / Notifications->包含NotificationManager.cs和WebSocketsMiddleware.cs
  • WebServer / UI-MVC / Models->包含Notification.cs
  • Client /包含客户端控制台应用程序的所有代码

为您提供有关该应用程序的一些背景信息:此应用程序代表一种票务系统,该系统允许使用我的软件的客户/客户打开支持票证。 WebServer部分供我的管理员/员工回答和管理票证。我的客户/客户需要安装控制台应用程序才能联系我的支持服务并打开支持通知单。当管理员通过单击按钮关闭客户的凭单时,这意味着凭单,从而解决并关闭了客户的问题。结果导致客户端收到关于此的推送通知。

我不是在寻找有关WebSockets的其他教程的参考,也不是在寻找使用SignalR或其他方法的建议,我已经阅读了所有内容,并且已经在使用SignalR,但是现在对纯WebSockets感兴趣。对于能帮助我解决此问题中发布的第一部分代码(关闭功能)并解释他的工作的人,我将非常感激。谢谢!

c# asp.net websocket
1个回答
0
投票

我自己找到了解决方案。

首先,我创建了一个名为NotificationsController.cs的新控制器。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using SC.UI.MVC.Models;
using NotificationsApi.Notifications;
//using NotificationsApi.Persistence;
using System.Net.Http;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace NotificationsApi.Controllers
{
    [Route("api/notifications")]
    public class NotificationsController : Controller
    {
        // GET api/notifications
        [HttpGet]
        public ActionResult Get()
        {
            try
            {
                var notifications = new List<Notification>();
                //notifications = PersistenceManager.Instance.GetNotifications();
                return Ok(notifications);
            }
            catch (Exception exception)
            {
                // log exception
                // TODO: implement logging

                // return a 500
                return StatusCode(500);
            }
        }

        // POST api/notifications
        [HttpPost]
        public async Task<ActionResult> Post(string message)
        {

            Notification notification = new Notification();
            notification.message = message;

            Console.WriteLine(message);

            try
            {
                // return a 400 if we didn't get a valid json payload in the body
                if (notification == null)
                    return BadRequest();

                await NotificationManager.Instance.SendNotificationAsync(notification);

                // we aren't returning the object to reference because POSTing a notification is fire and forget
                return Created(string.Empty, null);
            }
            catch (Exception exception)
            {
                // log the error
                // TODO: implement logging

                // return a 500
                return StatusCode(500);
            }

            return Ok();
        }
    }
}

然后,我注释掉了Notification.cs中不必要的属性,因此仅保留Guid和message。现在,我可以通过使用带有消息参数作为数据的POST请求调用NotificationsController来发送通知。

© www.soinside.com 2019 - 2024. All rights reserved.