diff --git a/src/ninegate-1.0/app/config/config.yml b/src/ninegate-1.0/app/config/config.yml index 08147083..d13e70c6 100644 --- a/src/ninegate-1.0/app/config/config.yml +++ b/src/ninegate-1.0/app/config/config.yml @@ -137,6 +137,7 @@ gos_web_socket: server: port: %websocket_port% #The port the socket server will listen on host: %websocket_host% #The host ip to bind to + router: resources: - "@CadolesWebsocketBundle/Resources/config/topic.yml" diff --git a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Controller/ChatController.php b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Controller/ChatController.php index c5611688..c4407118 100644 --- a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Controller/ChatController.php +++ b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Controller/ChatController.php @@ -105,4 +105,47 @@ class ChatController extends Controller $response->headers->set('Content-Type', 'application/json'); return $response; } + + public function listAction(Request $request) { + // S'assurer que c'est un appel ajax + if (!$request->isXmlHttpRequest()) { + return new JsonResponse(array('message' => 'Interdit'), 400); + } + + $em = $this->getDoctrine()->getManager(); + $id=$request->request->get('id'); + $output=array(); + + // On récupère le groupe + $group=$em->getRepository("CadolesCoreBundle:Group")->find($id); + if(!$group) die(); + + // On récupere le user + $user=$this->getUser(); + if(!$user) die(); + + // On récupère le lien usergroup + $usergroup=$em->getRepository("CadolesCoreBundle:UserGroup")->findOneBy(["group"=>$group,"user"=>$user]); + if(!$usergroup) die(); + + // On récupere les messages + $messages=$em->getRepository("CadolesWebsocketBundle:Message")->findBy(["group"=>$group],["submitdate"=>"DESC"],10); + + foreach($messages as $message) { + $tmp=[]; + $tmp["id"]=$message->getId(); + $tmp["message"]=$message->getTopic(); + $tmp["submitdate"]=$message->getSubmitdate(); + $tmp["userid"]=$message->getUser()->getId(); + $tmp["userlastname"]=$message->getUser()->getLastname(); + $tmp["userfirstname"]=$message->getUser()->getFirstname(); + $tmp["useravatar"]=$message->getUser()->getAvatar(); + + array_push($output,$tmp); + } + + $response = new Response(json_encode($output)); + $response->headers->set('Content-Type', 'application/json'); + return $response; + } } diff --git a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Resources/config/routing.yml b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Resources/config/routing.yml index b249936e..800de139 100644 --- a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Resources/config/routing.yml +++ b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Resources/config/routing.yml @@ -4,4 +4,8 @@ cadoles_websocket_chat: cadoles_websocket_message_see: path: /user/message/see - defaults: { _controller: CadolesWebsocketBundle:Chat:see } \ No newline at end of file + defaults: { _controller: CadolesWebsocketBundle:Chat:see } + +cadoles_websocket_message_list: + path: /user/message/list + defaults: { _controller: CadolesWebsocketBundle:Chat:list } \ No newline at end of file diff --git a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Resources/views/Chat/client.html.twig b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Resources/views/Chat/client.html.twig index d4135296..2f4f1e39 100644 --- a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Resources/views/Chat/client.html.twig +++ b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Resources/views/Chat/client.html.twig @@ -115,12 +115,90 @@ {% block localexternalscript %} - {% endblock %} {% block localjavascript %} -var webSocket; + {% if websocket_activate and app.user %} + var WS = (function() + { + var GosSocket = function(uri, sessionConfig){ + this._uri = uri; + this._session = false; + this._listeners = {}; + this.connect(sessionConfig); + }; + + GosSocket.prototype.connect = function (sessionConfig) { + var that = this; + + ab.connect(this._uri, + + //Function on connect + function(session){ + that.fire({type: "socket/connect", data: session }); + }, + + //Function on disconnect / error + function(code, reason){ + that._session = false; + + that.fire({type: "socket/disconnect", data: {code: code, reason: reason}}); + }, + + sessionConfig + ); + }; + + GosSocket.prototype.on = function(type, listener){ + if (typeof this._listeners[type] == "undefined"){ + this._listeners[type] = []; + } + + this._listeners[type].push(listener); + }; + + GosSocket.prototype.fire = function(event){ + if (typeof event == "string"){ + event = { type: event }; + } + if (!event.target){ + event.target = this; + } + + if (!event.type){ //falsy + throw new Error("Event object missing 'type' property."); + } + + if (this._listeners[event.type] instanceof Array){ + var listeners = this._listeners[event.type]; + for (var i=0, len=listeners.length; i < len; i++){ + listeners[i].call(this, event.data); + } + } + }; + + GosSocket.prototype.off = function(type, listener){ + if (this._listeners[type] instanceof Array){ + var listeners = this._listeners[type]; + for (var i=0, len=listeners.length; i < len; i++){ + if (listeners[i] === listener){ + listeners.splice(i, 1); + break; + } + } + } + }; + + return { + connect: function(uri, sessionConfig) + { + return new GosSocket(uri, sessionConfig); + } + } + + })(); + {% endif %} $(document).ready(function(){ if (CKEDITOR.instances["chat_message"]) CKEDITOR.instances["chat_message"].destroy(); @@ -153,26 +231,43 @@ var webSocket; }); - function websocket() - { - {% if websocket_activate and app.user %} + {% if websocket_activate and app.user %} + + var session; + var dateoptions = {weekday: "long", year: "numeric", month: "long", day: "numeric", hour: "2-digit", minute: "2-digit" }; + var timer; + var delayRetry=5000; + var isdeco=false; + var tosend=false; + var tosendmail=false; + var todelete=false; + var idtodelete; + + function websocket() + { var _WS_URI = "wss://{{ gos_web_socket_server_host }}:{{ gos_web_socket_server_port }}"; - - console.log("INI WS"); - webSocket = WS.connect(_WS_URI); - console.log("END INI WS"); + webSocket = WS.connect(_WS_URI,{retryDelay: delayRetry}); - var dateoptions = {weekday: "long", year: "numeric", month: "long", day: "numeric", hour: "2-digit", minute: "2-digit" }; - var timer; - var session; + webSocket.on("socket/connect", function (sess) { + console.log("=== CONNECT ==="); - webSocket.on("socket/connect", function (session) { - // Reinit du tps de tentative de reco - $('#modalinfo').modal('hide'); - clearInterval(timer); + // Sauvegardce de la session + session=sess; //the callback function in "subscribe" is called everytime an event is published in that channel. - session.subscribe("websocket/channel/{{groupid}}", function (uri, payload) { + sess.subscribe("websocket/channel/{{groupid}}", function (uri, payload) { + + // Récupération lastmessage + if(isdeco) recuplastmsg(); + + // Si action en cours + if(tosend) sendbtn(); + if(tosendmail) sendbtnmail(); + if(todelete) delmessage(idtodelete); + + // Il est co + isdeco=false; + if(payload.msg) { html ="