dlovely-websocket
Version:
WebSocket For Dlovely
44 lines (43 loc) • 1.72 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OutStream = void 0;
const stream_1 = __importDefault(require("stream"));
const frame_1 = require("./frame");
class OutStream extends stream_1.default.Writable {
connection;
minSize;
constructor(connection, minSize) {
super();
this.connection = connection;
this.minSize = minSize;
this.on('finish', () => {
if (this.connection.readyState === this.connection.OPEN) {
this.connection.socket.write((0, frame_1.createBinaryFrame)(this.buffer, !this.connection.server, !this.hasSent, true));
}
this.connection.outStream = undefined;
});
}
buffer = Buffer.alloc(0);
hasSent = false;
_write(chunk, encoding, callback) {
this.buffer = Buffer.concat([this.buffer, chunk], this.buffer.length + chunk.length);
if (this.buffer.length >= this.minSize) {
if (this.connection.readyState === this.connection.OPEN) {
const frameBuffer = (0, frame_1.createBinaryFrame)(this.buffer, !this.connection.server, !this.hasSent, false);
this.connection.socket.write(frameBuffer, encoding, callback);
}
this.buffer = Buffer.alloc(0);
this.hasSent = true;
if (this.connection.readyState !== this.connection.OPEN) {
callback();
}
}
else {
callback();
}
}
}
exports.OutStream = OutStream;