2018-03-24 14:13:27 +00:00
|
|
|
import ClientDriver from './client-driver.js'
|
2018-03-23 17:43:59 +00:00
|
|
|
|
2018-03-24 14:13:27 +00:00
|
|
|
export default class WebSocketClientDriver extends ClientDriver{
|
2018-03-23 17:43:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (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-03-23 17:43:59 +00:00
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
2018-04-03 13:37:18 +00:00
|
|
|
constructor(_resource, _auth={token:null}){
|
2018-03-23 17:43:59 +00:00
|
|
|
|
|
|
|
/* (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 };
|
2018-03-23 17:43:59 +00:00
|
|
|
|
2018-04-04 09:19:31 +00:00
|
|
|
/* (2) When token response is OK */
|
|
|
|
this.valid_token = false;
|
|
|
|
|
|
|
|
/* (3) Create useful attributes */
|
2018-03-23 17:43:59 +00:00
|
|
|
this.ws = null;
|
|
|
|
this.buffer = null; // useful when waiting for WebSocket to open
|
|
|
|
|
2018-04-04 09:19:31 +00:00
|
|
|
/* (4) Manage connection opened */
|
|
|
|
this.event.onconnected = function(){
|
|
|
|
|
|
|
|
// update state
|
|
|
|
this.state = ClientDriver.STATE.CONNECTED;
|
|
|
|
|
|
|
|
// send token
|
|
|
|
this.ws.send(JSON.stringify({ token: this.auth.token }));
|
2018-04-03 13:37:18 +00:00
|
|
|
|
2018-04-04 09:19:31 +00:00
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
|
|
|
|
/* (5) Manage response received */
|
2018-04-03 13:37:18 +00:00
|
|
|
this.event.onreceive = function(_response){
|
|
|
|
|
2018-04-04 09:19:31 +00:00
|
|
|
/* (1) set state from TRANSFERING to CONNECTED */
|
2018-04-03 13:37:18 +00:00
|
|
|
this.state = ClientDriver.STATE.CONNECTED;
|
|
|
|
|
2018-04-04 09:19:31 +00:00
|
|
|
/* (2) Try to parse JSON */
|
|
|
|
var obj_resp = null;
|
|
|
|
try{ obj_resp = JSON.parse(_response); }catch(e){}
|
|
|
|
|
|
|
|
/* (3) JSON error -> send null */
|
|
|
|
if( obj_resp === null )
|
|
|
|
this.callback.onreceive( null );
|
|
|
|
|
|
|
|
/* (4) if TOKEN VALIDATION */
|
|
|
|
if( typeof obj_resp.error === 'number' ){
|
|
|
|
|
|
|
|
// invalid token -> tell connection closed
|
|
|
|
if( obj_resp.error !== 0 )
|
|
|
|
return this.event.onclose();
|
|
|
|
|
|
|
|
// valid token -> connected
|
|
|
|
this.callback.onconnected();
|
|
|
|
|
|
|
|
// if request(s) in stack -> pop & send them
|
|
|
|
while( this.stack.length > 0 )
|
|
|
|
this.send(this.stack.shift());
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (5) Else -> pass message to callback */
|
|
|
|
this.callback.onreceive(obj_resp);
|
2018-04-03 13:37:18 +00:00
|
|
|
|
|
|
|
}.bind(this)
|
|
|
|
|
2018-03-23 17:43:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (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 */
|
2018-03-24 14:13:27 +00:00
|
|
|
this.ws.onopen = this.event.onconnected;
|
2018-03-23 17:43:59 +00:00
|
|
|
|
|
|
|
/* (3) Bind callback.onclose */
|
2018-03-24 14:13:27 +00:00
|
|
|
this.ws.onclose = this.event.onclose;
|
2018-03-23 17:43:59 +00:00
|
|
|
|
|
|
|
/* (4) Bind callback.onerror */
|
2018-03-24 14:13:27 +00:00
|
|
|
this.ws.onerror = this.event.onclose;
|
2018-03-23 17:43:59 +00:00
|
|
|
|
|
|
|
/* (5) Bind callback.onreceive */
|
2018-03-24 14:13:27 +00:00
|
|
|
this.ws.onmessage = (_message_event) => this.event.onreceive(_message_event.data);
|
2018-03-23 17:43:59 +00:00
|
|
|
|
2018-04-03 13:37:18 +00:00
|
|
|
|
2018-03-23 17:43:59 +00:00
|
|
|
/* (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-04 09:19:31 +00:00
|
|
|
var buffer = null;
|
2018-04-03 13:37:18 +00:00
|
|
|
|
2018-03-23 17:43:59 +00:00
|
|
|
/* (0) Parent check */
|
|
|
|
if( !super.send(_request) )
|
|
|
|
return false;
|
|
|
|
|
2018-04-03 13:37:18 +00:00
|
|
|
this.state = ClientDriver.STATE.CONNECTED;
|
|
|
|
|
2018-03-23 17:43:59 +00:00
|
|
|
/* (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
|
2018-03-23 17:43:59 +00:00
|
|
|
return false;
|
2018-04-03 13:37:18 +00:00
|
|
|
|
2018-03-23 17:43:59 +00:00
|
|
|
|
|
|
|
/* (2) Send message */
|
2018-04-03 13:37:18 +00:00
|
|
|
this.ws.send(buffer);
|
2018-03-23 17:43:59 +00:00
|
|
|
|
|
|
|
/* (3) Return success */
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-24 14:13:27 +00:00
|
|
|
/* (3) Closes the connection
|
2018-03-23 17:43:59 +00:00
|
|
|
*
|
2018-03-24 14:13:27 +00:00
|
|
|
* @return closed<boolean> Whether the connection has been closed
|
2018-03-23 17:43:59 +00:00
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
2018-03-24 14:13:27 +00:00
|
|
|
close(){
|
2018-03-23 17:43:59 +00:00
|
|
|
|
|
|
|
/* (0) Parent check */
|
2018-03-24 14:13:27 +00:00
|
|
|
if( !super.close() )
|
2018-03-23 17:43:59 +00:00
|
|
|
return false;
|
|
|
|
|
2018-03-24 14:13:27 +00:00
|
|
|
/* (1) Close websocket */
|
|
|
|
this.ws.close();
|
2018-03-23 17:43:59 +00:00
|
|
|
|
2018-03-24 14:13:27 +00:00
|
|
|
/* (2) Return success */
|
|
|
|
return true;
|
2018-03-23 17:43:59 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|