UNPKG

@genexus/web-standard-functions

Version:

GeneXus JavaScript standard functions library for web generators

53 lines 1.76 kB
import { WS } from "../websocket/ws"; import { publish } from "../pubSub/pubSub"; import * as log from "loglevel"; export default class SocketService { constructor() { this.logger = log.getLogger("SocketManagerService"); this.connections = {}; } static getInstance() { if (!SocketService.instance) { SocketService.instance = new SocketService(); } return SocketService.instance; } async open(url, id) { if (!this.connections[url]) { const connection = new WS(); this.connections[url] = connection; this.attachHandlers(id !== null ? id : url, connection); return connection.open(url); } return Promise.resolve(); } close(url) { const connection = this.connections[url]; if (connection) { delete this.connections[url]; connection.close(); } } async send(url, msg) { await this.open(url); this.connections[url].send(msg); } attachHandlers(id, connection) { connection.onOpen = (ev) => { publish(`${id}.socket.connected`, ev); publish(`GeneXus.Client.Socket.Connected`, ev); }; connection.onClose = (ev) => { publish(`${id}.socket.connectionclosed`, ev); }; connection.onError = (ev) => { publish(`${id}.socket.connectionfailed`, ev); publish(`GeneXus.Client.Socket.ConnectionFailed`, ev); }; connection.onMessage = (ev) => { publish(`${id}.socket.messagereceived`, ev.data); publish(`GeneXus.Client.Socket.MessageReceived`, ev.data); }; } } //# sourceMappingURL=socketService.js.map