Added websockets: connection tracker, chat messenger to VueJS

This commit is contained in:
xdrm-brackets 2017-12-04 11:22:10 +01:00
parent b66bafb3c3
commit e81817b819
4 changed files with 57 additions and 3 deletions

View File

@ -25,7 +25,9 @@
echo "window._SERVER = ".json_encode([
'a' => [1, 2]
'session' => [
'name' => $_SESSION['NAME']
]
])."\n";

View File

@ -8,7 +8,12 @@
/* (1) Start session */
session_start();
// $_SESSION['PERM'] = ['journalist'];
if( count($_SESSION['USER']) > 0 )
$_SESSION['NAME'] = $_SESSION['USER']['username'];
elseif( count($_SESSION['ADMIN']) > 0 )
$_SESSION['NAME'] = $_SESSION['ADMIN']['username'];
elseif( !isset($_SESSION['NAME']) || strlen($_SESSION['NAME']) == 0 )
$_SESSION['NAME'] = 'guest'.uniqid();
/* (2) Set default Driver for Repos */
Repo::setDriver(DatabaseDriver::get());

View File

@ -31,3 +31,50 @@ new Vue({
gstore: window.gstore.data
}
})
/* (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 ){
console.warn(err);
/*TEMP*///return;
/*TEMP*/msg = {error: 0, connected: ['guest123', 'guest456', 'guest789']};
}
// {2} Manage wsclient error //
if( msg.connected == null || msg.error == null || msg.error !== 0 ){
console.warn('websocket error: '+msg.error);
return;
}
// {3} Manage notification //
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 ){
console.warn(err);
/*TEMP*///return;
/*TEMP*/msg = {error: 0, msg: [['guest123', 'message 1'], ['guest456', 'message 2'], ['guest789', 'message 3']]};
}
// {2} Manage wsclient error //
if( msg.msg == null || msg.error == null || msg.error !== 0 ){
console.warn('websocket error: '+msg.error);
return;
}
// {3} Manage notification //
window.gstore.data.notif[1].data = window.gstore.data.notif[1].data.concat( msg.msg );
}).send({name: window._SERVER.session.name});

View File

@ -11,7 +11,7 @@ window.gstore.add('info', {
});
window.gstore.add('notif', [
{ class: 'bell', data: ['guest1224934 connected', 'guest2349329042 connected'] },
{ class: 'message', data: ['guestsdfljd: \"blabla bloblo\"'] },
{ class: 'message', data: [['guestsdfljd, \"blabla bloblo\"']] },
{ class: 'search', data: [] },
{ class: 'menu', data: [] }
])