From e0a662a410fed12cab52d4cdc0e4e46b009844dc Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 3 Apr 2018 15:37:18 +0200 Subject: [PATCH] [lib.*] sending message + to refact --- webpack/lib/channel-controller.js | 13 +++++++++ webpack/lib/client/client-driver.js | 9 ++++-- webpack/lib/client/ws.js | 43 +++++++++++++++++++++++++---- webpack/lib/content-controller.js | 40 ++++++++++++++++++++++++++- 4 files changed, 96 insertions(+), 9 deletions(-) diff --git a/webpack/lib/channel-controller.js b/webpack/lib/channel-controller.js index aef4306..4d6fb67 100644 --- a/webpack/lib/channel-controller.js +++ b/webpack/lib/channel-controller.js @@ -47,6 +47,19 @@ export default class ChannelController{ /* (5) Load rooms */ gs.get.room.fetch(); + /* (6) Open channel websocket */ + window.csock = new wscd(`wss://ws.douscord.xdrm.io/channel/${channel.id}`, { token: auth.token }); + csock.bind(); + csock.onreceive = (_dat) => { + + if( _dat.error !== 0 ) + return console.log('[WS] auth failed'); + + csock.send({ buffer: { rid: gs.get.content.rid } }); + + csock.onreceive = gs.get.content.manage_update; + }; + /* (6) Log channel */ console.log(`[channel.current] ${channel.link} (${channel.label})`); diff --git a/webpack/lib/client/client-driver.js b/webpack/lib/client/client-driver.js index 46d4f8e..43e6612 100644 --- a/webpack/lib/client/client-driver.js +++ b/webpack/lib/client/client-driver.js @@ -168,12 +168,15 @@ export default class ClientDriver{ return false; /* (5) READY -> stack message + bind() [in case connection failed)] */ - if( state === ClientDriver.STATE.READY ) - return ( this.stack.push(_request) > -1 ) && this.bind(); + if( state === ClientDriver.STATE.READY ){ + this.stack.push(_request); + this.bind(); + return false; + } /* (6) CONNECTING -> stack message */ if( state === ClientDriver.STATE.CONNECTING ) - return ( this.stack.push(_request) > -1 ); + return ( this.stack.push(_request) < -1 ); /* (7) update state */ this.state = ClientDriver.STATE.TRANFERING; diff --git a/webpack/lib/client/ws.js b/webpack/lib/client/ws.js index 243b861..5e0efa8 100644 --- a/webpack/lib/client/ws.js +++ b/webpack/lib/client/ws.js @@ -6,9 +6,15 @@ export default class WebSocketClientDriver extends ClientDriver{ /* (1) Creates a client driver * * @_resource Target resource (typically an URL) + * @_auth Authentication object + * + * [fornat::AuthObject] + * { + * token: string + * } * ---------------------------------------------------------*/ - constructor(_resource){ + constructor(_resource, _auth={token:null}){ /* (0) Parent check */ if( !super().error ) @@ -16,11 +22,25 @@ export default class WebSocketClientDriver extends ClientDriver{ /* (1) Set inherited attributes */ this.resource = _resource; + let tmp = typeof _auth !== 'object' || _auth['token'] == null || typeof _auth.token !== 'string'; + this.auth = (tmp) ? { token: null } : { token: _auth.token }; /* (2) Create useful attributes */ this.ws = null; this.buffer = null; // useful when waiting for WebSocket to open + + /* (3) Manage response received */ + this.event.onreceive = function(_response){ + + // set state from TRANSFERING to CONNECTED + this.state = ClientDriver.STATE.CONNECTED; + + // call callback + this.callback.onreceive( JSON.parse(_response) ); + + }.bind(this) + } @@ -50,6 +70,10 @@ export default class WebSocketClientDriver extends ClientDriver{ /* (5) Bind callback.onreceive */ this.ws.onmessage = (_message_event) => this.event.onreceive(_message_event.data); + /* (6) Send authentication token */ + this.send({ buffer: { token: this.auth.token } }); + + /* (6) Return success */ return true; @@ -65,18 +89,27 @@ export default class WebSocketClientDriver extends ClientDriver{ ---------------------------------------------------------*/ send(_request){ + var buffer = '{ "token": null }'; + /* (0) Parent check */ if( !super.send(_request) ) return false; + this.state = ClientDriver.STATE.CONNECTED; + /* (1) Error: invalid _request.buffer */ - if( typeof _request.buffer !== 'string' ){ - this.state = ClientDriver.STATE.CONNECTED; + if( typeof _request.buffer === 'object' ) + buffer = JSON.stringify(_request.buffer); + + else if( typeof _request.buffer === 'string' ) + buffer = _request.buffer; + + else return false; - } + /* (2) Send message */ - this.ws.send(_request.buffer); + this.ws.send(buffer); /* (3) Return success */ return true; diff --git a/webpack/lib/content-controller.js b/webpack/lib/content-controller.js index 1f748b6..02e1545 100644 --- a/webpack/lib/content-controller.js +++ b/webpack/lib/content-controller.js @@ -131,13 +131,51 @@ export default class ContentController{ return true; /* (2) Send message */ - console.log(`SEND: "${_msg}"`); + window.csock.send({ buffer: { + rid: this.rid, + mid: null, + message: _msg + }}); return true; } + /* (9) MAIN UPDATER + * + ---------------------------------------------------------*/ + manage_update(_dat){ + + for( let r in _dat.room ){ + + if( _dat.room[r] === null ) + continue; + + + // create new room + for( let r2 in gs.get.room.text.list ){ + + if( gs.get.room.text.list[r2].id === r ) + return console.log('CREATE ROOM'); + + for( let m of _dat.room[r].messages ) + + gs.get.room.text.list[r2].messages.push({ + uid: m.uid, + mid: m.mid, + msg: m.content, + ts: m.ts + }) + + } + + + } + + } + + } \ No newline at end of file