Skip to content

Commit

Permalink
close #53
Browse files Browse the repository at this point in the history
  • Loading branch information
lzghzr committed Jan 18, 2018
1 parent 1b203d8 commit 0d6cabf
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 117 deletions.
86 changes: 38 additions & 48 deletions bilive/listener.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import * as request from 'request'
import * as tools from './lib/tools'
import { EventEmitter } from 'events'
import { AppClient } from './lib/app_client'
import { CommentClient } from './lib/comment_client'
import * as tools from './lib/tools'
import AppClient from './lib/app_client'
import DMclient from './dm_client_re'
import { liveOrigin, apiLiveOrigin, smallTVPathname, rafflePathname, _options } from './index'
/**
* 监听服务器消息
*
* @export
* @class Listener
* @extends {EventEmitter}
*/
export class Listener extends EventEmitter {
class Listener extends EventEmitter {
constructor() {
super()
}
/**
* 用于接收弹幕消息
*
* @private
* @type {CommentClient}
* @type {DMclient}
* @memberof Listener
*/
private _CommentClient: CommentClient
private _DMclient: DMclient
/**
* 小电视ID
*
Expand Down Expand Up @@ -61,14 +60,13 @@ export class Listener extends EventEmitter {
* @memberof Listener
*/
public Start() {
let config = _options.config
, roomID = config.defaultRoomID
, userID = config.defaultUserID
this._CommentClient = new CommentClient({ roomID, userID })
this._CommentClient
.on('serverError', error => { tools.Error('弹幕服务器监听', error) })
.on('SYS_MSG', this._SYSMSGHandler.bind(this))
.on('SYS_GIFT', this._SYSGiftHandler.bind(this))
const config = _options.config
const roomID = config.defaultRoomID
const userID = config.defaultUserID
this._DMclient = new DMclient({ roomID, userID })
this._DMclient
.on('SYS_MSG', dataJson => this._SYSMSGHandler(dataJson))
.on('SYS_GIFT', dataJson => this._SYSGiftHandler(dataJson))
.Connect()
}
/**
Expand All @@ -79,10 +77,10 @@ export class Listener extends EventEmitter {
* @memberof Listener
*/
private _SYSMSGHandler(dataJson: SYS_MSG) {
if (dataJson.real_roomid == null || dataJson.tv_id == null) return
let url = apiLiveOrigin + smallTVPathname
, roomID = dataJson.real_roomid
this._RaffleCheck(url, roomID, 'smallTV').catch(error => { tools.Error('系统消息', roomID, error) })
if (dataJson.real_roomid === undefined || dataJson.tv_id === undefined) return
const url = apiLiveOrigin + smallTVPathname
const roomID = dataJson.real_roomid
this._RaffleCheck(url, roomID, 'smallTV')
}
/**
* 监听系统礼物消息
Expand All @@ -92,11 +90,11 @@ export class Listener extends EventEmitter {
* @memberof Listener
*/
private _SYSGiftHandler(dataJson: SYS_GIFT) {
if (dataJson.real_roomid == null || dataJson.giftId == null) return
let url = apiLiveOrigin + rafflePathname
, roomID = dataJson.real_roomid
this._RaffleCheck(url, roomID, 'raffle').catch(error => { tools.Error('系统礼物消息', roomID, error) })
// this._AppLightenCheck(roomID).catch(error => { tools.Error('系统礼物消息', roomID, error) })
if (dataJson.real_roomid === undefined || dataJson.giftId === undefined) return
const url = apiLiveOrigin + rafflePathname
const roomID = dataJson.real_roomid
this._AppLightenCheck(roomID)
this._RaffleCheck(url, roomID, 'raffle')
}
/**
* 检查房间抽奖信息
Expand All @@ -108,30 +106,21 @@ export class Listener extends EventEmitter {
* @memberof Listener
*/
private async _RaffleCheck(url: string, roomID: number, raffle: 'smallTV' | 'raffle' | 'lighten') {
let check: request.Options = {
const check: request.Options = {
uri: `${url}/check?roomid=${roomID}`,
json: true,
headers: { 'Referer': `${liveOrigin}/${roomID}` }
}
, raffleCheck = await tools.XHR<raffleCheck>(check)
if (raffleCheck.response.statusCode === 200 && raffleCheck.body.code === 0 && raffleCheck.body.data.length > 0) {
const raffleCheck = await tools.XHR<raffleCheck>(check)
if (raffleCheck !== undefined && raffleCheck.response.statusCode === 200
&& raffleCheck.body.code === 0 && raffleCheck.body.data.length > 0) {
raffleCheck.body.data.forEach(data => {
let message: raffleMSG = {
const message: raffleMSG = {
cmd: raffle,
roomID,
id: +data.raffleId
}
this._RaffleHandler(message)
// 临时
if (raffle === 'raffle') {
let message: appLightenMSG = {
cmd: 'appLighten',
roomID,
id: +data.raffleId,
type: 'openfire'
}
this._RaffleHandler(message)
}
})
}
}
Expand All @@ -142,18 +131,18 @@ export class Listener extends EventEmitter {
* @param {number} roomID
* @memberof Listener
*/
// @ts-ignore 暂时无用
private async _AppLightenCheck(roomID: number) {
let room: request.Options = {
const room: request.Options = {
uri: `${apiLiveOrigin}/AppRoom/index?${AppClient.signQueryBase(`room_id=${roomID}`)}`,
json: true
}
, roomInfo = await tools.XHR<roomInfo>(room, 'Android')
if (roomInfo.response.statusCode === 200 && roomInfo.body.code === 0 && roomInfo.body.data.event_corner.length > 0) {
const roomInfo = await tools.XHR<roomInfo>(room, 'Android')
if (roomInfo !== undefined && roomInfo.response.statusCode === 200
&& roomInfo.body.code === 0 && roomInfo.body.data.event_corner.length > 0) {
roomInfo.body.data.event_corner.forEach(event => {
let type = event.event_type.split('-')
const type = event.event_type.split('-')
if (type.length !== 2) return
let message: appLightenMSG = {
const message: appLightenMSG = {
cmd: 'appLighten',
roomID,
id: +type[1],
Expand All @@ -171,9 +160,9 @@ export class Listener extends EventEmitter {
* @memberof Listener
*/
private _RaffleHandler(raffleMSG: raffleMSG | appLightenMSG) {
let roomID = raffleMSG.roomID
, id = raffleMSG.id
, msg = ''
const roomID = raffleMSG.roomID
const id = raffleMSG.id
let msg = ''
switch (raffleMSG.cmd) {
case 'smallTV':
if (this._smallTVID >= id) return
Expand All @@ -199,4 +188,5 @@ export class Listener extends EventEmitter {
this.emit('raffle', raffleMSG)
tools.Log(`房间 ${roomID} 开启了第 ${id}${msg}抽奖`)
}
}
}
export default Listener
55 changes: 27 additions & 28 deletions bilive/options.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,48 @@
"protocol": "admin"
},
"apiIPs": [
"101.69.131.10",
"14.136.134.87",
"27.221.61.109",
"47.88.138.238",
"47.90.50.109",
"47.90.207.174",
"47.91.19.168",
"47.91.74.133",
"58.222.35.202",
"58.222.35.205",
"61.147.236.15",
"61.188.190.7",
"101.75.240.7",
"101.200.58.11",
"110.80.129.143",
"111.6.174.4",
"111.47.237.18",
"111.62.72.10",
"111.230.84.45",
"111.230.84.59",
"111.230.85.89",
"111.231.212.88",
"112.117.218.167",
"113.113.80.9",
"113.207.39.7",
"113.6.246.37",
"113.113.80.9",
"114.80.223.172",
"114.80.223.177",
"116.207.118.12",
"118.89.213.55",
"120.221.74.132",
"120.41.32.15",
"120.192.82.106",
"120.221.74.132",
"121.32.238.201",
"121.32.238.204",
"122.143.15.150",
"122.228.77.82",
"124.14.8.137",
"124.232.151.74",
"122.228.77.85",
"123.206.1.201",
"125.88.148.10",
"14.136.134.86",
"14.136.134.87",
"140.143.177.142",
"140.249.9.4",
"218.98.33.197",
"219.144.79.7",
"221.13.201.9",
"221.204.199.135",
"222.186.35.81",
"223.99.231.4",
"27.221.106.4",
"27.221.61.109",
"36.248.28.7",
"47.88.107.100",
"47.88.138.238",
"47.90.207.174",
"47.90.50.109",
"47.91.19.168",
"47.91.74.133",
"58.222.35.202",
"58.222.35.205",
"58.56.111.209",
"59.47.225.8",
"61.147.236.15",
"61.188.190.7"
"222.186.35.81"
],
"config": {
"defaultUserID": 0,
Expand Down
Loading

0 comments on commit 0d6cabf

Please sign in to comment.