// You should have received a copy of the GNU General Public License\r
// along with Euphorik. If not, see <http://www.gnu.org/licenses/>.\r
\r
-// regroupe la partie communication client -> sevreur de euphorik.\r
+// Regroupe la partie communication JSON client -> serveur de euphorik.\r
+// Voir : http://dev.euphorik.ch/wiki/euk/Protocole\r
\r
-/**\r
- * @param funError un fonction executé lors d'un réponse 'error' de la part du serveur, peut être redéfinit pour une requête.\r
+/**
+ * Les fonctions debutReq et finReq servent, par exemple, à afficher à l'utilisateur
+ * qu'une communication est en cours.\r
+ * @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)\r
*/\r
-euphorik.Communication = function(util, funError) {\r
-}\r
-\r
-euphorik.Communication.prototype.getBase = function(action) {\r
- this.base = {\r
- "header" : { "action" : action, "version" : euphorik.conf.versionProtocole }\r
- };\r
-}\r
-\r
-euphorik.Communication.prototype.requete = function(action, json, funOk, funError, asynchrone) {\r
+euphorik.Communication = function(funError, funDebutReq, funFinReq) {\r
+ this.funError = funError;
+ this.funDebutReq = funDebutReq;
+ this.funFinReq = funFinReq;\r
+};
+
+/**
+ * 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;
+};\r
+
+/**
+ * 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)
+ */\r
+euphorik.Communication.prototype.requete = function(action, json, funOk, funError, asynchrone, paramsSupp) {\r
+ var thisCommunication = this;\r
if (asynchrone === undefined) {\r
asynchrone = true; // asynchrone par défaut\r
}\r
\r
- var mess = this.getbase(action);\r
+ var mess = this.getBase(action);\r
objectEach(json, function(nom, val) {\r
mess[nom] = val;\r
- });\r
- \r
- jQuery.ajax({\r
+ });
+
+ if (this.funDebutReq) {
+ this.funDebutReq();
+ }\r
+ \r
+ paramsAjax = {\r
async: asynchrone,\r
type: "POST",\r
url: "request",\r
dataType: "json",\r
- data: { action : JSON.stringify(mess) }\r
+ data: { action : JSON.stringify(mess) },\r
success:\r
- function(data) {\r
+ function(data) {
+ if (thisCommunication.funFinReq) {
+ thisCommunication.funFinReq();
+ }\r
if (data.reply === "error") {\r
if (funError) {\r
funError(data);\r
- } else if (this.funError) {\r
- this.funError(data);\r
- } \r
+ } else if (thisCommunication.funError) {\r
+ thisCommunication.funError(data);\r
+ }\r
} else if (funOk) {\r
- funOk(data)\r
+ funOk(data);\r
}\r
+ },
+ error:
+ function(data) {
+ if (thisCommunication.funFinReq) {
+ thisCommunication.funFinReq();
+ }
}\r
- });\r
-}\r
+ };\r
+ \r
+ if (paramsSupp) {\r
+ objectEach(paramsSupp, function(nom, val) {\r
+ paramsAjax[nom] = val;\r
+ });\r
+ }\r
+ \r
+ jQuery.ajax(paramsAjax);\r
+};\r
+\r
+euphorik.Communication.prototype.getBase = function(action) {\r
+ return {\r
+ "header" : { "action" : action, "version" : euphorik.conf.versionProtocole }\r
+ };\r
+};
\ No newline at end of file