X-Git-Url: http://git.euphorik.ch/index.cgi?a=blobdiff_plain;ds=sidebyside;f=js%2Fcommunication.js;h=64ebf403988502e68d55ede23993cb0e7b889db4;hb=HEAD;hp=db9beb887fd7d09b4079293376c00ca0ea02d775;hpb=972919ab7f5651cd721eb6eec75f7150fdeaf347;p=euphorik.git
diff --git a/js/communication.js b/js/communication.js
index db9beb8..64ebf40 100644
--- a/js/communication.js
+++ b/js/communication.js
@@ -16,47 +16,208 @@
// 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 = function(funError, funDebutReq, funFinReq) {
+ this.funError = funError;
+ this.funDebutReq = funDebutReq;
+ this.funFinReq = funFinReq;
+};
-euphorik.Communication.prototype.getBase = function(action) {
- this.base = {
- "header" : { "action" : action, "version" : euphorik.conf.versionProtocole }
- };
-}
+/**
+ * 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;
+};
-euphorik.Communication.prototype.requete = function(action, json, funOk, funError, asynchrone) {
+/**
+ * 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);
- objectEach(json, function(nom, val) {
- mess[nom] = val;
+ var mess = this.getBase(action);
+ objectEach(json, function(name, val) {
+ mess[name] = 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) {
+ 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(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 :
+ * - 'funsReceive' un ensemble de fonctions à appeler en fonction du "reply" du serveur, par exemple {"set_nom" : function(data) { print("ok : " + data.nom); } }
+ * - 'funSend' une fonction qui renvoie l'objet à envoyer avant l'attente, par exemple {"dernierMess" : 23}
+ * ("header" et "page" sont automatiquement ajoutés à l'objet)
+ *
+ * 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.
+ * @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){ ... }}
+ * @funSend une fonction renvoyant les données json à envoyer (optional)
+ */
+Comet.prototype.waitEvent = function(funsReceive, funSend) {
+ 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;
+
+ if (funSend !== undefined) {
+ var tmp = funSend();
+ objectEach(tmp, 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(funsReceive, funSend); }, 100);
+ },
+ error:
+ function(XMLHttpRequest, textStatus, errorThrown) {
+ // console.log("Connexion perdue dans Comet.prototype.waitEvent() : \n" + textStatus);
+ setTimeout(function(){ thisComet.waitEvent2(funsReceive, funSend); }, 1000);
}
});
-}
+};
+
+/**
+ * Si un stopAttenteCourante survient un peu n'importe quand il faut imédiatement arreter de boucler.
+ */
+Comet.prototype.waitEvent2 = function(funsReceive, funSend) {
+ if (this.stop) {
+ return;
+ }
+ this.waitEvent(funsReceive, funSend);
+};
\ No newline at end of file