4 ** This plugin upgrades Strophe to support XEP-0295: JSON Encodings for XMPP
8 Strophe
.addConnectionPlugin('jsonstreams', {
9 init: function (conn
) {
11 var parseXMLString = function(xmlStr
) {
13 if (window
.ActiveXObject
) {
14 xmlDoc
= new ActiveXObject("Microsoft.XMLDOM");
16 xmlDoc
.loadXML(xmlStr
);
18 var parser
= new DOMParser();
19 xmlDoc
= parser
.parseFromString(xmlStr
, "text/xml");
24 // replace Strophe.Request._newXHR with new jsonstreams version
25 // if JSON is detected
27 var _newXHR
= Strophe
.Request
.prototype._newXHR
;
28 Strophe
.Request
.prototype._newXHR = function () {
29 var _xhr
= _newXHR
.apply(this, arguments
);
35 open: function(a
, b
, c
) { return _xhr
.open(a
, b
, c
) },
36 abort: function() { _xhr
.abort(); },
37 send: function(data
) {
38 data
= JSON
.stringify({"s":data
});
39 return _xhr
.send(data
);
43 xhr
.onreadystatechange
= this.func
.bind(null, this);
44 _xhr
.onreadystatechange = function() {
45 xhr
.readyState
= _xhr
.readyState
;
46 if (xhr
.readyState
!= 4) {
48 xhr
.responseText
= "";
49 xhr
.responseXML
= null;
51 xhr
.status
= _xhr
.status
;
52 xhr
.responseText
= _xhr
.responseText
;
53 xhr
.responseXML
= _xhr
.responseXML
;
54 if (_xhr
.responseText
&& !(_xhr
.responseXML
55 && _xhr
.responseXML
.documentElement
56 && _xhr
.responseXML
.documentElement
.tagName
!= "parsererror")) {
57 var data
= JSON
.parse(_xhr
.responseText
);
59 xhr
.responseText
= data
.s
;
60 xhr
.responseXML
= parseXMLString(data
.s
);
64 if ("function" == typeof xhr
.onreadystatechange
) { xhr
.onreadystatechange(req
); }
69 Strophe
.error("jsonstreams plugin loaded, but JSON not found." +
70 " Falling back to native XHR implementation.");