view mod_json_streams/strophe.jsonstreams.js @ 3656:3e0f4d727825

mod_vcard_muc: Add an alternative method of signaling avatar change When the avatar has been changed, a signal is sent that the room configuration has changed. Clients then do a disco#info query to find the SHA-1 of the new avatar. They can then fetch it as before, or not if they have it cached already. This is meant to be less disruptive than signaling via presence, which caused problems for some clients. If clients transition to the new method, the old one can eventually be removed. The namespace is made up while waiting for standardization. Otherwise it is very close to what's described in https://xmpp.org/extensions/inbox/muc-avatars.html
author Kim Alvefur <zash@zash.se>
date Sun, 25 Aug 2019 20:46:43 +0200
parents 7dbde05b48a9
children
line wrap: on
line source


/* jsonstreams plugin
**
** This plugin upgrades Strophe to support XEP-0295: JSON Encodings for XMPP
**
*/

Strophe.addConnectionPlugin('jsonstreams', {
    init: function (conn) {

        var parseXMLString = function(xmlStr) {
			var xmlDoc = null;
			if (window.ActiveXObject) {
				xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.async=false;
				xmlDoc.loadXML(xmlStr);
			} else {
				var parser = new DOMParser();
				xmlDoc = parser.parseFromString(xmlStr, "text/xml");
			}
			return xmlDoc;
        }

        // replace Strophe.Request._newXHR with new jsonstreams version
        // if JSON is detected
        if (window.JSON) {
        	var _newXHR = Strophe.Request.prototype._newXHR;
            Strophe.Request.prototype._newXHR = function () {
            	var _xhr = _newXHR.apply(this, arguments);
                var xhr = {
                	readyState: 0,
                	responseText: null,
                	responseXML: null,
                	status: null,
                	open: function(a, b, c) { return _xhr.open(a, b, c) },
                	abort: function() { _xhr.abort(); },
                	send: function(data) {
                		data = JSON.stringify({"s":data});
                		return _xhr.send(data);
                	}
                };
                var req = this;
                xhr.onreadystatechange = this.func.bind(null, this);
                _xhr.onreadystatechange = function() {
                	xhr.readyState = _xhr.readyState;
                	if (xhr.readyState != 4) {
                		xhr.status = 0;
                		xhr.responseText = "";
                		xhr.responseXML = null;
                	} else {
	                	xhr.status = _xhr.status;
	               		xhr.responseText = _xhr.responseText;
	               		xhr.responseXML = _xhr.responseXML;
	                	if (_xhr.responseText && !(_xhr.responseXML
	                			&& _xhr.responseXML.documentElement
	                			&& _xhr.responseXML.documentElement.tagName != "parsererror")) {
	                		var data = JSON.parse(_xhr.responseText);
	                		if (data && data.s) {
	                			xhr.responseText = data.s;
	                			xhr.responseXML = parseXMLString(data.s);
	                		}
	                	}
	                }
                	if ("function" == typeof xhr.onreadystatechange) { xhr.onreadystatechange(req); }
                }
                return xhr;
            };
        } else {
            Strophe.error("jsonstreams plugin loaded, but JSON not found." +
                          "  Falling back to native XHR implementation.");
        }
    }
});