X-Git-Url: http://git.euphorik.ch/?p=euphorik.git;a=blobdiff_plain;f=js%2Fcommunication.js;h=985aeedce59bc4862a09392b5173a38f9855b495;hp=db9beb887fd7d09b4079293376c00ca0ea02d775;hb=85dc0facbc2b2de826978fac3768db7949a6b92f;hpb=972919ab7f5651cd721eb6eec75f7150fdeaf347 diff --git a/js/communication.js b/js/communication.js index db9beb8..985aeed 100644 --- a/js/communication.js +++ b/js/communication.js @@ -16,47 +16,101 @@ // You should have received a copy of the GNU General Public License // along with Euphorik. If not, see . -// regroupe la partie communication client -> sevreur de euphorik. +// Regroupe la partie communication JSON client -> serveur de euphorik. +// Voir : http://dev.euphorik.ch/wiki/euk/Protocole -/** - * @param funError un fonction executé lors d'un réponse 'error' de la part du serveur, peut être redéfinit pour une requête. +/** + * Les fonctions debutReq et finReq servent, par exemple, à afficher à l'utilisateur + * qu'une communication est en cours. + * @param funError un fonction executée lors d'un réponse 'error' de la part du serveur, peut être redéfinit pour une requête. + * @param funDebutReq fonction appelée au début d'une requête (facultatif) + * @param funFinReq fonction appelée à la fin d'une requête (facultatif) */ -euphorik.Communication = function(util, funError) { -} - -euphorik.Communication.prototype.getBase = function(action) { - this.base = { - "header" : { "action" : action, "version" : euphorik.conf.versionProtocole } - }; -} - -euphorik.Communication.prototype.requete = function(action, json, funOk, funError, asynchrone) { +euphorik.Communication = function(funError, funDebutReq, funFinReq) { + this.funError = funError; + this.funDebutReq = funDebutReq; + this.funFinReq = funFinReq; +}; + +/** + * Charge un fichier depuis une url et retourne son contenu. + */ +euphorik.Communication.prototype.load = function(url) { + if (this.funDebutReq) { + this.funDebutReq(); + } + var contenu = ""; + $.ajax({async: false, url: url, success : function(page) { contenu += page; }}); + if (this.funFinReq) { + this.funFinReq(); + } + return contenu; +}; + +/** + * Effectue une requête JSON auprès du serveur. + * @param action une chaine spécifiant l'action, par exemple "put_message" + * @param json les données à envoyer associé à l'action, par exemple {"cookie" : "LKJDLAKSJBFLKASN", "nick" : "Paul", "content" : "Bonjour", "answer_to" : [] } + * @param funOk la fonction exécuté après réception des données du serveur + * @param funError la fonction exécuté si une erreur arrive (facultatif) + * @param asynchrone true pour une communication asychrone (facultatif, truepar défaut) + * @param paramsSupp un objet contenant des paramètres supplémentaire pour la fonction ajax de jQuery (facultatif) + */ +euphorik.Communication.prototype.requete = function(action, json, funOk, funError, asynchrone, paramsSupp) { + var thisCommunication = this; if (asynchrone === undefined) { asynchrone = true; // asynchrone par défaut } - var mess = this.getbase(action); + var mess = this.getBase(action); objectEach(json, function(nom, val) { mess[nom] = val; - }); - - jQuery.ajax({ + }); + + if (this.funDebutReq) { + this.funDebutReq(); + } + + paramsAjax = { async: asynchrone, type: "POST", url: "request", dataType: "json", - data: { action : JSON.stringify(mess) } + data: { action : JSON.stringify(mess) }, success: - function(data) { + function(data) { + if (thisCommunication.funFinReq) { + thisCommunication.funFinReq(); + } if (data.reply === "error") { if (funError) { funError(data); - } else if (this.funError) { - this.funError(data); - } + } else if (thisCommunication.funError) { + thisCommunication.funError(data); + } } else if (funOk) { - funOk(data) + funOk(data); } + }, + error: + function(data) { + if (thisCommunication.funFinReq) { + thisCommunication.funFinReq(); + } } - }); -} + }; + + if (paramsSupp) { + objectEach(paramsSupp, function(nom, val) { + paramsAjax[nom] = val; + }); + } + + jQuery.ajax(paramsAjax); +}; + +euphorik.Communication.prototype.getBase = function(action) { + return { + "header" : { "action" : action, "version" : euphorik.conf.versionProtocole } + }; +}; \ No newline at end of file