[lib.*] sending message + to refact
This commit is contained in:
parent
7328645735
commit
e0a662a410
|
@ -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})`);
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -6,9 +6,15 @@ export default class WebSocketClientDriver extends ClientDriver{
|
|||
/* (1) Creates a client driver
|
||||
*
|
||||
* @_resource<String> Target resource (typically an URL)
|
||||
* @_auth<AuthObject> 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;
|
||||
|
||||
/* (1) Error: invalid _request.buffer */
|
||||
if( typeof _request.buffer !== 'string' ){
|
||||
this.state = ClientDriver.STATE.CONNECTED;
|
||||
|
||||
/* (1) Error: invalid _request.buffer */
|
||||
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;
|
||||
|
|
|
@ -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
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue