WebSocketNotifier.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using MediaBrowser.Controller.Net;
  2. using MediaBrowser.Controller.Notifications;
  3. using MediaBrowser.Controller.Plugins;
  4. using System.Linq;
  5. namespace Emby.Server.Implementations.Notifications
  6. {
  7. /// <summary>
  8. /// Notifies clients anytime a notification is added or udpated
  9. /// </summary>
  10. public class WebSocketNotifier : IServerEntryPoint
  11. {
  12. private readonly INotificationsRepository _notificationsRepo;
  13. private readonly IServerManager _serverManager;
  14. public WebSocketNotifier(INotificationsRepository notificationsRepo, IServerManager serverManager)
  15. {
  16. _notificationsRepo = notificationsRepo;
  17. _serverManager = serverManager;
  18. }
  19. public void Run()
  20. {
  21. _notificationsRepo.NotificationAdded += _notificationsRepo_NotificationAdded;
  22. _notificationsRepo.NotificationsMarkedRead += _notificationsRepo_NotificationsMarkedRead;
  23. }
  24. void _notificationsRepo_NotificationsMarkedRead(object sender, NotificationReadEventArgs e)
  25. {
  26. var list = e.IdList.ToList();
  27. list.Add(e.UserId);
  28. list.Add(e.IsRead.ToString().ToLower());
  29. var msg = string.Join("|", list.ToArray());
  30. _serverManager.SendWebSocketMessage("NotificationsMarkedRead", msg);
  31. }
  32. void _notificationsRepo_NotificationAdded(object sender, NotificationUpdateEventArgs e)
  33. {
  34. var msg = e.Notification.UserId + "|" + e.Notification.Id;
  35. _serverManager.SendWebSocketMessage("NotificationAdded", msg);
  36. }
  37. public void Dispose()
  38. {
  39. _notificationsRepo.NotificationAdded -= _notificationsRepo_NotificationAdded;
  40. }
  41. }
  42. }