Skip to content

Commit

Permalink
feat: 增加简单CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
shanmite committed Apr 13, 2021
1 parent cc29e0c commit 14aa086
Show file tree
Hide file tree
Showing 4 changed files with 367 additions and 30 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# BiliBili 直播间弹幕
包括全区礼物广播, 投喂信息, 入场信息, 人气值等
# BiliBili 直播间信息流
包括评论, 醒目留言, 入场信息, 投喂送礼, 全区礼物广播, 总榜排行, 人气值等

## 初始化
`GET` [https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo](https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo)
Expand Down
102 changes: 75 additions & 27 deletions lib/LiveChat.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,27 @@ class ServerInfo {
};
}

/**
* 设置直播房间号
* @param {number} rid
* @returns
*/
setRoomId(rid) {
this.roomid = rid;
return this
}

/**
* 设置自己的uid(可随意设置)
* @param {number} uid
* @returns
*/
setUid(uid) {
this.uid = uid;
return this
}

setWssURL(wss_url) {
_setWssURL(wss_url) {
this.wss_url = wss_url;
}

Expand Down Expand Up @@ -65,42 +75,55 @@ class LiveChat extends ServerInfo {
this.option = {
retryTimeOut: 1 * 1000,
heartBeatInterval: 30 * 1000,
msgState: false
}
this.messageHandle = (msg) => { console.log(msg) };
}

this.connectState = false;
this._messageHandle = (msg) => { console.log(msg) };
}

/**
* @typedef MSG
* @property {"MESSAGE"|"POPULAR_COUNT"|"CONNECT_SUCCESS"} type
* @property {Array|number} inner
* @param {MessageHandle} cb
* @returns {this}
* @callback MessageHandle
* @param {MSG} msg
*/
setMessageHandle(cb) {
this.messageHandle = cb;
this._messageHandle = cb;
return this;
}

async run() {
if (this.roomid && this.uid && await this.initInfo()) {
console.log('chat_client -> initialize success');
this.HostList = this.hostListIter();
if (this.connectState) {
this._wsConnect()
} else {
console.log('chat_client -> initialize failed');
}
}

_wsConnect() {
const { value, done } = this.HostList.next();
if (!done) {
this.setWssURL(value);
this.ws = new WebSocket(this.wss_url);
this.ws.onopen = this.onOpen.bind(this);
this.ws.onmessage = this.onMessage.bind(this);
this.ws.onerror = this.onError.bind(this);
this.ws.onclose = this.onClose.bind(this);
if (this.roomid && this.uid && await this.initInfo()) {
console.log('chat_client -> initialize success');
this.HostList = this.hostListIter();
this._wsConnect()
} else {
console.log('chat_client -> initialize failed');
}
}
}

close() {
this.ws.close()
}

_wsConnect() {
if (!this.connectState) {
const { value, done } = this.HostList.next();
if (!done) this._setWssURL(value);
}
this.ws = new WebSocket(this.wss_url);
this.ws.onopen = this.onOpen.bind(this);
this.ws.onmessage = this.onMessage.bind(this);
this.ws.onerror = this.onError.bind(this);
this.ws.onclose = this.onClose.bind(this);
}

_heartBeat() {
return setInterval(() => {
console.log('chat_client -> heartBeat');
Expand All @@ -121,20 +144,45 @@ class LiveChat extends ServerInfo {
convert.WS.OP_USER_AUTHENTICATION
)
);
this.heartBeat = this._heartBeat();
}

onMessage(raw_msg) {
this.option.msgState = true;
let raw_data_toArrayBuffer = null;
let msg = { type: '' };
const raw_data = raw_msg.data;
if (raw_data instanceof Buffer) {
raw_data_toArrayBuffer = raw_data.buffer.slice(raw_data.byteOffset, raw_data.byteOffset + raw_data.byteLength)
} else if (raw_data instanceof ArrayBuffer) {
raw_data_toArrayBuffer = raw_data
}
const raw_data_obj = convert.toObject(raw_data_toArrayBuffer);
this.messageHandle(raw_data_obj);
const { totalLen, body, op, popular_count } = convert.toObject(raw_data_toArrayBuffer);
if (totalLen > 0) {
const { WS } = convert;
switch (op) {
case WS.OP_MESSAGE:
msg.type = 'MESSAGE';
msg.inner = body;
break;
case WS.OP_HEARTBEAT_REPLY:
msg.type = 'POPULAR_COUNT';
msg.inner = popular_count;
break;
case WS.OP_CONNECT_SUCCESS:
if (body[0].code === WS.AUTH_OK) {
msg.type = 'CONNECT_SUCCESS';
this.connectState = true;
this.heartBeat = this._heartBeat();
}
else if (body[0].code === WS.AUTH_TOKEN_ERROR) {
this.close();
}
break;
default:
this.close();
return;
}
this._messageHandle(msg);
}
}

onError(err) {
Expand All @@ -143,7 +191,7 @@ class LiveChat extends ServerInfo {

onClose() {
clearInterval(this.heartBeat);
if (this.option.msgState) {
if (this.connectState) {
console.log('chat_client -> close');
} else {
console.log('chat_client -> change host');
Expand Down
Loading

0 comments on commit 14aa086

Please sign in to comment.