"use strict"; import {Event} from "../Application/Event.ecma.js"; import {Check} from "../Utils/Checks.ecma.js"; import {Common} from "../Utils/Common.ecma.js"; /** * @typedef {import("../Application/AnP.ecma.js").AnP} AnP */ /** * @class WebSocketsClientsAbstract * @constructor * @param {!AnP} anp * @param {!string} key * @param {!(Object.|Array.)} inputs * @return {void} * @access public * @static */ export const WebSocketsClientsAbstract = (function(){ /** * @constructs WebSocketsClientsAbstract * @param {!AnP} anp * @param {!string} key * @param {!(Object.|Array.)} inputs * @return {void} * @access private * @static */ const WebSocketsClientsAbstract = function(anp, key, inputs){ /** @type {WebSocketsClientsAbstract|Object.} */ let self = this; /** @type {string|null} */ this.key = null; /** @type {string|null} */ this.url = null; /** @type {string|null} */ this.id = null; /** @type {Event} */ this.on_open = new Event(); /** @type {Event} */ this.on_message = new Event(); /** @type {Event} */ this.on_close = new Event(); /** @type {Event} */ this.on_error = new Event(); /** * @param {!Object.} item * @returns {void} * @access public */ this.change_self = item => { self = item; }; /** * @return {void} * @access private */ const constructor = () => { self.key = key; self.url = Common.get_value("url", ( Check.is_string(inputs) ? inputs = {url : inputs} : inputs), ""); self.on_open.add(on_opened); self.on_message.add(on_received); self.on_close.add(on_closed); self.on_error.add(on_errored); }; /** * @returns {void} * @access private */ const on_opened = () => {}; /** * @param {!string} inputs * @returns {void} * @access private */ const on_received = inputs => { /** @type {Object.} */ const data = Common.data_decode(inputs); switch(data.controller + "." + data.action){ case "web_socket_client.set_id": self.id = data.data; break; default: anp.controllers.execute(data.controller, data.action, data, data.code); break; }; }; /** * @param {!string} controller * @param {!string} action * @param {?any} [data = null] * @param {!number} [code = 200] * @return {void} * @access public */ this.send = (controller, action, data, code = 200) => {}; /** * @return {void} * @access public */ this.close = () => {}; /** * @returns {void} * @access private */ const on_closed = () => { self.id = null; }; /** * @param {!string} error * @returns {void} * @access private */ const on_errored = error => { console.error(error); self.close(); }; constructor(); }; return WebSocketsClientsAbstract; })();