UNPKG

nxkit

Version:

This is a collection of tools, independent of any other libraries

154 lines (153 loc) 5.81 kB
"use strict"; /* ***** BEGIN LICENSE BLOCK ***** * Distributed under the BSD license: * * Copyright (c) 2015, xuewen.chu * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of xuewen.chu nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL xuewen.chu BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * ***** END LICENSE BLOCK ***** */ Object.defineProperty(exports, "__esModule", { value: true }); const util_1 = require("../util"); const data_1 = require("./data"); const buffer_1 = require("../buffer"); const event_1 = require("../event"); exports.KEEP_ALIVE_TIME = 5e4; // 50s class ConversationBasic { constructor() { this.m_overflow = false; this.m_last_packet_time = Date.now(); this.m_KEEP_ALIVE_TIME = exports.KEEP_ALIVE_TIME; this.m_isGzip = false; this.m_replyPong = true; this.m_handles = {}; this.m_services_count = 0; this.m_token = ''; this.m_isOpen = false; this.m_default_service = ''; this.onClose = new event_1.EventNoticer('Close', this); this.onOpen = new event_1.EventNoticer('Open', this); this.onPing = new event_1.EventNoticer('Ping', this); this.onPong = new event_1.EventNoticer('Pong', this); this.onDrain = new event_1.EventNoticer('Drain', this); this.onOverflow = new event_1.EventNoticer('Overflow', this); } get overflow() { return this.m_overflow; } get lastPacketTime() { return this.m_last_packet_time; } get keepAliveTime() { return this.m_KEEP_ALIVE_TIME; } set keepAliveTime(value) { this.m_KEEP_ALIVE_TIME = Math.max(5e3, Number(value) || exports.KEEP_ALIVE_TIME); } get isGzip() { return this.m_isGzip; } get replyPong() { return this.m_replyPong; } get token() { return this.m_token; } get isOpen() { return this.m_isOpen; } _service(service) { return this.m_services_count == 1 ? undefined : service; } get handles() { return { ...this.m_handles }; } /** * @fun parse # parser message * @arg packet {String|Buffer} * @arg {Boolean} isText */ async handlePacket(packet, isText) { this.m_last_packet_time = Date.now(); var data = await data_1.DataBuilder.parse(packet, isText, this.isGzip); if (!data) return; if (!this.isOpen) return console.warn('ConversationBasic.handlePacket, connection close status'); switch (data.type) { case data_1.Types.T_BIND: this.bindServices([data.service]).catch(console.warn); break; case data_1.Types.T_PING: // ping Extension protocol this.handlePing(buffer_1.Zero); break; case data_1.Types.T_PONG: // pong Extension protocol this.handlePong(buffer_1.Zero); break; default: var handle = this.m_handles[data.service || this.m_default_service]; if (handle) { handle.receiveMessage(data).catch((e) => console.error(e)); } else { console.error('Could not find the message handler, ' + 'discarding the message, ' + data.service); } } } handlePing(data) { this.m_last_packet_time = Date.now(); if (this.replyPong) this.pong().catch(console.error); this.onPing.trigger(data); } handlePong(data) { this.m_last_packet_time = Date.now(); this.onPong.trigger(data); } async sendFormatData(data) { var df = new data_1.DataBuilder(data); var bf = await df.builder(this.isGzip); await this.send(buffer_1.default.from(bf)); } static write(self, api, args) { return util_1.default.promise(function (resolve, reject) { var ok = api(...args, function (err) { if (err) { reject(Error.new(err)); } else { resolve(); } }); if (!ok) { if (!self.m_overflow) { self.m_overflow = true; self.onOverflow.trigger({}); } } }); } } exports.ConversationBasic = ConversationBasic;