main/view/main.js

105 lines
2.9 KiB
JavaScript
Executable File

/* (1) Imports
---------------------------------------------------------*/
/* (1) NPM libs */
import Vue from 'vue'
import VueRouter from 'vue-router'
/* (2) Internal libs */
import {API} from './lib/api-es6'
import {InfoBox} from './lib/infobox-es6'
import {WSClient,WSClientBuilder} from './lib/ws-client-es6'
import routes from './routes'
/* (3) Vues */
import wrapper_vue from './vue/wrapper.vue'
/* (2) Initialisation
---------------------------------------------------------*/
/* (1) API */
window.api = new API("http://ndli1718/api/v/1.0/");
/* (2) wsclient */
window.wsc = new WSClientBuilder("wss://websocket.xdrm.io");
/* (3) global store init */
require('./vue-config');
window.gstore.add('server', window._SERVER);
window.infobox = new InfoBox(gstore.data.info);
/* (4) Init vue router */
Vue.use(VueRouter);
const router = new VueRouter({
mode: 'history',
routes: routes[0]
});
/* (5) Render view */
new Vue({
el: '#main-vue',
router,
render: h => h(wrapper_vue)
});
gstore.data.func.nav(router, null);
/* (3) Set WebSocket channels
---------------------------------------------------------*/
/* (1) Connection channel */
window.wsc_connect = wsc.channel('connect').listen(function(msg, err){
// {1} Manage error //
if( msg == null && err != null )
return infobox.show('Erreur de connexion WebSocket@connect ('+err+')', 'error', 3000);
// {2} Manage wsclient error //
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)');
gstore.data.notif[0].data = gstore.data.notif[0].data.concat( msg.connected );
gstore.data.notif[0].count += msg.connected.length;
}
// {4} Reset notification count if already on page //
gstore.data.func.nav(router, null);
}).send({name: _SERVER.session.name});
/* (2) 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} 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 );
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});
/* (4) Clean sockets before page quit
---------------------------------------------------------*/
window.onbeforeunload = function() {
wsc_chat.send('{"close": true}');
wsc_connect.send('{"close": true}');
};