From a6c9fdd0f26201a49dd9d2d3365f713104230eaa Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 25 Mar 2013 23:01:47 -0400 Subject: [PATCH] Moved some web socket classes to the model --- .../MediaBrowser.Common.csproj | 2 - .../Net/BasePeriodicWebSocketListener.cs | 1 + MediaBrowser.Common/Net/IWebSocket.cs | 1 + .../Net/IWebSocketConnection.cs | 1 + MediaBrowser.Model/MediaBrowser.Model.csproj | 2 + .../Net/WebSocketMessageType.cs | 4 +- .../Net/WebSocketState.cs | 2 +- .../HttpServer/NativeWebSocket.cs | 6 +- .../ServerManager/ServerManager.cs | 1 + .../ServerManager/WebSocketConnection.cs | 1 + .../WebSocket/AlchemyWebSocket.cs | 1 + MediaBrowser.WebDashboard/ApiClient.js | 136 ++++++++++-------- MediaBrowser.WebDashboard/packages.config | 2 +- 13 files changed, 88 insertions(+), 72 deletions(-) rename {MediaBrowser.Common => MediaBrowser.Model}/Net/WebSocketMessageType.cs (87%) rename {MediaBrowser.Common => MediaBrowser.Model}/Net/WebSocketState.cs (95%) diff --git a/MediaBrowser.Common/MediaBrowser.Common.csproj b/MediaBrowser.Common/MediaBrowser.Common.csproj index 76bf63e3a..bdef529a5 100644 --- a/MediaBrowser.Common/MediaBrowser.Common.csproj +++ b/MediaBrowser.Common/MediaBrowser.Common.csproj @@ -87,8 +87,6 @@ - - diff --git a/MediaBrowser.Common/Net/BasePeriodicWebSocketListener.cs b/MediaBrowser.Common/Net/BasePeriodicWebSocketListener.cs index de5071a53..a8e8d7a8c 100644 --- a/MediaBrowser.Common/Net/BasePeriodicWebSocketListener.cs +++ b/MediaBrowser.Common/Net/BasePeriodicWebSocketListener.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; +using MediaBrowser.Model.Net; namespace MediaBrowser.Common.Net { diff --git a/MediaBrowser.Common/Net/IWebSocket.cs b/MediaBrowser.Common/Net/IWebSocket.cs index 9c9bcb41d..805340b90 100644 --- a/MediaBrowser.Common/Net/IWebSocket.cs +++ b/MediaBrowser.Common/Net/IWebSocket.cs @@ -1,6 +1,7 @@ using System; using System.Threading; using System.Threading.Tasks; +using MediaBrowser.Model.Net; namespace MediaBrowser.Common.Net { diff --git a/MediaBrowser.Common/Net/IWebSocketConnection.cs b/MediaBrowser.Common/Net/IWebSocketConnection.cs index 8e1b13ceb..c09d5ba2b 100644 --- a/MediaBrowser.Common/Net/IWebSocketConnection.cs +++ b/MediaBrowser.Common/Net/IWebSocketConnection.cs @@ -1,6 +1,7 @@ using System; using System.Threading; using System.Threading.Tasks; +using MediaBrowser.Model.Net; namespace MediaBrowser.Common.Net { diff --git a/MediaBrowser.Model/MediaBrowser.Model.csproj b/MediaBrowser.Model/MediaBrowser.Model.csproj index a3bc341c3..ae6e21923 100644 --- a/MediaBrowser.Model/MediaBrowser.Model.csproj +++ b/MediaBrowser.Model/MediaBrowser.Model.csproj @@ -46,6 +46,8 @@ + + diff --git a/MediaBrowser.Common/Net/WebSocketMessageType.cs b/MediaBrowser.Model/Net/WebSocketMessageType.cs similarity index 87% rename from MediaBrowser.Common/Net/WebSocketMessageType.cs rename to MediaBrowser.Model/Net/WebSocketMessageType.cs index 3b12bc2b2..37ae7fc55 100644 --- a/MediaBrowser.Common/Net/WebSocketMessageType.cs +++ b/MediaBrowser.Model/Net/WebSocketMessageType.cs @@ -1,5 +1,5 @@  -namespace MediaBrowser.Common.Net +namespace MediaBrowser.Model.Net { /// /// Enum WebSocketMessageType @@ -17,6 +17,6 @@ namespace MediaBrowser.Common.Net /// /// The close /// - Close, + Close } } diff --git a/MediaBrowser.Common/Net/WebSocketState.cs b/MediaBrowser.Model/Net/WebSocketState.cs similarity index 95% rename from MediaBrowser.Common/Net/WebSocketState.cs rename to MediaBrowser.Model/Net/WebSocketState.cs index 7f8ac3cbe..41a2e9741 100644 --- a/MediaBrowser.Common/Net/WebSocketState.cs +++ b/MediaBrowser.Model/Net/WebSocketState.cs @@ -1,5 +1,5 @@  -namespace MediaBrowser.Common.Net +namespace MediaBrowser.Model.Net { /// /// Enum WebSocketState diff --git a/MediaBrowser.Server.Implementations/HttpServer/NativeWebSocket.cs b/MediaBrowser.Server.Implementations/HttpServer/NativeWebSocket.cs index 9f5d14eaf..9ad617847 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/NativeWebSocket.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/NativeWebSocket.cs @@ -4,8 +4,8 @@ using System; using System.Net.WebSockets; using System.Threading; using System.Threading.Tasks; -using WebSocketMessageType = MediaBrowser.Common.Net.WebSocketMessageType; -using WebSocketState = MediaBrowser.Common.Net.WebSocketState; +using WebSocketMessageType = MediaBrowser.Model.Net.WebSocketMessageType; +using WebSocketState = MediaBrowser.Model.Net.WebSocketState; namespace MediaBrowser.Server.Implementations.HttpServer { @@ -83,7 +83,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer } catch (WebSocketException ex) { - _logger.ErrorException("Error reveiving web socket message", ex); + _logger.ErrorException("Error receiving web socket message", ex); break; } diff --git a/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs b/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs index cba74ffbf..c1453cd8b 100644 --- a/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs +++ b/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs @@ -3,6 +3,7 @@ using MediaBrowser.Common.Net; using MediaBrowser.Controller; using MediaBrowser.Controller.Configuration; using MediaBrowser.Model.Logging; +using MediaBrowser.Model.Net; using MediaBrowser.Model.Serialization; using System; using System.Collections.Generic; diff --git a/MediaBrowser.Server.Implementations/ServerManager/WebSocketConnection.cs b/MediaBrowser.Server.Implementations/ServerManager/WebSocketConnection.cs index d92c60773..5a074d194 100644 --- a/MediaBrowser.Server.Implementations/ServerManager/WebSocketConnection.cs +++ b/MediaBrowser.Server.Implementations/ServerManager/WebSocketConnection.cs @@ -1,5 +1,6 @@ using MediaBrowser.Common.Net; using MediaBrowser.Model.Logging; +using MediaBrowser.Model.Net; using MediaBrowser.Model.Serialization; using System; using System.IO; diff --git a/MediaBrowser.Server.Implementations/WebSocket/AlchemyWebSocket.cs b/MediaBrowser.Server.Implementations/WebSocket/AlchemyWebSocket.cs index 13a9ce050..7bf1fe86d 100644 --- a/MediaBrowser.Server.Implementations/WebSocket/AlchemyWebSocket.cs +++ b/MediaBrowser.Server.Implementations/WebSocket/AlchemyWebSocket.cs @@ -5,6 +5,7 @@ using System; using System.Text; using System.Threading; using System.Threading.Tasks; +using MediaBrowser.Model.Net; namespace MediaBrowser.Server.Implementations.WebSocket { diff --git a/MediaBrowser.WebDashboard/ApiClient.js b/MediaBrowser.WebDashboard/ApiClient.js index 94dc72d54..5381a7abd 100644 --- a/MediaBrowser.WebDashboard/ApiClient.js +++ b/MediaBrowser.WebDashboard/ApiClient.js @@ -1,54 +1,4 @@ -(function (jQuery, window, undefined) { - "use strict"; - - var matched, browser; - - jQuery.uaMatch = function (ua) { - ua = ua.toLowerCase(); - - var match = /(chrome)[ \/]([\w.]+)/.exec(ua) || - /(webkit)[ \/]([\w.]+)/.exec(ua) || - /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) || - /(msie) ([\w.]+)/.exec(ua) || - ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) || - []; - - var platform_match = /(ipad)/.exec(ua) || - /(iphone)/.exec(ua) || - /(android)/.exec(ua) || - []; - - return { - browser: match[1] || "", - version: match[2] || "0", - platform: platform_match[0] || "" - }; - }; - - matched = jQuery.uaMatch(window.navigator.userAgent); - browser = {}; - - if (matched.browser) { - browser[matched.browser] = true; - browser.version = matched.version; - } - - if (matched.platform) { - browser[matched.platform] = true - } - - // Chrome is Webkit, but Webkit is also Safari. - if (browser.chrome) { - browser.webkit = true; - } else if (browser.webkit) { - browser.safari = true; - } - - jQuery.browser = browser; - -})(jQuery, window); - -if (!window.MediaBrowser) { +if (!window.MediaBrowser) { window.MediaBrowser = {}; } @@ -63,6 +13,16 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) { */ return function (serverProtocol, serverHostName, serverPortNumber, clientName) { + if (!serverProtocol) { + throw new Error("Must supply a serverProtocol, e.g. http:"); + } + if (!serverHostName) { + throw new Error("Must supply serverHostName, e.g. 192.168.1.1 or myServerName"); + } + if (!serverPortNumber) { + throw new Error("Must supply a serverPortNumber"); + } + var self = this; var deviceName = "Web Browser"; var deviceId = MediaBrowser.SHA1(navigator.userAgent + (navigator.cpuClass || "")); @@ -113,10 +73,7 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) { else if ($.browser.msie) { name = "Internet Explorer"; } - else if ($.browser.firefox) { - name = "Firefox"; - } - else if ($.browser.mozilla) { + else if ($.browser.firefox || $.browser.mozilla) { name = "Firefox"; } else if ($.browser.opera) { @@ -147,16 +104,19 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) { throw new Error("Request cannot be null"); } - var auth = 'MediaBrowser Client="' + clientName + '", Device="' + deviceName + '", DeviceId="' + deviceId + '"'; + if (clientName) { - if (currentUserId) { - auth += ', UserId="' + currentUserId + '"'; + var auth = 'MediaBrowser Client="' + clientName + '", Device="' + deviceName + '", DeviceId="' + deviceId + '"'; + + if (currentUserId) { + auth += ', UserId="' + currentUserId + '"'; + } + + request.headers = { + Authorization: auth + }; } - request.headers = { - Authorization: auth - }; - return $.ajax(request); }; @@ -1933,4 +1893,54 @@ MediaBrowser.SHA1 = function (msg) { var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4); return temp.toLowerCase(); -}; \ No newline at end of file +}; + +(function (jQuery, window, undefined) { + "use strict"; + + var matched, browser; + + jQuery.uaMatch = function (ua) { + ua = ua.toLowerCase(); + + var match = /(chrome)[ \/]([\w.]+)/.exec(ua) || + /(webkit)[ \/]([\w.]+)/.exec(ua) || + /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) || + /(msie) ([\w.]+)/.exec(ua) || + ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) || + []; + + var platform_match = /(ipad)/.exec(ua) || + /(iphone)/.exec(ua) || + /(android)/.exec(ua) || + []; + + return { + browser: match[1] || "", + version: match[2] || "0", + platform: platform_match[0] || "" + }; + }; + + matched = jQuery.uaMatch(window.navigator.userAgent); + browser = {}; + + if (matched.browser) { + browser[matched.browser] = true; + browser.version = matched.version; + } + + if (matched.platform) { + browser[matched.platform] = true + } + + // Chrome is Webkit, but Webkit is also Safari. + if (browser.chrome) { + browser.webkit = true; + } else if (browser.webkit) { + browser.safari = true; + } + + jQuery.browser = browser; + +})(jQuery, window); \ No newline at end of file diff --git a/MediaBrowser.WebDashboard/packages.config b/MediaBrowser.WebDashboard/packages.config index cd8da2732..5599c176d 100644 --- a/MediaBrowser.WebDashboard/packages.config +++ b/MediaBrowser.WebDashboard/packages.config @@ -1,6 +1,6 @@  - + \ No newline at end of file