From 1f3f2246fd857342fb611ad7e27d6a8471be90f6 Mon Sep 17 00:00:00 2001 From: afornerot Date: Thu, 19 Sep 2019 13:46:25 +0200 Subject: [PATCH] gestion websocket server --- .../scripts/ninegate-postservice-01.sh | 11 +-- .../scripts/ninegate-websocket.sh | 25 ++++-- .../CoreBundle/Command/data/core-init-01.sql | 2 +- .../Controller/PageController.php | 21 +++++ .../PortalBundle/Resources/config/routing.yml | 4 + .../Resources/views/Page/pages.html.twig | 89 +++++++++++++++++-- .../Resources/views/Chat/client.html.twig | 49 +++++++++- .../WebsocketBundle/Topic/WebsocketTopic.php | 4 +- tmpl/ninegate.cron | 5 ++ 9 files changed, 183 insertions(+), 27 deletions(-) diff --git a/src/ninegate-1.0/scripts/ninegate-postservice-01.sh b/src/ninegate-1.0/scripts/ninegate-postservice-01.sh index 906af8dd..360d7235 100755 --- a/src/ninegate-1.0/scripts/ninegate-postservice-01.sh +++ b/src/ninegate-1.0/scripts/ninegate-postservice-01.sh @@ -22,6 +22,7 @@ composer install ./perm.sh www-data # Nettoyage du cache +rm /var/www/html/ninegate/var/cache/* -rf php bin/console cache:clear --env=prod --no-debug # Migration si nécessaire du schéma de la base @@ -55,13 +56,5 @@ fi # Server Websocket if [[ "$ninegate_websocket" = 'oui' ]] then - echo "" - echo WEBSOCKET = STOP - pid="$(pgrep -f gos:websocket)" - if [ -n "${pid}" ]; then - kill -9 ${pid}; - fi - - echo WEBSOCKET = START - bin/console gos:websocket:server --port $websocket_portinterne -a $websocket_url --no-debug -n -q --env=prod & disown + /var/www/html/ninegate/scripts/ninegate-websocket.sh fi \ No newline at end of file diff --git a/src/ninegate-1.0/scripts/ninegate-websocket.sh b/src/ninegate-1.0/scripts/ninegate-websocket.sh index 6a42c30e..eeafe2ee 100755 --- a/src/ninegate-1.0/scripts/ninegate-websocket.sh +++ b/src/ninegate-1.0/scripts/ninegate-websocket.sh @@ -3,11 +3,24 @@ websocket_url=$(CreoleGet adresse_ip_eth0 non) websocket_portinterne=$(CreoleGet ninegate_websocket_portinterne non) # Server Websocket -echo WEBSOCKET = STOP -pid="$(pgrep -f gos:websocket)" -if [ -n "${pid}" ]; then - kill -9 ${pid}; +if [[ "$1" != "restartifdown" ]]; then + echo WEBSOCKET = STOP + pid="$(pgrep -a -f gos:websocket | grep $websocket_portinterne)" + if [ -n "${pid}" ]; then + IFS=' ' read -r -a array <<< "$pid" + kill -9 ${array[0]}; + fi fi -echo WEBSOCKET = START -bin/console gos:websocket:server --port $websocket_portinterne -a $websocket_url --no-debug -n -q --env=prod & disown \ No newline at end of file +# Port LISTEN ? +pid="$(pgrep -a -f gos:websocket | grep $websocket_portinterne)" +restart="yes" +if [ "$1" == "restartifdown" ] && [ -n "${pid}" ] ; then + restart="no" +fi + +if [[ "$restart" == "yes" ]]; then + echo WEBSOCKET = START + cd /var/www/html/ninegate + bin/console gos:websocket:server --port $websocket_portinterne -a $websocket_url --no-debug -n -q --env=prod & disown +fi \ No newline at end of file diff --git a/src/ninegate-1.0/src/Cadoles/CoreBundle/Command/data/core-init-01.sql b/src/ninegate-1.0/src/Cadoles/CoreBundle/Command/data/core-init-01.sql index b16a6853..c8e9650b 100644 --- a/src/ninegate-1.0/src/Cadoles/CoreBundle/Command/data/core-init-01.sql +++ b/src/ninegate-1.0/src/Cadoles/CoreBundle/Command/data/core-init-01.sql @@ -8,7 +8,7 @@ INSERT IGNORE INTO `niveau01` (`id`, `label`, `siren`) VALUES (-100, 'DRAAF', '130007107'); INSERT IGNORE INTO `user` (`id`, `niveau01_id`, `username`, `firstname`, `lastname`, `password`, `email`, `avatar`, `role`,`siren`,`authlevel`) VALUES -(-100, -100, 'admin', 'Administrateur', 'draaf', '{SSHA}30Z8NQIFHi2TUxnZr8sENMbBiOWgonju +(-100, -100, 'admin', 'Administrateur', 'draaf', '{SSHA}vVciL1BZlGqITdyEI5am1J21xEgy2M02 ', 'admin@ldapbundle.ac-arno.fr', 'admin.jpg', 'ROLE_ADMIN', '130007107', 'simple'); diff --git a/src/ninegate-1.0/src/Cadoles/PortalBundle/Controller/PageController.php b/src/ninegate-1.0/src/Cadoles/PortalBundle/Controller/PageController.php index 63d39a5e..a106066a 100644 --- a/src/ninegate-1.0/src/Cadoles/PortalBundle/Controller/PageController.php +++ b/src/ninegate-1.0/src/Cadoles/PortalBundle/Controller/PageController.php @@ -646,6 +646,27 @@ class PageController extends Controller } + public function msgcounterAction(Request $request,$access="config") + { + // S'assurer que c'est un appel ajax + if (!$request->isXmlHttpRequest()) { + return new JsonResponse(array('message' => 'Interdit'), 400); + } + + $user=$this->getUser(); + $this->getDoctrine()->getRepository("CadolesPortalBundle:Page")->getPagesUser($user,null,$entity,$pagesuser,$pagesadmin,$groupsshared); + + $output=[]; + foreach($groupsshared as $groupshared) { + $output[$groupshared->getId()]=$groupshared->pagesshared[0]->getCounterRead(); + } + + $response = new Response(json_encode($output)); + $response->headers->set('Content-Type', 'application/json'); + return $response; + + } + protected function getErrorForm($id,$form,$request,$data,$mode) { if ($form->get('submit')->isClicked() && $mode=="submit") { $usage = $form->get('usage')->getData(); diff --git a/src/ninegate-1.0/src/Cadoles/PortalBundle/Resources/config/routing.yml b/src/ninegate-1.0/src/Cadoles/PortalBundle/Resources/config/routing.yml index 1107dbf0..bbb8fa1d 100644 --- a/src/ninegate-1.0/src/Cadoles/PortalBundle/Resources/config/routing.yml +++ b/src/ninegate-1.0/src/Cadoles/PortalBundle/Resources/config/routing.yml @@ -325,6 +325,10 @@ cadoles_portal_user_page_application: path: /page/application defaults: { _controller: CadolesPortalBundle:Page:application, access: user } +cadoles_portal_user_page_ajax_msgcounter: + path: /page/ajax/msgcounter + defaults: { _controller: CadolesPortalBundle:Page:msgcounter, access: user } + #== BOOKMARK ============================================================================================================================================= diff --git a/src/ninegate-1.0/src/Cadoles/PortalBundle/Resources/views/Page/pages.html.twig b/src/ninegate-1.0/src/Cadoles/PortalBundle/Resources/views/Page/pages.html.twig index ebef369d..bc0124de 100644 --- a/src/ninegate-1.0/src/Cadoles/PortalBundle/Resources/views/Page/pages.html.twig +++ b/src/ninegate-1.0/src/Cadoles/PortalBundle/Resources/views/Page/pages.html.twig @@ -269,7 +269,14 @@ order: [[ 1, "asc" ]], }); + {% if app.user %} + setInterval(function(){ + loadmsgCounter() + }, 10000); + {% endif %} + // Init socket de counter + /* Désactiver car il semble préférable de faire un appel ajax régulier plutot qu'ouvrir un channel websocket {% if websocket_activate %} var _WS_URI = "wss://{{ gos_web_socket_server_host }}:{{ gos_web_socket_server_port }}"; @@ -309,6 +316,7 @@ }); }); {% endif %} + */ }); $(window).resize(function() { @@ -336,6 +344,7 @@ // Affichage des frames associés aux items de bureau function showFrameitem(id,url,forcereload) { + $(".pageframereload").remove(); $(".pageframe").hide(); // Si force le rechargement et frame existante on la détruit @@ -346,9 +355,11 @@ if($("#frameitem-"+id).length) $("#frameitem-"+id).show(); // Sinon on la génère - else - $("#pagecontainer").append(""); - + else { + var myclass=""; + if(forcereload) myclass="pageframereload"; + $("#pagecontainer").append(""); + } resizeFrame(); } @@ -359,6 +370,7 @@ idpage=id; // Cacher toutes les pages + $(".pageframereload").remove(); $(".pageframe").hide(); // Rendre actif le page en cours dans le menu @@ -378,8 +390,11 @@ if($("#page-"+id).length) $("#page-"+id).show(); // Sinon on la génère - else - $("#pagecontainer").append(""); + else { + var myclass=""; + if(forcereload) myclass="pageframereload"; + $("#pagecontainer").append(""); + } // Détruire le badge associé car normalement de fait on a lu les notif if(usage=="group") { @@ -455,9 +470,73 @@ $(location).attr('href', url); }; + function loadmsgCounter() { + /* + menu=$("a[data-group='"+payload.group+"']"); + if(menu.length) { + if(payload.add) { + console.log("Ajout counter pour group "+payload.group+" by "+payload.from); + if(menu.children(".badge").length) { + menu.children(".badge").html(+(menu.children(".badge").html())+1); + } + else menu.append("10) + menu.append(""+counter+"Impossible de se connecter au serveur.
Vous avez dépassé le nombre de reconnexion maximum

Veuillez retentez d'ici quelques secondes

"+laberror+"


Forcer la tentative de reconnexion"); + + clearInterval(timer); + var nbtentative=10; + timer=setInterval(function(){ $('#modalinfotimer').html((nbtentative--)+"s") }, 1000); + + setTimeout(function(){ location.reload(true); }, 10000); + } + + if(error.code==3) { + $('#modalinfotext').html("
Impossible de se connecter au serveur.
Veuillez retentez d'ici quelques secondes

"+laberror+"


Forcer la tentative de reconnexion
"); + + clearInterval(timer); + var nbtentative=10; + timer=setInterval(function(){ $('#modalinfotimer').html((nbtentative--)+"s") }, 1000); + + setTimeout(function(){ location.reload(true); }, 10000); + } + + if(error.code==6 || error.code==5) { + $('#modalinfotext').html("
Impossible de se connecter au serveur.
Vous allez être reconnecté d'ici quelques secondes

"+laberror+"


Forcer la tentative de reconnexion
"); + + clearInterval(timer); + var nbtentative=5; + timer=setInterval(function(){ $('#modalinfotimer').html((nbtentative--)+"s") }, 1000); + } + }); + {% endif %} }); diff --git a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Topic/WebsocketTopic.php b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Topic/WebsocketTopic.php index 01f46989..92f03209 100644 --- a/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Topic/WebsocketTopic.php +++ b/src/ninegate-1.0/src/Cadoles/WebsocketBundle/Topic/WebsocketTopic.php @@ -37,7 +37,7 @@ class WebsocketTopic implements TopicInterface public function onSubscribe(ConnectionInterface $connection, Topic $topic, WampRequest $request) { //this will broadcast the message to ALL subscribers of this topic. - //$topic->broadcast(['log' => $connection->resourceId." has joined ".$topic->getId()]); + $topic->broadcast(['log' => $connection->resourceId." has joined ".$topic->getId()]); } @@ -52,7 +52,7 @@ class WebsocketTopic implements TopicInterface public function onUnSubscribe(ConnectionInterface $connection, Topic $topic, WampRequest $request) { //this will broadcast the message to ALL subscribers of this topic. - //$topic->broadcast(['msg' => $connection->resourceId . " has left " . $topic->getId()]); + $topic->broadcast(['log' => $connection->resourceId . " has left " . $topic->getId()]); } diff --git a/tmpl/ninegate.cron b/tmpl/ninegate.cron index ab6e7635..470ea282 100644 --- a/tmpl/ninegate.cron +++ b/tmpl/ninegate.cron @@ -1 +1,6 @@ * * * * * root /var/www/html/ninegate/scripts/ninegate-cron.sh &>/dev/null +%if %%getVar("ninegate_activate_websocket", 'non') == "oui" +* * * * * root /var/www/html/ninegate/scripts/ninegate-websocket.sh restartifdown &>/dev/null +0 5 * * * root /var/www/html/ninegate/scripts/ninegate-websocket.sh &>/dev/null +%end if +