-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (71 loc) · 1.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const HuyaDanmu = require('huya-danmu');
// 数据库(根据数据库配置而定)
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017/';
const dbName = 'huya';
// 房间号(改动这个)
const roomid = process.argv.length >= 3 ? process.argv[2] : '';
if (!roomid) {
console.error('未指定房间号!用法 ------ node index.js 房间号');
process.exit(-1);
}
// 监听Client
const client = new HuyaDanmu(roomid);
let count = 0;
const reconnectInterval = 60000;
const connect = () => {
// 连接事件
client.on('connect', () => {
console.log(`已连接虎牙${roomid}房间弹幕~`);
});
// 监听到弹幕事件
client.on('message', msg => {
count += 1;
// 加工信息
const info = {
room: roomid,
...msg,
time: new Date(),
};
// 输出chat类型弹幕信息(文字弹幕)
const infoType = info.type;
if (infoType === 'chat') {
console.log(`number: ${count}`);
Object.keys(info).forEach(key => {
const val = key === 'from' ? JSON.stringify(info[key]) : info[key];
console.log(`${key}: ${val}`);
});
console.log('');
}
// 存至数据库
MongoClient.connect(url, function (err, db) {
if (err) {
console.error(err);
} else {
const dbo = db.db(dbName);
dbo.collection(infoType).insertOne(info, function (err) {
if (err) {
console.error(err);
}
db.close();
});
}
});
});
// 错误事件
client.on('error', e => {
console.error(e);
});
// 断连事件
client.on('close', () => {
console.log(`Connection closed! Obtained ${count} messages! Try to reconnect in 1 minute...`);
try {
setTimeout(client.start(), reconnectInterval);
} catch (err) {
console.error(err);
}
});
// 启动监听
client.start();
};
connect();