WebSocketNotifier.cs 1.8 KB

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