Mercurial > prosody-modules
comparison mod_json_streams/strophe.jsonstreams.js @ 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 | |
children | 0b4fe47e648d |
comparison
equal
deleted
inserted
replaced
350:98569ec25ac2 | 351:85d3c04c64f6 |
---|---|
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 xhr.onreadystatechange = _xhr.onreadystatechange; | |
43 _xhr.onreadystatechange = function() { | |
44 xhr.readyState = _xhr.readyState; | |
45 if (xhr.readyState != 4) { | |
46 xhr.status = 0; | |
47 xhr.responseText = ""; | |
48 xhr.responseXML = null; | |
49 } else { | |
50 xhr.status = _xhr.status; | |
51 xhr.responseText = _xhr.responseText; | |
52 xhr.responseXML = _xhr.responseXML; | |
53 if (_xhr.responseText && !_xhr.responseXML) { | |
54 var data = JSON.parse(_xhr.responseText); | |
55 if (data && data.s) { | |
56 xhr.responseText = data.s; | |
57 xhr.responseXML = parseXMLString(data.s); | |
58 } | |
59 } | |
60 } | |
61 if (xhr.onreadystatechange) { xhr.onreadystatechange(); } | |
62 } | |
63 return xhr; | |
64 }; | |
65 } else { | |
66 Strophe.error("jsonstreams plugin loaded, but JSON not found." + | |
67 " Falling back to native XHR implementation."); | |
68 } | |
69 } | |
70 }); |