UNPKG

nicolive-api

Version:
133 lines (106 loc) 5.21 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _cheerio = require('cheerio'); var _cheerio2 = _interopRequireDefault(_cheerio); var _BaseViewer2 = require('./BaseViewer'); var _BaseViewer3 = _interopRequireDefault(_BaseViewer2); var _Comment = require('../Comment'); var _Comment2 = _interopRequireDefault(_Comment); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var LiveViewer = function (_BaseViewer) { _inherits(LiveViewer, _BaseViewer); function LiveViewer(_ref) { var port = _ref.port, addr = _ref.addr, open_time = _ref.open_time, thread = _ref.thread, version = _ref.version, res_from = _ref.res_from, user_id = _ref.user_id, premium = _ref.premium, mail = _ref.mail, cookie = _ref.cookie, room = _ref.room; _classCallCheck(this, LiveViewer); var _this = _possibleConstructorReturn(this, (LiveViewer.__proto__ || Object.getPrototypeOf(LiveViewer)).call(this, { port: port, addr: addr, thread: thread, version: version, res_from: res_from, cookie: cookie })); _this.open_time = open_time; _this.user_id = user_id; _this.premium = premium; _this.mail = mail; _this.room = room; return _this; } _createClass(LiveViewer, [{ key: 'setOnDataEvent', value: function setOnDataEvent(connection) { var _this2 = this; var mainConnection = connection ? connection : this.connection; this.connection.on('data', function (buffer) { var chunk = buffer.toString(); if (!chunk.match(/\0$/)) return; var data = (0, _cheerio2.default)('<data>' + chunk + '</data>'); var resultCodeValue = data.find('thread').attr('resultcode'); if (resultCodeValue) { var foundThread = data.find('thread'); _this2.ticket = foundThread.attr('ticket'); _this2.last_res = foundThread.attr('last_res'); if (resultCodeValue === '0') { _this2.connection.emit('handshaked'); } else { _this2.connection.emit('fail', foundThread.toString()); _this2.connection.destroy(); } } var comments = data.find('chat'); for (var i = 0, len = comments.length; i < len; i++) { var element = (0, _cheerio2.default)(comments[i]); var comment = new _Comment2.default(element, _this2.room); mainConnection.emit('comment', comment); _this2.last_res = comment.attr.no; } }); } }, { key: 'comment', value: function comment(text) { var _this3 = this; var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; return new Promise(function (resolve, reject) { _this3.client.getPostKey(_this3.thread, _this3.last_res).then(function (postkey) { var vpos = (Math.floor(Date.now() / 1000) - _this3.open_time) * 100; var chat = (0, _cheerio2.default)('<chat />'); chat.attr(JSON.parse(JSON.stringify({ thread: _this3.thread, ticket: _this3.ticket, mail: _this3.mail, user_id: _this3.user_id, premium: _this3.premium }))); chat.attr({ vpos: vpos }); chat.attr({ postkey: postkey }); if (options.mail) chat.attr('mail', options.mail); chat.text(text); _this3.connection.write(chat + '\0'); resolve(); }).catch(function (err) { return reject(err); }); }); } }]); return LiveViewer; }(_BaseViewer3.default); exports.default = LiveViewer;