UNPKG

@bugbytes/hapi-connect

Version:

Lightweight HashConnect Browser Side Client Library

57 lines 1.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Relay = void 0; const ts_typed_events_1 = require("ts-typed-events"); class Relay { url; socket; emitConnected; emitDisconnected; emitReceived; received; connected; disconnected; get isOpen() { return this.socket.readyState === WebSocket.OPEN; } constructor(url = "wss://hashconnect.hashpack.app") { this.url = url; this.emitConnected = (0, ts_typed_events_1.createEmitter)(); this.emitDisconnected = (0, ts_typed_events_1.createEmitter)(); this.emitReceived = (0, ts_typed_events_1.createEmitter)(); this.received = this.emitReceived.event; this.connected = this.emitConnected.event; this.disconnected = this.emitDisconnected.event; this.connect(); } send(message) { this.socket.send(message); } connect() { const socket = new WebSocket(this.url); socket.onopen = this.onOpen.bind(this); socket.onmessage = this.onMessage.bind(this); socket.onerror = this.onError.bind(this); socket.onclose = this.onClose.bind(this); this.socket = socket; } onOpen() { this.emitConnected(); } onMessage(message) { this.emitReceived(message.data); } onClose() { this.emitDisconnected(); this.socket.onopen = null; this.socket.onmessage = null; this.socket.onerror = null; this.socket.onclose = null; this.connect(); } onError(err) { console.error(err); } } exports.Relay = Relay; //# sourceMappingURL=relay.js.map