test html uplevel detect browser and MozWebSocket
authorAndy Green <andy@warmcat.com>
Sun, 25 Sep 2011 09:01:02 +0000 (10:01 +0100)
committerAndy Green <andy@warmcat.com>
Sun, 25 Sep 2011 09:01:02 +0000 (10:01 +0100)
Signed-off-by: Andy Green <andy@warmcat.com>
test-server/test.html

index 5f030f3..86c7eb3 100644 (file)
@@ -6,6 +6,7 @@
 </head>
 
 <body>
+<h3>Detected Browser: <div id=brow>...</div></h3>
 <h2>libwebsockets "dumb-increment-protocol" test applet</h2>
 The incrementing number is coming from the server and is individual for
 each connection to the server... try opening a second browser window.
@@ -50,6 +51,130 @@ run.</p>
 </table>
 
 <script>
+
+/* BrowserDetect came from http://www.quirksmode.org/js/detect.html */
+
+var BrowserDetect = {
+       init: function () {
+               this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
+               this.version = this.searchVersion(navigator.userAgent)
+                       || this.searchVersion(navigator.appVersion)
+                       || "an unknown version";
+               this.OS = this.searchString(this.dataOS) || "an unknown OS";
+       },
+       searchString: function (data) {
+               for (var i=0;i<data.length;i++) {
+                       var dataString = data[i].string;
+                       var dataProp = data[i].prop;
+                       this.versionSearchString = data[i].versionSearch || data[i].identity;
+                       if (dataString) {
+                               if (dataString.indexOf(data[i].subString) != -1)
+                                       return data[i].identity;
+                       }
+                       else if (dataProp)
+                               return data[i].identity;
+               }
+       },
+       searchVersion: function (dataString) {
+               var index = dataString.indexOf(this.versionSearchString);
+               if (index == -1) return;
+               return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
+       },
+       dataBrowser: [
+               {
+                       string: navigator.userAgent,
+                       subString: "Chrome",
+                       identity: "Chrome"
+               },
+               {       string: navigator.userAgent,
+                       subString: "OmniWeb",
+                       versionSearch: "OmniWeb/",
+                       identity: "OmniWeb"
+               },
+               {
+                       string: navigator.vendor,
+                       subString: "Apple",
+                       identity: "Safari",
+                       versionSearch: "Version"
+               },
+               {
+                       prop: window.opera,
+                       identity: "Opera",
+                       versionSearch: "Version"
+               },
+               {
+                       string: navigator.vendor,
+                       subString: "iCab",
+                       identity: "iCab"
+               },
+               {
+                       string: navigator.vendor,
+                       subString: "KDE",
+                       identity: "Konqueror"
+               },
+               {
+                       string: navigator.userAgent,
+                       subString: "Firefox",
+                       identity: "Firefox"
+               },
+               {
+                       string: navigator.vendor,
+                       subString: "Camino",
+                       identity: "Camino"
+               },
+               {               // for newer Netscapes (6+)
+                       string: navigator.userAgent,
+                       subString: "Netscape",
+                       identity: "Netscape"
+               },
+               {
+                       string: navigator.userAgent,
+                       subString: "MSIE",
+                       identity: "Explorer",
+                       versionSearch: "MSIE"
+               },
+               {
+                       string: navigator.userAgent,
+                       subString: "Gecko",
+                       identity: "Mozilla",
+                       versionSearch: "rv"
+               },
+               {               // for older Netscapes (4-)
+                       string: navigator.userAgent,
+                       subString: "Mozilla",
+                       identity: "Netscape",
+                       versionSearch: "Mozilla"
+               }
+       ],
+       dataOS : [
+               {
+                       string: navigator.platform,
+                       subString: "Win",
+                       identity: "Windows"
+               },
+               {
+                       string: navigator.platform,
+                       subString: "Mac",
+                       identity: "Mac"
+               },
+               {
+                          string: navigator.userAgent,
+                          subString: "iPhone",
+                          identity: "iPhone/iPod"
+           },
+               {
+                       string: navigator.platform,
+                       subString: "Linux",
+                       identity: "Linux"
+               }
+       ]
+
+};
+BrowserDetect.init();
+
+document.getElementById("brow").textContent = " " + BrowserDetect.browser + " "
+       + BrowserDetect.version +" " + BrowserDetect.OS +" ";
+
        var pos = 0;
 
 function get_appropriate_ws_url()
@@ -81,8 +206,16 @@ document.getElementById("number").textContent = get_appropriate_ws_url();
 
 /* dumb increment protocol */
        
-       var socket_di = new WebSocket(get_appropriate_ws_url(),
-                                  "dumb-increment-protocol");  
+       var socket_di;
+
+       if (BrowserDetect.browser == "Firefox") {
+               socket_di = new MozWebSocket(get_appropriate_ws_url(),
+                                  "dumb-increment-protocol");
+       } else {
+               socket_di = new WebSocket(get_appropriate_ws_url(),
+                                  "dumb-increment-protocol");
+       }
+
 
        try {
                socket_di.onopen = function() {
@@ -113,10 +246,18 @@ function reset() {
        var no_last = 1;
        var last_x = 0, last_y = 0;
        var ctx;
-       var socket_lm = new WebSocket(get_appropriate_ws_url(),
-                                  "lws-mirror-protocol");
+       var socket_lm;
        var color = "#000000";
 
+       if (BrowserDetect.browser == "Firefox") {
+               socket_lm = new MozWebSocket(get_appropriate_ws_url(),
+                                  "lws-mirror-protocol");
+       } else {
+               socket_lm = new WebSocket(get_appropriate_ws_url(),
+                                  "lws-mirror-protocol");
+       }
+
+
        try {
                socket_lm.onopen = function() {
                        document.getElementById("wslm_statustd").style.backgroundColor = "#40ff40";