diff --git a/ApiClient.js b/ApiClient.js index 66bf342488..7db97e6603 100644 --- a/ApiClient.js +++ b/ApiClient.js @@ -27,14 +27,6 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout, wi var currentUserId; var webSocket; - $(window).on("beforeunload", function () { - - // Close the connection gracefully when possible - if (webSocket && webSocket.readyState === WebSocket.OPEN) { - webSocket.close(); - } - }); - /** * Gets the server host name. */ @@ -195,6 +187,12 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout, wi }; }; + self.closeWebSocket = function () { + if (webSocket && webSocket.readyState === WebSocket.OPEN) { + webSocket.close(); + } + }; + self.sendWebSocketMessage = function (name, data) { var msg = { MessageType: name }; @@ -3427,17 +3425,15 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout, wi throw new Error("null itemId"); } - // Always use the http api in case playback is stopped by closing the browser - // See window beforeunload event at the top of this file - //if (self.isWebSocketOpen()) { + if (self.isWebSocketOpen()) { - // var deferred = $.Deferred(); + var deferred = $.Deferred(); - // self.sendWebSocketMessage("PlaybackStopped", itemId + "|" + (positionTicks == null ? "" : positionTicks)); + self.sendWebSocketMessage("PlaybackStopped", itemId + "|" + (positionTicks == null ? "" : positionTicks)); - // deferred.resolveWith(null, []); - // return deferred.promise(); - //} + deferred.resolveWith(null, []); + return deferred.promise(); + } var params = { }; diff --git a/dashboard-ui/scripts/site.js b/dashboard-ui/scripts/site.js index 806cf6d465..6f8c04b9c1 100644 --- a/dashboard-ui/scripts/site.js +++ b/dashboard-ui/scripts/site.js @@ -1167,6 +1167,14 @@ $(function () { if (!IsStorageEnabled()) { alert("This browser does not support local storage or is running in private mode. For a better experience, try a newer browser such as Chrome (android, desktop), Firefox, IE10, Safari (iOS) or Opera."); } + + $(window).on("beforeunload", function () { + + // Close the connection gracefully when possible + if (ApiClient.isWebSocketOpen() && !MediaPlayer.isPlaying()) { + ApiClient.closeWebSocket(); + } + }); }); Dashboard.jQueryMobileInit(); @@ -1208,4 +1216,5 @@ $(document).on('pagebeforeshow', ".page", function () { Dashboard.refreshSystemInfoFromServer(); } -}); \ No newline at end of file +}); + diff --git a/packages.config b/packages.config index 21812f626b..b3add5674e 100644 --- a/packages.config +++ b/packages.config @@ -1,6 +1,6 @@  - + \ No newline at end of file