UNPKG

@genexus/web-standard-functions

Version:

GeneXus JavaScript standard functions library for web generators

54 lines 2.04 kB
import SocketService from "../../web/socketService"; import { GeneXusSDNetwork } from "../sd/network"; import { GeneXusClientClientInformation } from "./client-information"; export var SocketStatus; (function (SocketStatus) { SocketStatus[SocketStatus["Disconnected"] = 0] = "Disconnected"; SocketStatus[SocketStatus["Connected"] = 1] = "Connected"; })(SocketStatus || (SocketStatus = {})); export class GeneXusClientSocket { /** * Connects to a Socket Server endpoint using the URL specified in the 'url' property * New messages will be published to '{url}.socket.messagereceived' event. * New opened connections will be published to '{url}.socket.connected' event. * Failed connections will be published to '{url}.socket.connectionfailed' event. * Closed connections will be published to '{url}.socket.connectionclosed' event. */ static async open(url) { this.url = resolveUrl(url); await this.socketService.open(this.url, url); this.status = SocketStatus.Connected; } /** * Closes the socket connection. */ static close() { this.status = SocketStatus.Disconnected; this.socketService.close(this.url); } /** * Sends the data to the currently connected Socket Server. * @param {any} data The data to send. */ static async send(data) { return this.socketService.send(this.url, data); } } GeneXusClientSocket.url = ""; GeneXusClientSocket.status = SocketStatus.Disconnected; GeneXusClientSocket.socketService = SocketService.getInstance(); const resolveUrl = (url) => { let wsUrl = url; if (!wsUrl) { wsUrl = GeneXusSDNetwork.applicationServerURL() .replace("http://", "ws://") .replace("https://", "wss://") + "gxwebsocket"; wsUrl = wsUrl.indexOf("?") < 0 ? wsUrl + "?" + GeneXusClientClientInformation.id() : wsUrl; } return wsUrl; }; //# sourceMappingURL=client-socket.js.map