UNPKG

pinusmod

Version:

[![Build Status](https://travis-ci.org/node-pinus/pinus.svg?branch=master)](https://travis-ci.org/node-pinus/pinus)

49 lines (48 loc) 1.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WSProcessor = void 0; const http_1 = require("http"); const events_1 = require("events"); const WebSocket = require("ws"); let ST_STARTED = 1; let ST_CLOSED = 2; /** * websocket protocol processor */ class WSProcessor extends events_1.EventEmitter { constructor() { super(); this.httpServer = new http_1.Server(); let self = this; this.wsServer = new WebSocket.Server({ server: this.httpServer }); this.wsServer.on('connection', function (socket, request) { // emit socket to outside self.emit('connection', socket, request); }); this.state = ST_STARTED; } add(socket, data) { if (this.state !== ST_STARTED) { return; } this.httpServer.emit('connection', socket); if (typeof socket.ondata === 'function') { // compatible with stream2 socket.ondata(data, 0, data.length); } else { // compatible with old stream socket.emit('data', data); } } close() { if (this.state !== ST_STARTED) { return; } this.state = ST_CLOSED; this.wsServer.close(); this.wsServer = null; this.httpServer = null; } } exports.WSProcessor = WSProcessor;