Software /
code /
prosody-modules
Comparison
mod_json_streams/strophe.jsonstreams.js @ 353:8ef36af30181
merge with upstream
author | Phil Stewart <phil.stewart@lichp.co.uk> |
---|---|
date | Sun, 03 Apr 2011 22:49:36 +0100 |
parent | 352:0b4fe47e648d |
child | 1343:7dbde05b48a9 |
comparison
equal
deleted
inserted
replaced
347:cd838419a85d | 353:8ef36af30181 |
---|---|
1 | |
2 /* jsonstreams plugin | |
3 ** | |
4 ** This plugin upgrades Strophe to support XEP-0295: JSON Encodings for XMPP | |
5 ** | |
6 */ | |
7 | |
8 Strophe.addConnectionPlugin('jsonstreams', { | |
9 init: function (conn) { | |
10 | |
11 var parseXMLString = function(xmlStr) { | |
12 var xmlDoc = null; | |
13 if (window.ActiveXObject) { | |
14 xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); | |
15 xmlDoc.async=false; | |
16 xmlDoc.loadXML(xmlStr); | |
17 } else { | |
18 var parser = new DOMParser(); | |
19 xmlDoc = parser.parseFromString(xmlStr, "text/xml"); | |
20 } | |
21 return xmlDoc; | |
22 } | |
23 | |
24 // replace Strophe.Request._newXHR with new jsonstreams version | |
25 // if JSON is detected | |
26 if (window.JSON) { | |
27 var _newXHR = Strophe.Request.prototype._newXHR; | |
28 Strophe.Request.prototype._newXHR = function () { | |
29 var _xhr = _newXHR.apply(this, arguments); | |
30 var xhr = { | |
31 readyState: 0, | |
32 responseText: null, | |
33 responseXML: null, | |
34 status: null, | |
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); | |
40 } | |
41 }; | |
42 var req = this; | |
43 xhr.onreadystatechange = this.func.bind(null, this); | |
44 _xhr.onreadystatechange = function() { | |
45 xhr.readyState = _xhr.readyState; | |
46 if (xhr.readyState != 4) { | |
47 xhr.status = 0; | |
48 xhr.responseText = ""; | |
49 xhr.responseXML = null; | |
50 } else { | |
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); | |
58 if (data && data.s) { | |
59 xhr.responseText = data.s; | |
60 xhr.responseXML = parseXMLString(data.s); | |
61 } | |
62 } | |
63 } | |
64 if ("function" == typeof xhr.onreadystatechange) { xhr.onreadystatechange(req); } | |
65 } | |
66 return xhr; | |
67 }; | |
68 } else { | |
69 Strophe.error("jsonstreams plugin loaded, but JSON not found." + | |
70 " Falling back to native XHR implementation."); | |
71 } | |
72 } | |
73 }); |