changeset 351:85d3c04c64f6

mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
author Waqas Hussain <waqas20@gmail.com>
date Sat, 02 Apr 2011 03:11:22 +0500
parents 98569ec25ac2
children 0b4fe47e648d
files mod_json_streams/strophe.jsonstreams.js
diffstat 1 files changed, 70 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_json_streams/strophe.jsonstreams.js	Sat Apr 02 03:11:22 2011 +0500
@@ -0,0 +1,70 @@
+
+/* jsonstreams plugin
+**
+** This plugin upgrades Strophe to support XEP-0295: JSON Encodings for XMPP
+**
+*/
+
+Strophe.addConnectionPlugin('jsonstreams', {
+    init: function (conn) {
+
+        var parseXMLString = function(xmlStr) {
+			var xmlDoc = null;
+			if (window.ActiveXObject) {
+				xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
+				xmlDoc.async=false;
+				xmlDoc.loadXML(xmlStr);
+			} else {
+				var parser = new DOMParser();
+				xmlDoc = parser.parseFromString(xmlStr, "text/xml");
+			}
+			return xmlDoc;
+        }
+
+        // replace Strophe.Request._newXHR with new jsonstreams version
+        // if JSON is detected
+        if (window.JSON) {
+        	var _newXHR = Strophe.Request.prototype._newXHR;
+            Strophe.Request.prototype._newXHR = function () {
+            	var _xhr = _newXHR.apply(this, arguments);
+                var xhr = {
+                	readyState: 0,
+                	responseText: null,
+                	responseXML: null,
+                	status: null,
+                	open: function(a, b, c) { return _xhr.open(a, b, c) },
+                	abort: function() { _xhr.abort(); },
+                	send: function(data) {
+                		data = JSON.stringify({"s":data});
+                		return _xhr.send(data);
+                	}
+                };
+                xhr.onreadystatechange = _xhr.onreadystatechange;
+                _xhr.onreadystatechange = function() {
+                	xhr.readyState = _xhr.readyState;
+                	if (xhr.readyState != 4) {
+                		xhr.status = 0;
+                		xhr.responseText = "";
+                		xhr.responseXML = null;
+                	} else {
+	                	xhr.status = _xhr.status;
+	               		xhr.responseText = _xhr.responseText;
+	               		xhr.responseXML = _xhr.responseXML;
+	                	if (_xhr.responseText && !_xhr.responseXML) {
+	                		var data = JSON.parse(_xhr.responseText);
+	                		if (data && data.s) {
+	                			xhr.responseText = data.s;
+	                			xhr.responseXML = parseXMLString(data.s);
+	                		}
+	                	}
+	                }
+                	if (xhr.onreadystatechange) { xhr.onreadystatechange(); }
+                }
+                return xhr;
+            };
+        } else {
+            Strophe.error("jsonstreams plugin loaded, but JSON not found." +
+                          "  Falling back to native XHR implementation.");
+        }
+    }
+});