From 84cabd92ebb825f924548a3fbd64ea0ed51b996f Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 7 Dec 2017 19:11:23 +0100 Subject: [PATCH] upd: view.main (removed websocket management) | add: view.websocket (dispatched from 'view.main') --- view/main.js | 36 +++------------- view/websocket.js | 106 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 30 deletions(-) create mode 100644 view/websocket.js diff --git a/view/main.js b/view/main.js index f34e78d..4ad6a15 100755 --- a/view/main.js +++ b/view/main.js @@ -48,35 +48,11 @@ gstore.data.func.nav(router, null); /* (3) Set WebSocket channels ---------------------------------------------------------*/ -/* (1) Message channel */ -window.wsc_chat = wsc.channel('chat').listen(function(msg, err){ +/* (1) Make router globally available */ +window._router_ = router; - // {1} Manage error // - if( msg == null && err != null ) - return infobox.show('Erreur de connexion WebSocket@chat ('+err+')', 'error', 3000); +/* (2) Create/Manage web socket clients */ +require('./websocket'); - // {2} Manage wsclient error // - if( typeof msg.error != 'boolean' || msg.error !== false ) - return infobox.show('Erreur de connexion WebSocket@chat', 'warning', 3000); - - // {3} If no message -> exit // - if( msg.msg == null ) - return; - - // {4} Play sound if 1msg received + not already on page // - // note: 1msg means a new message but not the page load past buffer - if( msg.msg.length == 1 && router.app.$route.path != '/inbox' ) - ( new Audio('https://notificationsounds.com/message-tones/communication-channel-519/download/mp3') ).play(); - - // {5} Add messages to stack // - gstore.data.notif.inbox.data = gstore.data.notif.inbox.data.concat( msg.msg ); - - // {6} Add notification count if not already on page // - if( router.app.$route.path != '/inbox' ) - gstore.data.notif.inbox.count += msg.msg.length; - - // {7} Remove loader // - gstore.data.msg_pending.inbox = false; - - -}).send({name: _SERVER.session.name}); \ No newline at end of file +/* (3) Remove global ref to router */ +delete window._router_; \ No newline at end of file diff --git a/view/websocket.js b/view/websocket.js new file mode 100644 index 0000000..72ff9e5 --- /dev/null +++ b/view/websocket.js @@ -0,0 +1,106 @@ +// make router usable as it +const router = window._router_; + + + +/* (1) Emergency channel +---------------------------------------------------------*/ +window.wsc_emergency = wsc.channel('emergency').listen(function(msg, err){ + + /* (1) Manage error */ + if( msg == null && err != null ) + return infobox.show('Erreur de connexion WebSocket@emergency ('+err+')', 'error', 3000); + + /* (2) Manage wsclient error */ + if( typeof msg.error != 'boolean' || msg.error !== false ) + return infobox.show('Erreur de connexion WebSocket@emergency', 'warning', 3000); + + /* (3) If no message -> exit */ + if( msg.msg == null ) + return; + + /* (4) Play sound if 1msg received + not already on page */ + // note: 1msg means a new message but not the page load past buffer + if( msg.msg.length == 1 && router.app.$route.path != '/emergency' ) + ( new Audio('https://notificationsounds.com/message-tones/communication-channel-519/download/mp3') ).play(); + + /* (5) Add messages to stack */ + gstore.data.notif.emergency.data = gstore.data.notif.emergency.data.concat( msg.msg ); + + /* (6) Add notification count if not already on page */ + if( router.app.$route.path != '/emergency' ) + gstore.data.notif.emergency.count += msg.msg.length; + + /* (7) Remove loader */ + gstore.data.msg_pending.emergency = false; + + +}).send({name: _SERVER.session.name}); + +/* (2) Event channel +---------------------------------------------------------*/ +window.wsc_event = wsc.channel('event').listen(function(msg, err){ + + /* (1) Manage error */ + if( msg == null && err != null ) + return infobox.show('Erreur de connexion WebSocket@event ('+err+')', 'error', 3000); + + /* (2) Manage wsclient error */ + if( typeof msg.error != 'boolean' || msg.error !== false ) + return infobox.show('Erreur de connexion WebSocket@event', 'warning', 3000); + + /* (3) If no message -> exit */ + if( msg.msg == null ) + return; + + /* (4) Play sound if 1msg received + not already on page */ + // note: 1msg means a new message but not the page load past buffer + if( msg.msg.length == 1 && router.app.$route.path != '/event' ) + ( new Audio('https://notificationsounds.com/message-tones/communication-channel-519/download/mp3') ).play(); + + /* (5) Add messages to stack */ + gstore.data.notif.event.data = gstore.data.notif.event.data.concat( msg.msg ); + + /* (6) Add notification count if not already on page */ + if( router.app.$route.path != '/event' ) + gstore.data.notif.event.count += msg.msg.length; + + /* (7) Remove loader */ + gstore.data.msg_pending.event = false; + + +}).send({name: _SERVER.session.name}); + +/* (3) Message channel +---------------------------------------------------------*/ +window.wsc_chat = wsc.channel('chat').listen(function(msg, err){ + + /* (1) Manage error */ + if( msg == null && err != null ) + return infobox.show('Erreur de connexion WebSocket@chat ('+err+')', 'error', 3000); + + /* (2) Manage wsclient error */ + if( typeof msg.error != 'boolean' || msg.error !== false ) + return infobox.show('Erreur de connexion WebSocket@chat', 'warning', 3000); + + /* (3) If no message -> exit */ + if( msg.msg == null ) + return; + + /* (4) Play sound if 1msg received + not already on page */ + // note: 1msg means a new message but not the page load past buffer + if( msg.msg.length == 1 && router.app.$route.path != '/inbox' ) + ( new Audio('https://notificationsounds.com/message-tones/communication-channel-519/download/mp3') ).play(); + + /* (5) Add messages to stack */ + gstore.data.notif.inbox.data = gstore.data.notif.inbox.data.concat( msg.msg ); + + /* (6) Add notification count if not already on page */ + if( router.app.$route.path != '/inbox' ) + gstore.data.notif.inbox.count += msg.msg.length; + + /* (7) Remove loader */ + gstore.data.msg_pending.inbox = false; + + +}).send({name: _SERVER.session.name}); \ No newline at end of file