main/view/main.js

80 lines
2.3 KiB
JavaScript
Raw Normal View History

/* (1) Imports
---------------------------------------------------------*/
/* (1) NPM libs */
2017-11-28 12:37:49 +00:00
import Vue from 'vue'
/* (2) Internal libs */
2017-11-28 16:18:56 +00:00
import {API} from './lib/api-es6'
import {InfoBox} from './lib/infobox-es6'
import {WSClient,WSClientBuilder} from './lib/ws-client-es6'
2017-11-28 12:37:49 +00:00
/* (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(window.gstore.data.info);
/* (4) Render view */
2017-11-28 12:37:49 +00:00
new Vue({
el: '#main-vue',
render: h => h(wrapper_vue),
data: {
gstore: window.gstore.data
}
2017-11-28 12:37:49 +00:00
})
/* (3) Set WebSocket channels
---------------------------------------------------------*/
/* (1) Connection channel */
window.wsc_connect = window.wsc.channel('connect').listen(function(msg, err){
// {1} Manage error //
if( msg == null && err != null )
return window.infobox.show('Erreur de connexion WebSocket@connect ('+err+')', 'error', 3000);
// {2} Manage wsclient error //
if( typeof msg.error != 'boolean' || msg.error !== false )
return window.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)');
window.gstore.data.notif[0].data = window.gstore.data.notif[0].data.concat( msg.connected );
}
}).send({name: window._SERVER.session.name});
/* (2) Message channel */
window.wsc_chat = window.wsc.channel('chat').listen(function(msg, err){
// {1} Manage error //
if( msg == null && err != null )
return window.infobox.show('Erreur de connexion WebSocket@chat ('+err+')', 'error', 3000);
// {2} Manage wsclient error //
if( typeof msg.error != 'boolean' || msg.error !== false )
return window.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)');
window.gstore.data.notif[1].data = window.gstore.data.notif[1].data.concat( msg.msg );
}
}).send({name: window._SERVER.session.name});