浏览代码

Merge pull request #4210 from nielsvanvelzen/typed-websocket-message

Use enum for WebSocket message types
Bond-009 4 年之前
父节点
当前提交
e011659186
共有 24 个文件被更改,包括 156 次插入63 次删除
  1. 4 4
      Emby.Dlna/PlayTo/PlayToController.cs
  2. 4 3
      Emby.Server.Implementations/EntryPoints/LibraryChangedNotifier.cs
  3. 6 5
      Emby.Server.Implementations/EntryPoints/RecordingNotifier.cs
  4. 1 1
      Emby.Server.Implementations/EntryPoints/UserDataChangeNotifier.cs
  5. 3 2
      Emby.Server.Implementations/HttpServer/WebSocketConnection.cs
  6. 14 14
      Emby.Server.Implementations/Session/SessionManager.cs
  7. 2 1
      Emby.Server.Implementations/Session/SessionWebSocketListener.cs
  8. 2 1
      Emby.Server.Implementations/Session/WebSocketController.cs
  9. 9 5
      Jellyfin.Api/WebSocketListeners/ActivityLogWebSocketListener.cs
  10. 9 5
      Jellyfin.Api/WebSocketListeners/ScheduledTasksWebSocketListener.cs
  11. 8 1
      Jellyfin.Api/WebSocketListeners/SessionInfoWebSocketListener.cs
  12. 2 1
      Jellyfin.Server.Implementations/Events/Consumers/System/TaskCompletedNotifier.cs
  13. 2 1
      Jellyfin.Server.Implementations/Events/Consumers/Updates/PluginInstallationCancelledNotifier.cs
  14. 2 1
      Jellyfin.Server.Implementations/Events/Consumers/Updates/PluginInstallationFailedNotifier.cs
  15. 2 1
      Jellyfin.Server.Implementations/Events/Consumers/Updates/PluginInstalledNotifier.cs
  16. 2 1
      Jellyfin.Server.Implementations/Events/Consumers/Updates/PluginInstallingNotifier.cs
  17. 2 1
      Jellyfin.Server.Implementations/Events/Consumers/Updates/PluginUninstalledNotifier.cs
  18. 2 1
      Jellyfin.Server.Implementations/Events/Consumers/Users/UserDeletedNotifier.cs
  19. 2 1
      Jellyfin.Server.Implementations/Events/Consumers/Users/UserUpdatedNotifier.cs
  20. 20 7
      MediaBrowser.Controller/Net/BasePeriodicWebSocketListener.cs
  21. 2 1
      MediaBrowser.Controller/Session/ISessionController.cs
  22. 4 4
      MediaBrowser.Controller/Session/ISessionManager.cs
  23. 2 1
      MediaBrowser.Model/Net/WebSocketMessage.cs
  24. 50 0
      MediaBrowser.Model/Session/SessionMessageType.cs

+ 4 - 4
Emby.Dlna/PlayTo/PlayToController.cs

@@ -811,7 +811,7 @@ namespace Emby.Dlna.PlayTo
         }
 
         /// <inheritdoc />
-        public Task SendMessage<T>(string name, Guid messageId, T data, CancellationToken cancellationToken)
+        public Task SendMessage<T>(SessionMessageType name, Guid messageId, T data, CancellationToken cancellationToken)
         {
             if (_disposed)
             {
@@ -823,17 +823,17 @@ namespace Emby.Dlna.PlayTo
                 return Task.CompletedTask;
             }
 
-            if (string.Equals(name, "Play", StringComparison.OrdinalIgnoreCase))
+            if (name == SessionMessageType.Play)
             {
                 return SendPlayCommand(data as PlayRequest, cancellationToken);
             }
 
-            if (string.Equals(name, "PlayState", StringComparison.OrdinalIgnoreCase))
+            if (name == SessionMessageType.PlayState)
             {
                 return SendPlaystateCommand(data as PlaystateRequest, cancellationToken);
             }
 
-            if (string.Equals(name, "GeneralCommand", StringComparison.OrdinalIgnoreCase))
+            if (name == SessionMessageType.GeneralCommand)
             {
                 return SendGeneralCommand(data as GeneralCommand, cancellationToken);
             }

+ 4 - 3
Emby.Server.Implementations/EntryPoints/LibraryChangedNotifier.cs

@@ -16,6 +16,7 @@ using MediaBrowser.Controller.Plugins;
 using MediaBrowser.Controller.Providers;
 using MediaBrowser.Controller.Session;
 using MediaBrowser.Model.Entities;
+using MediaBrowser.Model.Session;
 using Microsoft.Extensions.Logging;
 
 namespace Emby.Server.Implementations.EntryPoints
@@ -105,7 +106,7 @@ namespace Emby.Server.Implementations.EntryPoints
 
             try
             {
-                _sessionManager.SendMessageToAdminSessions("RefreshProgress", dict, CancellationToken.None);
+                _sessionManager.SendMessageToAdminSessions(SessionMessageType.RefreshProgress, dict, CancellationToken.None);
             }
             catch
             {
@@ -123,7 +124,7 @@ namespace Emby.Server.Implementations.EntryPoints
 
                 try
                 {
-                    _sessionManager.SendMessageToAdminSessions("RefreshProgress", collectionFolderDict, CancellationToken.None);
+                    _sessionManager.SendMessageToAdminSessions(SessionMessageType.RefreshProgress, collectionFolderDict, CancellationToken.None);
                 }
                 catch
                 {
@@ -345,7 +346,7 @@ namespace Emby.Server.Implementations.EntryPoints
 
                 try
                 {
-                    await _sessionManager.SendMessageToUserSessions(new List<Guid> { userId }, "LibraryChanged", info, cancellationToken).ConfigureAwait(false);
+                    await _sessionManager.SendMessageToUserSessions(new List<Guid> { userId }, SessionMessageType.LibraryChanged, info, cancellationToken).ConfigureAwait(false);
                 }
                 catch (Exception ex)
                 {

+ 6 - 5
Emby.Server.Implementations/EntryPoints/RecordingNotifier.cs

@@ -10,6 +10,7 @@ using MediaBrowser.Controller.Library;
 using MediaBrowser.Controller.LiveTv;
 using MediaBrowser.Controller.Plugins;
 using MediaBrowser.Controller.Session;
+using MediaBrowser.Model.Session;
 using Microsoft.Extensions.Logging;
 
 namespace Emby.Server.Implementations.EntryPoints
@@ -46,25 +47,25 @@ namespace Emby.Server.Implementations.EntryPoints
 
         private async void OnLiveTvManagerSeriesTimerCreated(object sender, GenericEventArgs<TimerEventInfo> e)
         {
-            await SendMessage("SeriesTimerCreated", e.Argument).ConfigureAwait(false);
+            await SendMessage(SessionMessageType.SeriesTimerCreated, e.Argument).ConfigureAwait(false);
         }
 
         private async void OnLiveTvManagerTimerCreated(object sender, GenericEventArgs<TimerEventInfo> e)
         {
-            await SendMessage("TimerCreated", e.Argument).ConfigureAwait(false);
+            await SendMessage(SessionMessageType.TimerCreated, e.Argument).ConfigureAwait(false);
         }
 
         private async void OnLiveTvManagerSeriesTimerCancelled(object sender, GenericEventArgs<TimerEventInfo> e)
         {
-            await SendMessage("SeriesTimerCancelled", e.Argument).ConfigureAwait(false);
+            await SendMessage(SessionMessageType.SeriesTimerCancelled, e.Argument).ConfigureAwait(false);
         }
 
         private async void OnLiveTvManagerTimerCancelled(object sender, GenericEventArgs<TimerEventInfo> e)
         {
-            await SendMessage("TimerCancelled", e.Argument).ConfigureAwait(false);
+            await SendMessage(SessionMessageType.TimerCancelled, e.Argument).ConfigureAwait(false);
         }
 
-        private async Task SendMessage(string name, TimerEventInfo info)
+        private async Task SendMessage(SessionMessageType name, TimerEventInfo info)
         {
             var users = _userManager.Users.Where(i => i.HasPermission(PermissionKind.EnableLiveTvAccess)).Select(i => i.Id).ToList();
 

+ 1 - 1
Emby.Server.Implementations/EntryPoints/UserDataChangeNotifier.cs

@@ -115,7 +115,7 @@ namespace Emby.Server.Implementations.EntryPoints
 
         private Task SendNotifications(Guid userId, List<BaseItem> changedItems, CancellationToken cancellationToken)
         {
-            return _sessionManager.SendMessageToUserSessions(new List<Guid> { userId }, "UserDataChanged", () => GetUserDataChangeInfo(userId, changedItems), cancellationToken);
+            return _sessionManager.SendMessageToUserSessions(new List<Guid> { userId }, SessionMessageType.UserDataChanged, () => GetUserDataChangeInfo(userId, changedItems), cancellationToken);
         }
 
         private UserDataChangeInfo GetUserDataChangeInfo(Guid userId, List<BaseItem> changedItems)

+ 3 - 2
Emby.Server.Implementations/HttpServer/WebSocketConnection.cs

@@ -11,6 +11,7 @@ using System.Threading.Tasks;
 using MediaBrowser.Common.Json;
 using MediaBrowser.Controller.Net;
 using MediaBrowser.Model.Net;
+using MediaBrowser.Model.Session;
 using Microsoft.AspNetCore.Http;
 using Microsoft.Extensions.Logging;
 
@@ -227,7 +228,7 @@ namespace Emby.Server.Implementations.HttpServer
                 Connection = this
             };
 
-            if (info.MessageType.Equals("KeepAlive", StringComparison.Ordinal))
+            if (info.MessageType == SessionMessageType.KeepAlive)
             {
                 await SendKeepAliveResponse().ConfigureAwait(false);
             }
@@ -244,7 +245,7 @@ namespace Emby.Server.Implementations.HttpServer
                 new WebSocketMessage<string>
                 {
                     MessageId = Guid.NewGuid(),
-                    MessageType = "KeepAlive"
+                    MessageType = SessionMessageType.KeepAlive
                 }, CancellationToken.None);
         }
 

+ 14 - 14
Emby.Server.Implementations/Session/SessionManager.cs

@@ -1064,10 +1064,10 @@ namespace Emby.Server.Implementations.Session
                 AssertCanControl(session, controllingSession);
             }
 
-            return SendMessageToSession(session, "GeneralCommand", command, cancellationToken);
+            return SendMessageToSession(session, SessionMessageType.GeneralCommand, command, cancellationToken);
         }
 
-        private static async Task SendMessageToSession<T>(SessionInfo session, string name, T data, CancellationToken cancellationToken)
+        private static async Task SendMessageToSession<T>(SessionInfo session, SessionMessageType name, T data, CancellationToken cancellationToken)
         {
             var controllers = session.SessionControllers;
             var messageId = Guid.NewGuid();
@@ -1078,7 +1078,7 @@ namespace Emby.Server.Implementations.Session
             }
         }
 
-        private static Task SendMessageToSessions<T>(IEnumerable<SessionInfo> sessions, string name, T data, CancellationToken cancellationToken)
+        private static Task SendMessageToSessions<T>(IEnumerable<SessionInfo> sessions, SessionMessageType name, T data, CancellationToken cancellationToken)
         {
             IEnumerable<Task> GetTasks()
             {
@@ -1178,7 +1178,7 @@ namespace Emby.Server.Implementations.Session
                 }
             }
 
-            await SendMessageToSession(session, "Play", command, cancellationToken).ConfigureAwait(false);
+            await SendMessageToSession(session, SessionMessageType.Play, command, cancellationToken).ConfigureAwait(false);
         }
 
         /// <inheritdoc />
@@ -1186,7 +1186,7 @@ namespace Emby.Server.Implementations.Session
         {
             CheckDisposed();
             var session = GetSessionToRemoteControl(sessionId);
-            await SendMessageToSession(session, "SyncPlayCommand", command, cancellationToken).ConfigureAwait(false);
+            await SendMessageToSession(session, SessionMessageType.SyncPlayCommand, command, cancellationToken).ConfigureAwait(false);
         }
 
         /// <inheritdoc />
@@ -1194,7 +1194,7 @@ namespace Emby.Server.Implementations.Session
         {
             CheckDisposed();
             var session = GetSessionToRemoteControl(sessionId);
-            await SendMessageToSession(session, "SyncPlayGroupUpdate", command, cancellationToken).ConfigureAwait(false);
+            await SendMessageToSession(session, SessionMessageType.SyncPlayGroupUpdate, command, cancellationToken).ConfigureAwait(false);
         }
 
         private IEnumerable<BaseItem> TranslateItemForPlayback(Guid id, User user)
@@ -1297,7 +1297,7 @@ namespace Emby.Server.Implementations.Session
                 }
             }
 
-            return SendMessageToSession(session, "Playstate", command, cancellationToken);
+            return SendMessageToSession(session, SessionMessageType.PlayState, command, cancellationToken);
         }
 
         private static void AssertCanControl(SessionInfo session, SessionInfo controllingSession)
@@ -1322,7 +1322,7 @@ namespace Emby.Server.Implementations.Session
         {
             CheckDisposed();
 
-            return SendMessageToSessions(Sessions, "RestartRequired", string.Empty, cancellationToken);
+            return SendMessageToSessions(Sessions, SessionMessageType.RestartRequired, string.Empty, cancellationToken);
         }
 
         /// <summary>
@@ -1334,7 +1334,7 @@ namespace Emby.Server.Implementations.Session
         {
             CheckDisposed();
 
-            return SendMessageToSessions(Sessions, "ServerShuttingDown", string.Empty, cancellationToken);
+            return SendMessageToSessions(Sessions, SessionMessageType.ServerShuttingDown, string.Empty, cancellationToken);
         }
 
         /// <summary>
@@ -1348,7 +1348,7 @@ namespace Emby.Server.Implementations.Session
 
             _logger.LogDebug("Beginning SendServerRestartNotification");
 
-            return SendMessageToSessions(Sessions, "ServerRestarting", string.Empty, cancellationToken);
+            return SendMessageToSessions(Sessions, SessionMessageType.ServerRestarting, string.Empty, cancellationToken);
         }
 
         /// <summary>
@@ -1874,7 +1874,7 @@ namespace Emby.Server.Implementations.Session
         }
 
         /// <inheritdoc />
-        public Task SendMessageToAdminSessions<T>(string name, T data, CancellationToken cancellationToken)
+        public Task SendMessageToAdminSessions<T>(SessionMessageType name, T data, CancellationToken cancellationToken)
         {
             CheckDisposed();
 
@@ -1887,7 +1887,7 @@ namespace Emby.Server.Implementations.Session
         }
 
         /// <inheritdoc />
-        public Task SendMessageToUserSessions<T>(List<Guid> userIds, string name, Func<T> dataFn, CancellationToken cancellationToken)
+        public Task SendMessageToUserSessions<T>(List<Guid> userIds, SessionMessageType name, Func<T> dataFn, CancellationToken cancellationToken)
         {
             CheckDisposed();
 
@@ -1902,7 +1902,7 @@ namespace Emby.Server.Implementations.Session
         }
 
         /// <inheritdoc />
-        public Task SendMessageToUserSessions<T>(List<Guid> userIds, string name, T data, CancellationToken cancellationToken)
+        public Task SendMessageToUserSessions<T>(List<Guid> userIds, SessionMessageType name, T data, CancellationToken cancellationToken)
         {
             CheckDisposed();
 
@@ -1911,7 +1911,7 @@ namespace Emby.Server.Implementations.Session
         }
 
         /// <inheritdoc />
-        public Task SendMessageToUserDeviceSessions<T>(string deviceId, string name, T data, CancellationToken cancellationToken)
+        public Task SendMessageToUserDeviceSessions<T>(string deviceId, SessionMessageType name, T data, CancellationToken cancellationToken)
         {
             CheckDisposed();
 

+ 2 - 1
Emby.Server.Implementations/Session/SessionWebSocketListener.cs

@@ -8,6 +8,7 @@ using Jellyfin.Data.Events;
 using MediaBrowser.Controller.Net;
 using MediaBrowser.Controller.Session;
 using MediaBrowser.Model.Net;
+using MediaBrowser.Model.Session;
 using Microsoft.AspNetCore.Http;
 using Microsoft.Extensions.Logging;
 
@@ -316,7 +317,7 @@ namespace Emby.Server.Implementations.Session
             return webSocket.SendAsync(
                 new WebSocketMessage<int>
                 {
-                    MessageType = "ForceKeepAlive",
+                    MessageType = SessionMessageType.ForceKeepAlive,
                     Data = WebSocketLostTimeout
                 },
                 CancellationToken.None);

+ 2 - 1
Emby.Server.Implementations/Session/WebSocketController.cs

@@ -11,6 +11,7 @@ using System.Threading.Tasks;
 using MediaBrowser.Controller.Net;
 using MediaBrowser.Controller.Session;
 using MediaBrowser.Model.Net;
+using MediaBrowser.Model.Session;
 using Microsoft.Extensions.Logging;
 
 namespace Emby.Server.Implementations.Session
@@ -65,7 +66,7 @@ namespace Emby.Server.Implementations.Session
 
         /// <inheritdoc />
         public Task SendMessage<T>(
-            string name,
+            SessionMessageType name,
             Guid messageId,
             T data,
             CancellationToken cancellationToken)

+ 9 - 5
Jellyfin.Api/WebSocketListeners/ActivityLogWebSocketListener.cs

@@ -3,6 +3,7 @@ using System.Threading.Tasks;
 using Jellyfin.Data.Events;
 using MediaBrowser.Controller.Net;
 using MediaBrowser.Model.Activity;
+using MediaBrowser.Model.Session;
 using Microsoft.Extensions.Logging;
 
 namespace Jellyfin.Api.WebSocketListeners
@@ -29,11 +30,14 @@ namespace Jellyfin.Api.WebSocketListeners
             _activityManager.EntryCreated += OnEntryCreated;
         }
 
-        /// <summary>
-        /// Gets the name.
-        /// </summary>
-        /// <value>The name.</value>
-        protected override string Name => "ActivityLogEntry";
+        /// <inheritdoc />
+        protected override SessionMessageType Type => SessionMessageType.ActivityLogEntry;
+
+        /// <inheritdoc />
+        protected override SessionMessageType StartType => SessionMessageType.ActivityLogEntryStart;
+
+        /// <inheritdoc />
+        protected override SessionMessageType StopType => SessionMessageType.ActivityLogEntryStop;
 
         /// <summary>
         /// Gets the data to send.

+ 9 - 5
Jellyfin.Api/WebSocketListeners/ScheduledTasksWebSocketListener.cs

@@ -3,6 +3,7 @@ using System.Linq;
 using System.Threading.Tasks;
 using Jellyfin.Data.Events;
 using MediaBrowser.Controller.Net;
+using MediaBrowser.Model.Session;
 using MediaBrowser.Model.Tasks;
 using Microsoft.Extensions.Logging;
 
@@ -33,11 +34,14 @@ namespace Jellyfin.Api.WebSocketListeners
             _taskManager.TaskCompleted += OnTaskCompleted;
         }
 
-        /// <summary>
-        /// Gets the name.
-        /// </summary>
-        /// <value>The name.</value>
-        protected override string Name => "ScheduledTasksInfo";
+        /// <inheritdoc />
+        protected override SessionMessageType Type => SessionMessageType.ScheduledTasksInfo;
+
+        /// <inheritdoc />
+        protected override SessionMessageType StartType => SessionMessageType.ScheduledTasksInfoStart;
+
+        /// <inheritdoc />
+        protected override SessionMessageType StopType => SessionMessageType.ScheduledTasksInfoStop;
 
         /// <summary>
         /// Gets the data to send.

+ 8 - 1
Jellyfin.Api/WebSocketListeners/SessionInfoWebSocketListener.cs

@@ -3,6 +3,7 @@ using System.Threading.Tasks;
 using MediaBrowser.Controller.Library;
 using MediaBrowser.Controller.Net;
 using MediaBrowser.Controller.Session;
+using MediaBrowser.Model.Session;
 using Microsoft.Extensions.Logging;
 
 namespace Jellyfin.Api.WebSocketListeners
@@ -34,7 +35,13 @@ namespace Jellyfin.Api.WebSocketListeners
         }
 
         /// <inheritdoc />
-        protected override string Name => "Sessions";
+        protected override SessionMessageType Type => SessionMessageType.Sessions;
+
+        /// <inheritdoc />
+        protected override SessionMessageType StartType => SessionMessageType.SessionsStart;
+
+        /// <inheritdoc />
+        protected override SessionMessageType StopType => SessionMessageType.SessionsStop;
 
         /// <summary>
         /// Gets the data to send.

+ 2 - 1
Jellyfin.Server.Implementations/Events/Consumers/System/TaskCompletedNotifier.cs

@@ -2,6 +2,7 @@
 using System.Threading.Tasks;
 using MediaBrowser.Controller.Events;
 using MediaBrowser.Controller.Session;
+using MediaBrowser.Model.Session;
 using MediaBrowser.Model.Tasks;
 
 namespace Jellyfin.Server.Implementations.Events.Consumers.System
@@ -25,7 +26,7 @@ namespace Jellyfin.Server.Implementations.Events.Consumers.System
         /// <inheritdoc />
         public async Task OnEvent(TaskCompletionEventArgs eventArgs)
         {
-            await _sessionManager.SendMessageToAdminSessions("ScheduledTaskEnded", eventArgs.Result, CancellationToken.None).ConfigureAwait(false);
+            await _sessionManager.SendMessageToAdminSessions(SessionMessageType.ScheduledTaskEnded, eventArgs.Result, CancellationToken.None).ConfigureAwait(false);
         }
     }
 }

+ 2 - 1
Jellyfin.Server.Implementations/Events/Consumers/Updates/PluginInstallationCancelledNotifier.cs

@@ -3,6 +3,7 @@ using System.Threading.Tasks;
 using MediaBrowser.Controller.Events;
 using MediaBrowser.Controller.Events.Updates;
 using MediaBrowser.Controller.Session;
+using MediaBrowser.Model.Session;
 
 namespace Jellyfin.Server.Implementations.Events.Consumers.Updates
 {
@@ -25,7 +26,7 @@ namespace Jellyfin.Server.Implementations.Events.Consumers.Updates
         /// <inheritdoc />
         public async Task OnEvent(PluginInstallationCancelledEventArgs eventArgs)
         {
-            await _sessionManager.SendMessageToAdminSessions("PackageInstallationCancelled", eventArgs.Argument, CancellationToken.None).ConfigureAwait(false);
+            await _sessionManager.SendMessageToAdminSessions(SessionMessageType.PackageInstallationCancelled, eventArgs.Argument, CancellationToken.None).ConfigureAwait(false);
         }
     }
 }

+ 2 - 1
Jellyfin.Server.Implementations/Events/Consumers/Updates/PluginInstallationFailedNotifier.cs

@@ -3,6 +3,7 @@ using System.Threading.Tasks;
 using MediaBrowser.Common.Updates;
 using MediaBrowser.Controller.Events;
 using MediaBrowser.Controller.Session;
+using MediaBrowser.Model.Session;
 
 namespace Jellyfin.Server.Implementations.Events.Consumers.Updates
 {
@@ -25,7 +26,7 @@ namespace Jellyfin.Server.Implementations.Events.Consumers.Updates
         /// <inheritdoc />
         public async Task OnEvent(InstallationFailedEventArgs eventArgs)
         {
-            await _sessionManager.SendMessageToAdminSessions("PackageInstallationFailed", eventArgs.InstallationInfo, CancellationToken.None).ConfigureAwait(false);
+            await _sessionManager.SendMessageToAdminSessions(SessionMessageType.PackageInstallationFailed, eventArgs.InstallationInfo, CancellationToken.None).ConfigureAwait(false);
         }
     }
 }

+ 2 - 1
Jellyfin.Server.Implementations/Events/Consumers/Updates/PluginInstalledNotifier.cs

@@ -3,6 +3,7 @@ using System.Threading.Tasks;
 using MediaBrowser.Controller.Events;
 using MediaBrowser.Controller.Events.Updates;
 using MediaBrowser.Controller.Session;
+using MediaBrowser.Model.Session;
 
 namespace Jellyfin.Server.Implementations.Events.Consumers.Updates
 {
@@ -25,7 +26,7 @@ namespace Jellyfin.Server.Implementations.Events.Consumers.Updates
         /// <inheritdoc />
         public async Task OnEvent(PluginInstalledEventArgs eventArgs)
         {
-            await _sessionManager.SendMessageToAdminSessions("PackageInstallationCompleted", eventArgs.Argument, CancellationToken.None).ConfigureAwait(false);
+            await _sessionManager.SendMessageToAdminSessions(SessionMessageType.PackageInstallationCompleted, eventArgs.Argument, CancellationToken.None).ConfigureAwait(false);
         }
     }
 }

+ 2 - 1
Jellyfin.Server.Implementations/Events/Consumers/Updates/PluginInstallingNotifier.cs

@@ -3,6 +3,7 @@ using System.Threading.Tasks;
 using MediaBrowser.Controller.Events;
 using MediaBrowser.Controller.Events.Updates;
 using MediaBrowser.Controller.Session;
+using MediaBrowser.Model.Session;
 
 namespace Jellyfin.Server.Implementations.Events.Consumers.Updates
 {
@@ -25,7 +26,7 @@ namespace Jellyfin.Server.Implementations.Events.Consumers.Updates
         /// <inheritdoc />
         public async Task OnEvent(PluginInstallingEventArgs eventArgs)
         {
-            await _sessionManager.SendMessageToAdminSessions("PackageInstalling", eventArgs.Argument, CancellationToken.None).ConfigureAwait(false);
+            await _sessionManager.SendMessageToAdminSessions(SessionMessageType.PackageInstalling, eventArgs.Argument, CancellationToken.None).ConfigureAwait(false);
         }
     }
 }

+ 2 - 1
Jellyfin.Server.Implementations/Events/Consumers/Updates/PluginUninstalledNotifier.cs

@@ -3,6 +3,7 @@ using System.Threading.Tasks;
 using MediaBrowser.Controller.Events;
 using MediaBrowser.Controller.Events.Updates;
 using MediaBrowser.Controller.Session;
+using MediaBrowser.Model.Session;
 
 namespace Jellyfin.Server.Implementations.Events.Consumers.Updates
 {
@@ -25,7 +26,7 @@ namespace Jellyfin.Server.Implementations.Events.Consumers.Updates
         /// <inheritdoc />
         public async Task OnEvent(PluginUninstalledEventArgs eventArgs)
         {
-            await _sessionManager.SendMessageToAdminSessions("PluginUninstalled", eventArgs.Argument, CancellationToken.None).ConfigureAwait(false);
+            await _sessionManager.SendMessageToAdminSessions(SessionMessageType.PackageUninstalled, eventArgs.Argument, CancellationToken.None).ConfigureAwait(false);
         }
     }
 }

+ 2 - 1
Jellyfin.Server.Implementations/Events/Consumers/Users/UserDeletedNotifier.cs

@@ -6,6 +6,7 @@ using System.Threading.Tasks;
 using Jellyfin.Data.Events.Users;
 using MediaBrowser.Controller.Events;
 using MediaBrowser.Controller.Session;
+using MediaBrowser.Model.Session;
 
 namespace Jellyfin.Server.Implementations.Events.Consumers.Users
 {
@@ -30,7 +31,7 @@ namespace Jellyfin.Server.Implementations.Events.Consumers.Users
         {
             await _sessionManager.SendMessageToUserSessions(
                 new List<Guid> { eventArgs.Argument.Id },
-                "UserDeleted",
+                SessionMessageType.UserDeleted,
                 eventArgs.Argument.Id.ToString("N", CultureInfo.InvariantCulture),
                 CancellationToken.None).ConfigureAwait(false);
         }

+ 2 - 1
Jellyfin.Server.Implementations/Events/Consumers/Users/UserUpdatedNotifier.cs

@@ -6,6 +6,7 @@ using Jellyfin.Data.Events.Users;
 using MediaBrowser.Controller.Events;
 using MediaBrowser.Controller.Library;
 using MediaBrowser.Controller.Session;
+using MediaBrowser.Model.Session;
 
 namespace Jellyfin.Server.Implementations.Events.Consumers.Users
 {
@@ -33,7 +34,7 @@ namespace Jellyfin.Server.Implementations.Events.Consumers.Users
         {
             await _sessionManager.SendMessageToUserSessions(
                 new List<Guid> { e.Argument.Id },
-                "UserUpdated",
+                SessionMessageType.UserUpdated,
                 _userManager.GetUserDto(e.Argument),
                 CancellationToken.None).ConfigureAwait(false);
         }

+ 20 - 7
MediaBrowser.Controller/Net/BasePeriodicWebSocketListener.cs

@@ -8,6 +8,7 @@ using System.Net.WebSockets;
 using System.Threading;
 using System.Threading.Tasks;
 using MediaBrowser.Model.Net;
+using MediaBrowser.Model.Session;
 using Microsoft.Extensions.Logging;
 
 namespace MediaBrowser.Controller.Net
@@ -28,10 +29,22 @@ namespace MediaBrowser.Controller.Net
             new List<Tuple<IWebSocketConnection, CancellationTokenSource, TStateType>>();
 
         /// <summary>
-        /// Gets the name.
+        /// Gets the type used for the messages sent to the client.
         /// </summary>
-        /// <value>The name.</value>
-        protected abstract string Name { get; }
+        /// <value>The type.</value>
+        protected abstract SessionMessageType Type { get; }
+
+        /// <summary>
+        /// Gets the message type received from the client to start sending messages.
+        /// </summary>
+        /// <value>The type.</value>
+        protected abstract SessionMessageType StartType { get; }
+
+        /// <summary>
+        /// Gets the message type received from the client to stop sending messages.
+        /// </summary>
+        /// <value>The type.</value>
+        protected abstract SessionMessageType StopType { get; }
 
         /// <summary>
         /// Gets the data to send.
@@ -66,12 +79,12 @@ namespace MediaBrowser.Controller.Net
                 throw new ArgumentNullException(nameof(message));
             }
 
-            if (string.Equals(message.MessageType, Name + "Start", StringComparison.OrdinalIgnoreCase))
+            if (message.MessageType == StartType)
             {
                 Start(message);
             }
 
-            if (string.Equals(message.MessageType, Name + "Stop", StringComparison.OrdinalIgnoreCase))
+            if (message.MessageType == StopType)
             {
                 Stop(message);
             }
@@ -159,7 +172,7 @@ namespace MediaBrowser.Controller.Net
                         new WebSocketMessage<TReturnDataType>
                         {
                             MessageId = Guid.NewGuid(),
-                            MessageType = Name,
+                            MessageType = Type,
                             Data = data
                         },
                         cancellationToken).ConfigureAwait(false);
@@ -176,7 +189,7 @@ namespace MediaBrowser.Controller.Net
             }
             catch (Exception ex)
             {
-                Logger.LogError(ex, "Error sending web socket message {Name}", Name);
+                Logger.LogError(ex, "Error sending web socket message {Name}", Type);
                 DisposeConnection(tuple);
             }
         }

+ 2 - 1
MediaBrowser.Controller/Session/ISessionController.cs

@@ -3,6 +3,7 @@
 using System;
 using System.Threading;
 using System.Threading.Tasks;
+using MediaBrowser.Model.Session;
 
 namespace MediaBrowser.Controller.Session
 {
@@ -23,6 +24,6 @@ namespace MediaBrowser.Controller.Session
         /// <summary>
         /// Sends the message.
         /// </summary>
-        Task SendMessage<T>(string name, Guid messageId, T data, CancellationToken cancellationToken);
+        Task SendMessage<T>(SessionMessageType name, Guid messageId, T data, CancellationToken cancellationToken);
     }
 }

+ 4 - 4
MediaBrowser.Controller/Session/ISessionManager.cs

@@ -188,16 +188,16 @@ namespace MediaBrowser.Controller.Session
         /// <param name="data">The data.</param>
         /// <param name="cancellationToken">The cancellation token.</param>
         /// <returns>Task.</returns>
-        Task SendMessageToAdminSessions<T>(string name, T data, CancellationToken cancellationToken);
+        Task SendMessageToAdminSessions<T>(SessionMessageType name, T data, CancellationToken cancellationToken);
 
         /// <summary>
         /// Sends the message to user sessions.
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <returns>Task.</returns>
-        Task SendMessageToUserSessions<T>(List<Guid> userIds, string name, T data, CancellationToken cancellationToken);
+        Task SendMessageToUserSessions<T>(List<Guid> userIds, SessionMessageType name, T data, CancellationToken cancellationToken);
 
-        Task SendMessageToUserSessions<T>(List<Guid> userIds, string name, Func<T> dataFn, CancellationToken cancellationToken);
+        Task SendMessageToUserSessions<T>(List<Guid> userIds, SessionMessageType name, Func<T> dataFn, CancellationToken cancellationToken);
 
         /// <summary>
         /// Sends the message to user device sessions.
@@ -208,7 +208,7 @@ namespace MediaBrowser.Controller.Session
         /// <param name="data">The data.</param>
         /// <param name="cancellationToken">The cancellation token.</param>
         /// <returns>Task.</returns>
-        Task SendMessageToUserDeviceSessions<T>(string deviceId, string name, T data, CancellationToken cancellationToken);
+        Task SendMessageToUserDeviceSessions<T>(string deviceId, SessionMessageType name, T data, CancellationToken cancellationToken);
 
         /// <summary>
         /// Sends the restart required message.

+ 2 - 1
MediaBrowser.Model/Net/WebSocketMessage.cs

@@ -2,6 +2,7 @@
 #pragma warning disable CS1591
 
 using System;
+using MediaBrowser.Model.Session;
 
 namespace MediaBrowser.Model.Net
 {
@@ -15,7 +16,7 @@ namespace MediaBrowser.Model.Net
         /// Gets or sets the type of the message.
         /// </summary>
         /// <value>The type of the message.</value>
-        public string MessageType { get; set; }
+        public SessionMessageType MessageType { get; set; }
 
         public Guid MessageId { get; set; }
 

+ 50 - 0
MediaBrowser.Model/Session/SessionMessageType.cs

@@ -0,0 +1,50 @@
+#pragma warning disable CS1591
+
+namespace MediaBrowser.Model.Session
+{
+    /// <summary>
+    /// The different kinds of messages that are used in the WebSocket api.
+    /// </summary>
+    public enum SessionMessageType
+    {
+        // Server -> Client
+        ForceKeepAlive,
+        GeneralCommand,
+        UserDataChanged,
+        Sessions,
+        Play,
+        SyncPlayCommand,
+        SyncPlayGroupUpdate,
+        PlayState,
+        RestartRequired,
+        ServerShuttingDown,
+        ServerRestarting,
+        LibraryChanged,
+        UserDeleted,
+        UserUpdated,
+        SeriesTimerCreated,
+        TimerCreated,
+        SeriesTimerCancelled,
+        TimerCancelled,
+        RefreshProgress,
+        ScheduledTaskEnded,
+        PackageInstallationCancelled,
+        PackageInstallationFailed,
+        PackageInstallationCompleted,
+        PackageInstalling,
+        PackageUninstalled,
+        ActivityLogEntry,
+        ScheduledTasksInfo,
+
+        // Client -> Server
+        ActivityLogEntryStart,
+        ActivityLogEntryStop,
+        SessionsStart,
+        SessionsStop,
+        ScheduledTasksInfoStart,
+        ScheduledTasksInfoStop,
+
+        // Shared
+        KeepAlive,
+    }
+}