UNPKG

node-insim

Version:

An InSim library for NodeJS with TypeScript support

95 lines (94 loc) 3.92 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 __()); }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OutSim = void 0; var lodash_defaults_1 = __importDefault(require("lodash.defaults")); var tiny_typed_emitter_1 = require("tiny-typed-emitter"); var errors_1 = require("../errors"); var log_1 = require("../log"); var protocols_1 = require("../protocols"); var OutSimPack_1 = require("./OutSimPack"); var OutSimPack2_1 = require("./OutSimPack2"); var log = log_1.log.extend('outsim'); var OutSim = /** @class */ (function (_super) { __extends(OutSim, _super); function OutSim(_a) { var _b = _a === void 0 ? {} : _a, _c = _b.timeout, timeout = _c === void 0 ? 0 : _c; var _this = _super.call(this) || this; _this._options = defaultOutSimOptions; _this.connection = null; _this.timeout = 0; _this.timeout = timeout; _this.on('connect', function () { return log("Connected to ".concat(_this._options.Host, ":").concat(_this._options.Port)); }); _this.on('disconnect', function () { return log("Disconnected from ".concat(_this._options.Host, ":").concat(_this._options.Port)); }); return _this; } OutSim.prototype.connect = function (options) { var _this = this; this._options = (0, lodash_defaults_1.default)(options, defaultOutSimOptions); log("Connecting to ".concat(this._options.Host, ":").concat(this._options.Port, "...")); this.connection = new protocols_1.UDP({ host: this._options.Host, port: this._options.Port, timeout: this.timeout, socketInitialisationMode: 'bind', }); this.connection.connect(); this.connection.on('connect', function () { _this.emit('connect'); }); this.connection.on('disconnect', function () { _this.emit('disconnect'); }); this.connection.on('error', function (error) { throw new errors_1.InSimError("UDP connection error: ".concat(error.message)); }); this.connection.on('data', function (data) { return _this.handleMessage(data); }); this.connection.on('timeout', function () { _this.emit('timeout'); }); }; OutSim.prototype.disconnect = function () { log('Disconnecting...'); if (this.connection === null) { log('Cannot disconnect - not connected'); return; } this.connection.disconnect(); }; OutSim.prototype.handleMessage = function (data) { var outSimPack = this._options.OutSimOpts > 0 ? new OutSimPack2_1.OutSimPack2(this._options.OutSimOpts) : new OutSimPack_1.OutSimPack(); this.emit('packet', outSimPack.unpack(data)); }; return OutSim; }(tiny_typed_emitter_1.TypedEmitter)); exports.OutSim = OutSim; OutSim.defaultMaxListeners = 255; var defaultOutSimOptions = { Host: '127.0.0.1', Port: 29997, OutSimOpts: 0, };