[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 */
|
/* (5) Load rooms */
|
||||||
gs.get.room.fetch();
|
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 */
|
/* (6) Log channel */
|
||||||
console.log(`[channel.current] ${channel.link} (${channel.label})`);
|
console.log(`[channel.current] ${channel.link} (${channel.label})`);
|
||||||
|
|
||||||
|
|
|
@ -168,12 +168,15 @@ export default class ClientDriver{
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
/* (5) READY -> stack message + bind() [in case connection failed)] */
|
/* (5) READY -> stack message + bind() [in case connection failed)] */
|
||||||
if( state === ClientDriver.STATE.READY )
|
if( state === ClientDriver.STATE.READY ){
|
||||||
return ( this.stack.push(_request) > -1 ) && this.bind();
|
this.stack.push(_request);
|
||||||
|
this.bind();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/* (6) CONNECTING -> stack message */
|
/* (6) CONNECTING -> stack message */
|
||||||
if( state === ClientDriver.STATE.CONNECTING )
|
if( state === ClientDriver.STATE.CONNECTING )
|
||||||
return ( this.stack.push(_request) > -1 );
|
return ( this.stack.push(_request) < -1 );
|
||||||
|
|
||||||
/* (7) update state */
|
/* (7) update state */
|
||||||
this.state = ClientDriver.STATE.TRANFERING;
|
this.state = ClientDriver.STATE.TRANFERING;
|
||||||
|
|
|
@ -6,9 +6,15 @@ export default class WebSocketClientDriver extends ClientDriver{
|
||||||
/* (1) Creates a client driver
|
/* (1) Creates a client driver
|
||||||
*
|
*
|
||||||
* @_resource<String> Target resource (typically an URL)
|
* @_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 */
|
/* (0) Parent check */
|
||||||
if( !super().error )
|
if( !super().error )
|
||||||
|
@ -16,11 +22,25 @@ export default class WebSocketClientDriver extends ClientDriver{
|
||||||
|
|
||||||
/* (1) Set inherited attributes */
|
/* (1) Set inherited attributes */
|
||||||
this.resource = _resource;
|
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 */
|
/* (2) Create useful attributes */
|
||||||
this.ws = null;
|
this.ws = null;
|
||||||
this.buffer = null; // useful when waiting for WebSocket to open
|
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 */
|
/* (5) Bind callback.onreceive */
|
||||||
this.ws.onmessage = (_message_event) => this.event.onreceive(_message_event.data);
|
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 */
|
/* (6) Return success */
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
@ -65,18 +89,27 @@ export default class WebSocketClientDriver extends ClientDriver{
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
send(_request){
|
send(_request){
|
||||||
|
|
||||||
|
var buffer = '{ "token": null }';
|
||||||
|
|
||||||
/* (0) Parent check */
|
/* (0) Parent check */
|
||||||
if( !super.send(_request) )
|
if( !super.send(_request) )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
/* (1) Error: invalid _request.buffer */
|
|
||||||
if( typeof _request.buffer !== 'string' ){
|
|
||||||
this.state = ClientDriver.STATE.CONNECTED;
|
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;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
/* (2) Send message */
|
/* (2) Send message */
|
||||||
this.ws.send(_request.buffer);
|
this.ws.send(buffer);
|
||||||
|
|
||||||
/* (3) Return success */
|
/* (3) Return success */
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -131,13 +131,51 @@ export default class ContentController{
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
/* (2) Send message */
|
/* (2) Send message */
|
||||||
console.log(`SEND: "${_msg}"`);
|
window.csock.send({ buffer: {
|
||||||
|
rid: this.rid,
|
||||||
|
mid: null,
|
||||||
|
message: _msg
|
||||||
|
}});
|
||||||
|
|
||||||
return true;
|
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