Annotate

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
child 352:0b4fe47e648d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
351
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 /* jsonstreams plugin
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 **
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 ** This plugin upgrades Strophe to support XEP-0295: JSON Encodings for XMPP
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 **
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 */
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8 Strophe.addConnectionPlugin('jsonstreams', {
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 init: function (conn) {
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 var parseXMLString = function(xmlStr) {
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 var xmlDoc = null;
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 if (window.ActiveXObject) {
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 xmlDoc.async=false;
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 xmlDoc.loadXML(xmlStr);
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 } else {
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 var parser = new DOMParser();
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 xmlDoc = parser.parseFromString(xmlStr, "text/xml");
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 }
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 return xmlDoc;
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 }
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 // replace Strophe.Request._newXHR with new jsonstreams version
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 // if JSON is detected
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 if (window.JSON) {
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 var _newXHR = Strophe.Request.prototype._newXHR;
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 Strophe.Request.prototype._newXHR = function () {
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 var _xhr = _newXHR.apply(this, arguments);
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 var xhr = {
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 readyState: 0,
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 responseText: null,
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 responseXML: null,
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 status: null,
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 open: function(a, b, c) { return _xhr.open(a, b, c) },
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 abort: function() { _xhr.abort(); },
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 send: function(data) {
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 data = JSON.stringify({"s":data});
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 return _xhr.send(data);
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 }
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 };
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 xhr.onreadystatechange = _xhr.onreadystatechange;
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 _xhr.onreadystatechange = function() {
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 xhr.readyState = _xhr.readyState;
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 if (xhr.readyState != 4) {
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 xhr.status = 0;
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 xhr.responseText = "";
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 xhr.responseXML = null;
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 } else {
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 xhr.status = _xhr.status;
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 xhr.responseText = _xhr.responseText;
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 xhr.responseXML = _xhr.responseXML;
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 if (_xhr.responseText && !_xhr.responseXML) {
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 var data = JSON.parse(_xhr.responseText);
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 if (data && data.s) {
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 xhr.responseText = data.s;
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 xhr.responseXML = parseXMLString(data.s);
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 }
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 }
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 }
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 if (xhr.onreadystatechange) { xhr.onreadystatechange(); }
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 }
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 return xhr;
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 };
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 } else {
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 Strophe.error("jsonstreams plugin loaded, but JSON not found." +
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 " Falling back to native XHR implementation.");
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 }
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 }
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 });