From 40967db18b895860047c8f9de94271d12604a41c Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 6 Dec 2017 16:23:21 +0100 Subject: [PATCH] upd: view.main (added /connect ws-client management (connected: [], disconnected: []) + list unicity + notifications + sound (mp3 not pushed for now) --- view/main.js | 65 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 18 deletions(-) diff --git a/view/main.js b/view/main.js index e99ad94..6484cb3 100755 --- a/view/main.js +++ b/view/main.js @@ -48,7 +48,7 @@ gstore.data.func.nav(router, null); /* (3) Set WebSocket channels ---------------------------------------------------------*/ /* (1) Connection channel */ -/*NO_WS_CONNECT*//*window.wsc_connect = wsc.channel('connect').listen(function(msg, err){ +window.wsc_connect = wsc.channel('connect').listen(function(msg, err){ // {1} Manage error // if( msg == null && err != null ) @@ -58,17 +58,42 @@ gstore.data.func.nav(router, null); if( typeof msg.error != 'boolean' || msg.error !== false ) return infobox.show('Erreur de connexion WebSocket@connect', 'warning', 3000); - // {3} Manage notification // - if( msg.connected != null ){ - console.log('Detected '+msg.connected.length+' new user(s)'); + // {3} If no data -> exit // + if( msg.connected == null && msg.disconnected == null ) + return; + + // {4} Add connected users to stack // + if( msg.connected instanceof Array ){ + + // -1- add connected users + console.log('Detected '+msg.connected.length+' connected user(s)'); + var lastLen = gstore.data.notif[0].data.length; gstore.data.notif[0].data = gstore.data.notif[0].data.concat( msg.connected ); - gstore.data.notif[0].count += msg.connected.length; + + // -2- make each user unique + gstore.data.notif[0].data = gstore.data.notif[0].data.filter(function(item, i, arr){ return arr.indexOf(item) === i; }); + + // -3- Update count if not already on page + if( router.app.$route.path != 'notifications' ) + gstore.data.notif[0].count += gstore.data.notif[0].data.length - lastLen; + } - // {4} Reset notification count if already on page // - gstore.data.func.nav(router, null); + // {5} Add disconnected users to stack // + if( msg.disconnected instanceof Array ){ -}).send({name: _SERVER.session.name}); */ + // -1- Remove each disconnected user + console.log('Detected '+msg.disconnected.length+' disconnected user(s)'); + var lastLen = gstore.data.notif[0].data.length; + gstore.data.notif[0].data = gstore.data.notif[0].data.filter(function(item){ return msg.disconnected.indexOf(item) === -1; }); + + // -2- Update count if not already on page + if( router.app.$route.path != 'notifications' ) + gstore.data.notif[0].count += lastLen - gstore.data.notif[0].data.length; + + } + +}).send({name: _SERVER.session.name}); /* (2) Message channel */ @@ -82,16 +107,20 @@ window.wsc_chat = wsc.channel('chat').listen(function(msg, err){ if( typeof msg.error != 'boolean' || msg.error !== false ) return infobox.show('Erreur de connexion WebSocket@chat', 'warning', 3000); - // {3} Manage notification // - if( msg.msg != null ){ - console.log('Received '+msg.msg.length+' new message(s)'); - gstore.data.notif[1].data = gstore.data.notif[1].data.concat( msg.msg ); + // {3} If no message -> exit // + if( msg.msg == null ) + return; + + // {4} Play sound if 1msg received + not already on page // + if( msg.msg.length == 1 && router.app.$route.path != 'inbox' ) + ( new Audio('/sound/notification.mp3') ).play(); + + // {5} Add messages to stack // + gstore.data.notif[1].data = gstore.data.notif[1].data.concat( msg.msg ); + + // {6} Add notification count if not already on page // + if( router.app.$route.path != 'inbox' ) gstore.data.notif[1].count += msg.msg.length - } - - // {4} Reset notification count if already on page // - gstore.data.func.nav(router, null); - }).send({name: _SERVER.session.name}); @@ -101,5 +130,5 @@ window.wsc_chat = wsc.channel('chat').listen(function(msg, err){ ---------------------------------------------------------*/ window.onbeforeunload = function() { wsc_chat.send('{"close": true}'); - /*NO_WS_CONNECT*///wsc_connect.send('{"close": true}'); + wsc_connect.send('{"close": true}'); }; \ No newline at end of file