discord-client/webpack/lib/client/ws.js

141 lines
2.9 KiB
JavaScript
Raw Normal View History

import ClientDriver from './client-driver.js'
export default class WebSocketClientDriver extends ClientDriver{
/* (1) Creates a client driver
*
* @_resource<String> Target resource (typically an URL)
2018-04-03 13:37:18 +00:00
* @_auth<AuthObject> Authentication object
*
* [fornat::AuthObject]
* {
* token: string
* }
*
---------------------------------------------------------*/
2018-04-03 13:37:18 +00:00
constructor(_resource, _auth={token:null}){
/* (0) Parent check */
if( !super().error )
return;
/* (1) Set inherited attributes */
this.resource = _resource;
2018-04-03 13:37:18 +00:00
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
2018-04-03 13:37:18 +00:00
/* (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)
}
/* (2) Binds the client to the resource
*
* @return bound<boolean> Whether the binding has been successful
*
---------------------------------------------------------*/
bind(){
/* (0) Parent check */
if( !super.bind() )
return false;
/* (1) Create WebSocket client instance */
this.ws = new WebSocket(this.resource);
/* (2) Bind callback.onready */
this.ws.onopen = this.event.onconnected;
/* (3) Bind callback.onclose */
this.ws.onclose = this.event.onclose;
/* (4) Bind callback.onerror */
this.ws.onerror = this.event.onclose;
/* (5) Bind callback.onreceive */
this.ws.onmessage = (_message_event) => this.event.onreceive(_message_event.data);
2018-04-03 13:37:18 +00:00
/* (6) Send authentication token */
this.send({ buffer: { token: this.auth.token } });
/* (6) Return success */
return true;
}
/* (3) Send request
*
* @_request<Object> Request data
*
* @return sent<boolean> Whether the request has been successful
*
---------------------------------------------------------*/
send(_request){
2018-04-03 13:37:18 +00:00
var buffer = '{ "token": null }';
/* (0) Parent check */
if( !super.send(_request) )
return false;
2018-04-03 13:37:18 +00:00
this.state = ClientDriver.STATE.CONNECTED;
/* (1) Error: invalid _request.buffer */
2018-04-03 13:37:18 +00:00
if( typeof _request.buffer === 'object' )
buffer = JSON.stringify(_request.buffer);
else if( typeof _request.buffer === 'string' )
buffer = _request.buffer;
else
return false;
2018-04-03 13:37:18 +00:00
/* (2) Send message */
2018-04-03 13:37:18 +00:00
this.ws.send(buffer);
/* (3) Return success */
return true;
}
/* (3) Closes the connection
*
* @return closed<boolean> Whether the connection has been closed
*
---------------------------------------------------------*/
close(){
/* (0) Parent check */
if( !super.close() )
return false;
/* (1) Close websocket */
this.ws.close();
/* (2) Return success */
return true;
}
}