173 lines
4.2 KiB
JavaScript
173 lines
4.2 KiB
JavaScript
"use strict";
|
|
|
|
import {WebSocketsClientsAbstract} from "../Abstracts/WebSocketsClientsAbstract.ecma.js";
|
|
import {Common} from "../Utils/Common.ecma.js";
|
|
import {Event} from "../Application/Event.ecma.js";
|
|
|
|
/**
|
|
* @typedef {import("../Application/AnP.ecma.js").AnP} AnP
|
|
*/
|
|
|
|
/**
|
|
* @class WebSocketsDriver
|
|
* @constructor
|
|
* @extends WebSocketsClientsAbstract
|
|
* @param {!AnP} anp
|
|
* @param {!string} key
|
|
* @param {!(Object.<string, any|null>|Array.<any|null>)} inputs
|
|
* @returns {void}
|
|
* @access public
|
|
* @static
|
|
*/
|
|
export const WebSocketsClientsDriver = (function(){
|
|
|
|
/**
|
|
* @callback continue_callback
|
|
* @param {boolean} ok
|
|
* @return {void}
|
|
*/
|
|
|
|
/**
|
|
* @constructs WebSocketsClientsDriver
|
|
* @extends WebSocketsClientsAbstract
|
|
* @param {!AnP} anp
|
|
* @param {!string} key
|
|
* @param {!(Object.<string, any|null>|Array.<any|null>)} inputs
|
|
* @returns {void}
|
|
* @access private
|
|
* @static
|
|
*/
|
|
const WebSocketsClientsDriver = function(anp, key, inputs){
|
|
|
|
/** @type {WebSocketsClientsDriver} */
|
|
const self = this;
|
|
/** @type {WebSocketsClientsAbstract|null} */
|
|
let _super = null,
|
|
/** @type {WebSocket|null} */
|
|
client = null,
|
|
/** @type {boolean} */
|
|
started = false;
|
|
|
|
/**
|
|
* @returns {void}
|
|
* @access private
|
|
*/
|
|
const constructor = () => {
|
|
|
|
Common.extends(self, _super = new WebSocketsClientsAbstract(anp, key, inputs), false);
|
|
|
|
Common.get_value(["web_socket_autostart", "autostart"], inputs, true) && self.start();
|
|
|
|
};
|
|
|
|
/**
|
|
* @param {?continue_callback} callback
|
|
* @returns {boolean}
|
|
* @access public
|
|
*/
|
|
this.start = (callback = null) => {
|
|
|
|
/** @type {continue_callback} */
|
|
const end = ok => Common.execute(callback, ok);
|
|
|
|
if(started){
|
|
end(false);
|
|
return false;
|
|
};
|
|
started = true;
|
|
|
|
client = new WebSocket(self.url);
|
|
|
|
client.onopen = on_open;
|
|
client.onmessage = on_message
|
|
client.onerror = on_error;
|
|
client.onclose = on_close;
|
|
|
|
end(true);
|
|
|
|
return true;
|
|
};
|
|
|
|
/**
|
|
* @param {?continue_callback} callback
|
|
* @returns {boolean}
|
|
* @access public
|
|
*/
|
|
this.close = (callback = null) => {
|
|
|
|
/** @type {continue_callback} */
|
|
const end = ok => Common.execute(callback, ok);
|
|
|
|
if(!started){
|
|
end(false);
|
|
return false;
|
|
};
|
|
started = false;
|
|
|
|
client.close();
|
|
client = null;
|
|
|
|
return true;
|
|
};
|
|
|
|
/**
|
|
* @param {!Event} _
|
|
* @returns {void}
|
|
* @access private
|
|
*/
|
|
const on_open = _ => {
|
|
self.on_open.execute();
|
|
};
|
|
|
|
/**
|
|
* @param {!Event} event
|
|
* @returns {void}
|
|
* @access private
|
|
*/
|
|
const on_message = event => {
|
|
self.on_message.execute(event.data);
|
|
};
|
|
|
|
/**
|
|
* @param {!Event} event
|
|
* @returns {void}
|
|
* @access private
|
|
*/
|
|
const on_error = event => {
|
|
self.on_error.execute(event.error);
|
|
};
|
|
|
|
/**
|
|
* @param {!Event} _
|
|
* @returns {void}
|
|
* @access private
|
|
*/
|
|
const on_close = _ => {
|
|
self.on_close.execute();
|
|
};
|
|
|
|
/**
|
|
* @param {!string} controller
|
|
* @param {!string} action
|
|
* @param {?any} [data = null]
|
|
* @param {!number} [code = 200]
|
|
* @return {void}
|
|
* @access public
|
|
*/
|
|
this.send = (controller, action, data = null, code = 200) => {
|
|
client.send(Common.data_encode({
|
|
ok : code >= 200 && code < 300,
|
|
code : code,
|
|
controller : controller,
|
|
action : action,
|
|
data : data,
|
|
id : _super.id
|
|
}));
|
|
};
|
|
|
|
constructor();
|
|
|
|
};
|
|
|
|
return WebSocketsClientsDriver;
|
|
})(); |