X-Git-Url: http://git.euphorik.ch/?p=euphorik.git;a=blobdiff_plain;f=js%2Fcommunication.js;h=5ef2d888d2954e86bc4936b0d897ed90142489de;hp=e472ccb1cd5d0239484846f76d20a82cafcfc8e7;hb=24ed7a141aa345454300dd260bbabae3a9f92408;hpb=15f8b5143c6b9dcfe86eda84c22c31826a7f3d1c diff --git a/js/communication.js b/js/communication.js index e472ccb..5ef2d88 100644 --- a/js/communication.js +++ b/js/communication.js @@ -19,13 +19,43 @@ // 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(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 = function(funError) { - this.funError = funError; -} - euphorik.Communication.prototype.requete = function(action, json, funOk, funError, asynchrone, paramsSupp) { var thisCommunication = this; if (asynchrone === undefined) { @@ -33,9 +63,13 @@ euphorik.Communication.prototype.requete = function(action, json, funOk, funErro } var mess = this.getBase(action); - objectEach(json, function(nom, val) { - mess[nom] = val; - }); + objectEach(json, function(name, val) { + mess[name] = val; + }); + + if (this.funDebutReq) { + this.funDebutReq(); + } paramsAjax = { async: asynchrone, @@ -44,7 +78,10 @@ euphorik.Communication.prototype.requete = function(action, json, funOk, funErro dataType: "json", data: { action : JSON.stringify(mess) }, success: - function(data) { + function(data) { + if (thisCommunication.funFinReq) { + thisCommunication.funFinReq(); + } if (data.reply === "error") { if (funError) { funError(data); @@ -52,22 +89,133 @@ euphorik.Communication.prototype.requete = function(action, json, funOk, funErro 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; + objectEach(paramsSupp, function(name, val) { + paramsAjax[name] = val; }); } jQuery.ajax(paramsAjax); -} +}; + +euphorik.Communication.prototype.createCometConnection = function(name) { + return new Comet(name, this.getBase); +}; euphorik.Communication.prototype.getBase = function(action) { return { "header" : { "action" : action, "version" : euphorik.conf.versionProtocole } }; -} +}; + +/** + * Permet de gérer les événements (push serveur). + * Principe de fonctionnement : + * - La page courante créer un objet euphorik.Comet en indiquant le nom de la page et la version du protocole. + * - La page courante attend un événement en appelant 'waitEvent' (non-bloquant) et en donnant deux fonctions : + * - 'funSend' une fonction qui renvoie l'objet à envoyer avant l'attente, par exemple {"dernierMess" : 23} + * ("header" et "page" sont automatiquement ajoutés à l'objet) + * - 'funsReceive' un ensemble de fonctions à appeler en fonction du "reply" du serveur, par exemple {"set_nom" : function(data) { print("ok : " + data.nom); } } + * + * l'information envoyée est sous la forme : + * { + * "header" : {"action" : "wait_event", "version" : }, + * "page" : + * [..] + * } + * l'information reçue est sous la forme : + * { + * "reply" : + * [..] + * } + * et sont de type chaine + * + * @page [string] la page courante pour laquelle on écoute des événements (un string) + * @util [int] la version + */ +Comet = function(page, getBase) { + this.page = page; + this.getBase = getBase; + + // l'objet JSONHttpRequest représentant la connexion d'attente + this.attenteCourante = undefined; + + // le multhreading du pauvre, merci javascript de m'offrire autant de primitives pour la gestion de la concurrence... + this.stop = false; +}; + +/** + * Arrête l'attente courante s'il y en a une. + */ +Comet.prototype.stopAttenteCourante = function() { + this.stop = true; + + if (this.attenteCourante) { + this.attenteCourante.abort(); + } +}; + +/** + * Attend un événement lié à la page. Non-bloquant. + * @funSend une fonction renvoyant les données json à envoyer + * @funsReceive est un objet comprenant les fonctions à appeler en fonction du "reply" + * les fonctions acceptent un paramètre correspondant au données reçues. + * exemple : {"new_message" : function(data){ ... }} + */ +Comet.prototype.waitEvent = function(funSend, funsReceive) { + this.stopAttenteCourante(); + this.stop = false; + var thisComet = this; + + // on doit conserver l'ordre des valeurs de l'objet JSON (le serveur les veut dans l'ordre définit dans le protocole) + // TODO : ya pas mieux ? + var dataToSend = this.getBase("wait_event") + dataToSend["page"] = this.page; + + var poulpe = funSend(); + objectEach(poulpe, function(k, v) { + dataToSend[k] = v; + }); + + this.attenteCourante = jQuery.ajax({ + type: "POST", + url: "request", + dataType: "json", + // TODO : doit disparaitre + timeout: 180000, // timeout de 3min. Gros HACK pas beau. FIXME problème décrit ici : http://groups.google.com/group/jquery-en/browse_thread/thread/8724e64af3333a76 + data: { action : JSON.stringify(dataToSend) }, + success: + function(data) { + funsReceive[data.reply](data); + + // rappel de la fonction dans 100 ms + setTimeout(function(){ thisComet.waitEvent2(funSend, funsReceive); }, 100); + }, + error: + function(XMLHttpRequest, textStatus, errorThrown) { + ;; console.log("Connexion perdue dans Comet.prototype.waitEvent() : \n" + textStatus); + setTimeout(function(){ thisComet.waitEvent2(funSend, funsReceive); }, 1000); + } + }); +}; + +/** + * Si un stopAttenteCourante survient un peu n'importe quand il faut imédiatement arreter de boucler. + */ +Comet.prototype.waitEvent2 = function(funSend, funsReceive) { + if (this.stop) { + return; + } + this.waitEvent(funSend, funsReceive); +}; \ No newline at end of file