// 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\r
+ * 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.\r
+ * @param funDebutReq fonction appelée au début d'une requête (facultatif)\r
+ * @param funFinReq fonction appelée à la fin d'une requête (facultatif)\r
*/\r
-euphorik.Communication = function(util, funError) {\r
-}\r
+euphorik.Communication = function(funError, funDebutReq, funFinReq) {\r
+ this.funError = funError;\r
+ this.funDebutReq = funDebutReq;\r
+ this.funFinReq = funFinReq;\r
+};\r
\r
-euphorik.Communication.prototype.getBase = function(action) {\r
- this.base = {\r
- "header" : { "action" : action, "version" : euphorik.conf.versionProtocole }\r
- };\r
-}\r
+/**\r
+ * Charge un fichier depuis une url et retourne son contenu.\r
+ */\r
+euphorik.Communication.prototype.load = function(url) {\r
+ if (this.funDebutReq) {\r
+ this.funDebutReq();\r
+ }\r
+ var contenu = "";\r
+ $.ajax({async: false, url: url, success : function(page) { contenu += page; }});\r
+ if (this.funFinReq) {\r
+ this.funFinReq();\r
+ }\r
+ return contenu;\r
+};\r
\r
-euphorik.Communication.prototype.requete = function(action, json, funOk, funError, asynchrone) {\r
+/**\r
+ * Effectue une requête JSON auprès du serveur.\r
+ * @param action une chaine spécifiant l'action, par exemple "put_message"\r
+ * @param json les données à envoyer associé à l'action, par exemple {"cookie" : "LKJDLAKSJBFLKASN", "nick" : "Paul", "content" : "Bonjour", "answer_to" : [] }\r
+ * @param funOk la fonction exécuté après réception des données du serveur\r
+ * @param funError la fonction exécuté si une erreur arrive (facultatif)\r
+ * @param asynchrone true pour une communication asychrone (facultatif, truepar défaut)\r
+ * @param paramsSupp un objet contenant des paramètres supplémentaire pour la fonction ajax de jQuery (facultatif)\r
+ */\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
+\r
+ if (this.funDebutReq) {\r
+ this.funDebutReq();\r
+ }\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
+ if (thisCommunication.funFinReq) {\r
+ thisCommunication.funFinReq();\r
+ }\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
+ },\r
+ error:\r
+ function(data) {\r
+ if (thisCommunication.funFinReq) {\r
+ thisCommunication.funFinReq();\r
}\r
}\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
+};\r