upd: view.main (added /connect ws-client management (connected: [], disconnected: []) + list unicity + notifications + sound (mp3 not pushed for now)

This commit is contained in:
xdrm-brackets 2017-12-06 16:23:21 +01:00
parent 6a483539f5
commit 40967db18b
1 changed files with 47 additions and 18 deletions

View File

@ -48,7 +48,7 @@ gstore.data.func.nav(router, null);
/* (3) Set WebSocket channels /* (3) Set WebSocket channels
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* (1) Connection channel */ /* (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 // // {1} Manage error //
if( msg == null && err != null ) if( msg == null && err != null )
@ -58,17 +58,42 @@ gstore.data.func.nav(router, null);
if( typeof msg.error != 'boolean' || msg.error !== false ) if( typeof msg.error != 'boolean' || msg.error !== false )
return infobox.show('Erreur de connexion WebSocket@connect', 'warning', 3000); return infobox.show('Erreur de connexion WebSocket@connect', 'warning', 3000);
// {3} Manage notification // // {3} If no data -> exit //
if( msg.connected != null ){ if( msg.connected == null && msg.disconnected == null )
console.log('Detected '+msg.connected.length+' new user(s)'); 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].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 // // {5} Add disconnected users to stack //
gstore.data.func.nav(router, null); 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 */ /* (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 ) if( typeof msg.error != 'boolean' || msg.error !== false )
return infobox.show('Erreur de connexion WebSocket@chat', 'warning', 3000); return infobox.show('Erreur de connexion WebSocket@chat', 'warning', 3000);
// {3} Manage notification // // {3} If no message -> exit //
if( msg.msg != null ){ if( msg.msg == null )
console.log('Received '+msg.msg.length+' new message(s)'); 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 ); 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 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}); }).send({name: _SERVER.session.name});
@ -101,5 +130,5 @@ window.wsc_chat = wsc.channel('chat').listen(function(msg, err){
---------------------------------------------------------*/ ---------------------------------------------------------*/
window.onbeforeunload = function() { window.onbeforeunload = function() {
wsc_chat.send('{"close": true}'); wsc_chat.send('{"close": true}');
/*NO_WS_CONNECT*///wsc_connect.send('{"close": true}'); wsc_connect.send('{"close": true}');
}; };