Annotate

mod_json_streams/strophe.jsonstreams.js @ 2968:569b98d6fca1

mod_http_logging: Be robust against missing connection object
author Kim Alvefur <zash@zash.se>
date Fri, 30 Mar 2018 13:37:39 +0200
parent 1343:7dbde05b48a9
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) {
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 352
diff changeset
14 xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
351
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 };
352
0b4fe47e648d mod_json_streams/strophe.jsonstreams.js: Fix some compatability issues.
Waqas Hussain <waqas20@gmail.com>
parents: 351
diff changeset
42 var req = this;
0b4fe47e648d mod_json_streams/strophe.jsonstreams.js: Fix some compatability issues.
Waqas Hussain <waqas20@gmail.com>
parents: 351
diff changeset
43 xhr.onreadystatechange = this.func.bind(null, this);
351
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.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
45 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
46 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
47 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
48 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
49 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
50 } else {
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.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
52 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
53 xhr.responseXML = _xhr.responseXML;
352
0b4fe47e648d mod_json_streams/strophe.jsonstreams.js: Fix some compatability issues.
Waqas Hussain <waqas20@gmail.com>
parents: 351
diff changeset
54 if (_xhr.responseText && !(_xhr.responseXML
0b4fe47e648d mod_json_streams/strophe.jsonstreams.js: Fix some compatability issues.
Waqas Hussain <waqas20@gmail.com>
parents: 351
diff changeset
55 && _xhr.responseXML.documentElement
0b4fe47e648d mod_json_streams/strophe.jsonstreams.js: Fix some compatability issues.
Waqas Hussain <waqas20@gmail.com>
parents: 351
diff changeset
56 && _xhr.responseXML.documentElement.tagName != "parsererror")) {
351
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 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
58 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
59 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
60 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
61 }
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 }
352
0b4fe47e648d mod_json_streams/strophe.jsonstreams.js: Fix some compatability issues.
Waqas Hussain <waqas20@gmail.com>
parents: 351
diff changeset
64 if ("function" == typeof xhr.onreadystatechange) { xhr.onreadystatechange(req); }
351
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 }
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 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
67 };
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 } else {
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 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
70 " 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
71 }
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 }
85d3c04c64f6 mod_json_streams/strophe.jsonstreams.js: A strophe.js plugin to make it work.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 });