2
0

WebSocketNotifier.cs 1.8 KB

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