From e81817b8191845c819211e9d247d47859b6303a5 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 4 Dec 2017 11:22:10 +0100 Subject: [PATCH] Added websockets: connection tracker, chat messenger to VueJS --- build/router/controller/js.php | 4 ++- public_html/index.php | 7 ++++- view/main.js | 47 ++++++++++++++++++++++++++++++++++ view/vue-config.js | 2 +- 4 files changed, 57 insertions(+), 3 deletions(-) diff --git a/build/router/controller/js.php b/build/router/controller/js.php index e96d9a4..263b708 100644 --- a/build/router/controller/js.php +++ b/build/router/controller/js.php @@ -25,7 +25,9 @@ echo "window._SERVER = ".json_encode([ - 'a' => [1, 2] + 'session' => [ + 'name' => $_SESSION['NAME'] + ] ])."\n"; diff --git a/public_html/index.php b/public_html/index.php index 2c46efa..f1270f9 100755 --- a/public_html/index.php +++ b/public_html/index.php @@ -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()); diff --git a/view/main.js b/view/main.js index 4547727..81b12ad 100755 --- a/view/main.js +++ b/view/main.js @@ -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}); \ No newline at end of file diff --git a/view/vue-config.js b/view/vue-config.js index 4b86205..14f115d 100644 --- a/view/vue-config.js +++ b/view/vue-config.js @@ -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: [] } ])