UNPKG

swtc-lib

Version:

websocket access for jingtum blockchain

214 lines 7.12 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { EventEmitter } from "events"; import url from "url"; import WS from "ws"; class Server extends EventEmitter { constructor(remote, opts) { super(); this.setMaxListeners(0); if (typeof opts === "string") { const parsed = url.parse(opts); const secure = parsed.protocol === "wss:"; opts = { host: parsed.hostname, port: parsed.port ? Number(parsed.port) : secure ? 443 : 80, secure, path: parsed.path }; } if (opts === null || typeof opts !== "object") { this.opts = new TypeError("server options not supplied"); return this; } if (!Server.domainRE.test(opts.host)) { this.opts_host = new TypeError("server host incorrect"); return this; } if (Number.isNaN(opts.port) || !Number.isFinite(opts.port)) { this.port = new TypeError("server port not a number"); return this; } if (opts.port < 1 || opts.port > 65535) { this.port = new TypeError("server port out of range"); return this; } if (typeof opts.secure !== "boolean") { opts.secure = false; } this._opts = opts; this._url = (this._opts.secure ? "wss://" : "ws://") + this._opts.host + ":" + this._opts.port + (this._opts.path ? this._opts.path : ""); this._remote = remote; this._ws = null; this._connected = false; this._opened = false; this._state = "offline"; this._id = 0; this._timer = 0; } connect(callback) { if (this._connected) return; if (this._ws) this._ws.close(); try { this._ws = new WS(this._url); if ("open" in this._ws) { this._ws.open(); } } catch (e) { return callback(e); } this._ws.on("open", () => { this._opened = true; const req = this._remote.subscribe(["ledger", "server", "transactions"]); req.submit(callback); }); if ("open" in this._ws) { this._ws.on("message", (socket, data) => { this._remote._handleMessage(data); socket === null; }); } else { this._ws.on("message", data => { this._remote._handleMessage(data); }); } this._ws.on("close", () => { this._handleClose(); }); if ("open" in this._ws) { this._ws.on("error", (socket, err) => { callback(err); socket === null; }); } else { this._ws.on("error", err => callback(err)); } } connectPromise() { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { if (this._connected) { resolve(`this._server is connected already`); } if (this._ws) this._ws.close(); try { this._ws = new WS(this._url); if ("open" in this._ws) { this._ws.open(); } } catch (e) { reject(e); } this._ws.on("open", () => { this._opened = true; const req = this._remote.subscribe(["ledger", "server", "transactions"]); req .submitPromise() .then(result => resolve(result)) .catch(error => reject(error)); }); if ("open" in this._ws) { this._ws.on("message", (socket, data) => { this._remote._handleMessage(data); socket === null; }); } else { this._ws.on("message", data => { this._remote._handleMessage(data); }); } this._ws.on("close", () => { this._handleClose(); }); if ("open" in this._ws) { this._ws.on("error", (socket, err) => { reject(err); socket === null; }); } else { this._ws.on("error", err => reject(err)); } }); }); } disconnect() { this._ws.close(); this._ws = null; this._setState("offline"); } isConnected() { return this._connected; } sendMessage(command, data) { if (!this._opened) return; const req_id = this._id++; const msg = Object.assign({}, { id: req_id, command }, data); this._ws.send(JSON.stringify(msg)); return req_id; } _setState(state) { if (state === this._state) return; this._state = state; this._connected = state === "online"; if (!this._connected) { this._opened = false; } } _handleClose() { if (this._state === "offline") return; this._setState("offline"); if (this._timer !== 0) return; this._remote.emit("disconnect"); this._timer = setInterval(() => { this.connect((err, ret) => { if (err) { ret === 0; } else { clearInterval(this._timer); this._timer = 0; this._remote.emit("reconnect"); } }); }, 3000); } } Server.domainRE = /^(?=.{1,255}$)[0-9A-Za-z](?:(?:[0-9A-Za-z]|[-_]){0,61}[0-9A-Za-z])?(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|[-_]){0,61}[0-9A-Za-z])?)*\.?$/; Server.onlineStates = [ "syncing", "tracking", "proposing", "validating", "full", "connected" ]; export { Server }; //# sourceMappingURL=server.js.map