2017-11-29 11:29:27 +00:00
|
|
|
/* (1) Imports
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) NPM libs */
|
2017-11-28 12:37:49 +00:00
|
|
|
import Vue from 'vue'
|
2017-12-04 23:00:22 +00:00
|
|
|
import VueRouter from 'vue-router'
|
2017-11-29 11:29:27 +00:00
|
|
|
|
|
|
|
/* (2) Internal libs */
|
2017-12-04 23:00:22 +00:00
|
|
|
import {API} from './lib/api-es6'
|
|
|
|
import {InfoBox} from './lib/infobox-es6'
|
2017-11-29 19:36:30 +00:00
|
|
|
import {WSClient,WSClientBuilder} from './lib/ws-client-es6'
|
2017-12-04 23:00:22 +00:00
|
|
|
import routes from './routes'
|
2017-11-28 12:37:49 +00:00
|
|
|
|
2017-11-29 11:29:27 +00:00
|
|
|
/* (3) Vues */
|
2017-12-01 14:48:13 +00:00
|
|
|
import wrapper_vue from './vue/wrapper.vue'
|
2017-11-29 11:29:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (2) Initialisation
|
|
|
|
---------------------------------------------------------*/
|
2017-12-07 14:33:01 +00:00
|
|
|
/* (1) global store init */
|
2017-12-01 14:48:13 +00:00
|
|
|
require('./vue-config');
|
|
|
|
window.gstore.add('server', window._SERVER);
|
2017-12-05 17:41:39 +00:00
|
|
|
window.infobox = new InfoBox(gstore.data.info);
|
2017-12-01 14:48:13 +00:00
|
|
|
|
2017-12-07 14:33:01 +00:00
|
|
|
/* (2) API */
|
|
|
|
window.api = new API(gstore.data.is_local ? 'http://ndli1718/api/v/1.0/' : 'https://ndli1718.xdrm.io/api/v/1.0/');
|
|
|
|
|
|
|
|
/* (3) wsclient */
|
|
|
|
window.wsc = new WSClientBuilder(gstore.data.is_local ? 'ws://localhost:9999' : 'wss://websocket.xdrm.io');
|
|
|
|
|
|
|
|
|
2017-12-04 23:00:22 +00:00
|
|
|
/* (4) Init vue router */
|
|
|
|
Vue.use(VueRouter);
|
2017-12-05 07:39:24 +00:00
|
|
|
const router = new VueRouter({
|
2017-12-04 23:00:22 +00:00
|
|
|
mode: 'history',
|
|
|
|
routes: routes[0]
|
|
|
|
});
|
|
|
|
|
|
|
|
/* (5) Render view */
|
2017-11-28 12:37:49 +00:00
|
|
|
new Vue({
|
2017-12-05 07:39:24 +00:00
|
|
|
el: '#main-vue',
|
|
|
|
router,
|
2017-12-04 23:00:22 +00:00
|
|
|
render: h => h(wrapper_vue)
|
2017-12-05 07:39:24 +00:00
|
|
|
});
|
2017-12-04 10:22:10 +00:00
|
|
|
|
2017-12-05 21:30:03 +00:00
|
|
|
/* (6) Navigate at page load (to update notifications count) */
|
2017-12-05 17:55:07 +00:00
|
|
|
gstore.data.func.nav(router, null);
|
|
|
|
|
2017-12-04 10:22:10 +00:00
|
|
|
|
|
|
|
/* (3) Set WebSocket channels
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Connection channel */
|
2017-12-06 15:23:21 +00:00
|
|
|
window.wsc_connect = wsc.channel('connect').listen(function(msg, err){
|
2017-12-04 10:22:10 +00:00
|
|
|
|
|
|
|
// {1} Manage error //
|
2017-12-04 16:30:33 +00:00
|
|
|
if( msg == null && err != null )
|
2017-12-05 17:41:39 +00:00
|
|
|
return infobox.show('Erreur de connexion WebSocket@connect ('+err+')', 'error', 3000);
|
2017-12-04 10:22:10 +00:00
|
|
|
|
|
|
|
// {2} Manage wsclient error //
|
2017-12-04 16:30:33 +00:00
|
|
|
if( typeof msg.error != 'boolean' || msg.error !== false )
|
2017-12-05 17:41:39 +00:00
|
|
|
return infobox.show('Erreur de connexion WebSocket@connect', 'warning', 3000);
|
2017-12-04 10:22:10 +00:00
|
|
|
|
2017-12-06 15:23:21 +00:00
|
|
|
// {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;
|
2017-12-05 17:41:39 +00:00
|
|
|
gstore.data.notif[0].data = gstore.data.notif[0].data.concat( msg.connected );
|
2017-12-06 15:23:21 +00:00
|
|
|
|
|
|
|
// -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
|
2017-12-06 18:10:16 +00:00
|
|
|
if( router.app.$route.path != '/notifications' )
|
2017-12-06 15:23:21 +00:00
|
|
|
gstore.data.notif[0].count += gstore.data.notif[0].data.length - lastLen;
|
|
|
|
|
2017-12-04 16:30:33 +00:00
|
|
|
}
|
2017-12-04 10:22:10 +00:00
|
|
|
|
2017-12-06 15:23:21 +00:00
|
|
|
// {5} Add disconnected users to stack //
|
|
|
|
if( msg.disconnected instanceof Array ){
|
|
|
|
|
|
|
|
// -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
|
2017-12-06 18:10:16 +00:00
|
|
|
if( router.app.$route.path != '/notifications' )
|
2017-12-06 15:23:21 +00:00
|
|
|
gstore.data.notif[0].count += lastLen - gstore.data.notif[0].data.length;
|
2017-12-05 17:55:07 +00:00
|
|
|
|
2017-12-06 15:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}).send({name: _SERVER.session.name});
|
2017-12-04 10:22:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (2) Message channel */
|
2017-12-05 17:41:39 +00:00
|
|
|
window.wsc_chat = wsc.channel('chat').listen(function(msg, err){
|
2017-12-04 10:22:10 +00:00
|
|
|
|
|
|
|
// {1} Manage error //
|
2017-12-04 16:30:33 +00:00
|
|
|
if( msg == null && err != null )
|
2017-12-05 17:41:39 +00:00
|
|
|
return infobox.show('Erreur de connexion WebSocket@chat ('+err+')', 'error', 3000);
|
2017-12-04 10:22:10 +00:00
|
|
|
|
|
|
|
// {2} Manage wsclient error //
|
2017-12-04 16:30:33 +00:00
|
|
|
if( typeof msg.error != 'boolean' || msg.error !== false )
|
2017-12-05 17:41:39 +00:00
|
|
|
return infobox.show('Erreur de connexion WebSocket@chat', 'warning', 3000);
|
2017-12-04 10:22:10 +00:00
|
|
|
|
2017-12-06 15:23:21 +00:00
|
|
|
// {3} If no message -> exit //
|
|
|
|
if( msg.msg == null )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// {4} Play sound if 1msg received + not already on page //
|
2017-12-06 18:10:16 +00:00
|
|
|
// note: 1msg means a new message but not the page load past buffer
|
|
|
|
if( msg.msg.length == 1 && router.app.$route.path != '/inbox' )
|
2017-12-06 19:05:26 +00:00
|
|
|
( new Audio('https://notificationsounds.com/message-tones/communication-channel-519/download/mp3') ).play();
|
2017-12-04 10:22:10 +00:00
|
|
|
|
2017-12-06 15:23:21 +00:00
|
|
|
// {5} Add messages to stack //
|
|
|
|
gstore.data.notif[1].data = gstore.data.notif[1].data.concat( msg.msg );
|
2017-12-05 17:55:07 +00:00
|
|
|
|
2017-12-06 15:23:21 +00:00
|
|
|
// {6} Add notification count if not already on page //
|
2017-12-06 18:10:16 +00:00
|
|
|
if( router.app.$route.path != '/inbox' )
|
|
|
|
gstore.data.notif[1].count += msg.msg.length;
|
2017-12-05 17:55:07 +00:00
|
|
|
|
2017-12-06 17:23:48 +00:00
|
|
|
}).send({name: _SERVER.session.name});
|