UNPKG

@sevenqi/nodechannel

Version:
60 lines 2.39 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.WebSocketDuplex = void 0; var stream_1 = require("stream"); var WebSocketDuplex = /** @class */ (function (_super) { __extends(WebSocketDuplex, _super); function WebSocketDuplex(ws) { var _this = _super.call(this, { readableObjectMode: true, writableObjectMode: true }) || this; _this.ws = ws; var $this = _this; _this.ws.on("message", function (msg, isBinary) { $this.push(msg.toString()); }); _this.ws.on("close", function () { _this.emit("close"); }); return _this; } WebSocketDuplex.prototype._write = function (chunk, encoding, callback) { this.ws.send(chunk); callback(); }; WebSocketDuplex.prototype._read = function (size) { this.resume(); }; WebSocketDuplex.prototype.connect = function () { var _this = this; return new Promise(function (resolve, reject) { var openListener = function (error) { _this.ws.removeListener("error", errorListener); resolve(true); }; var errorListener = function (error) { _this.ws.removeListener("open", openListener); console.log(error); reject(error); }; _this.ws.once("open", openListener); _this.ws.once("error", errorListener); }); }; return WebSocketDuplex; }(stream_1.Duplex)); exports.WebSocketDuplex = WebSocketDuplex; //# sourceMappingURL=webSocketDuplex.js.map