Skip to content

Commit d37bccd

Browse files
authored
Merge pull request #4 from eric2788/develop
新增自動重連機制
2 parents 32d6924 + c0076ee commit d37bccd

File tree

7 files changed

+49
-38
lines changed

7 files changed

+49
-38
lines changed

.github/workflows/node.js.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Build Executable And Publish
22
env:
3-
version: 0.1.4
3+
version: 0.1.5
44
artifact_name: vup_monitors
55
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
66

Dockerfile

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
1-
FROM node:16 AS builder
1+
FROM node:16-alpine
22

33
WORKDIR /app
44

55
COPY ./src ./src
66
COPY *.json .
77

88
RUN npm install
9-
RUN npm install pkg -g
10-
RUN pkg .
119

12-
FROM ubuntu:latest
10+
VOLUME [ "/app/data" ]
1311

14-
WORKDIR /
15-
16-
ENV NODE_ENV=production
17-
18-
COPY --from=builder /app/dist/vup_monitors-linux /vup_monitors
19-
RUN chmod +x /vup_monitors
20-
21-
VOLUME [ "/data" ]
22-
23-
ENTRYPOINT [ "/vup_monitors" ]
12+
CMD [ "npm", "run", "start" ]

changelog.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
### 更新
55

6-
- 把所有js檔案放入 src folder
7-
- 更新 readme.md
8-
- 修复无法读取外部 data/settings.json
9-
- reduce docker image size
6+
- 新增 go-cqhttp 自動重連機制。(之前因為沒有重連機制,在 go-cqhttp 重啟之後整個 vup_monitors 程序會失效直到重新啟動。)
7+
8+
- 縮小 Docker Image 佔用空間 (500MB -> 154MB)
9+

data/settings.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"bot": {
3+
"http": "http://127.0.0.1:5700",
4+
"ws": "ws://127.0.0.1:6700"
5+
},
6+
"redis": {
7+
"host": "127.0.0.1",
8+
"port": 6379,
9+
"database": 0
10+
},
11+
"websocket": {
12+
"host": "localhost:8080",
13+
"use-tls": false
14+
},
15+
"source": "websocket",
16+
"owners": []
17+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vup_monitors",
3-
"version": "0.1.4",
3+
"version": "0.1.5",
44
"bin": "src/index.js",
55
"main": "src/index.js",
66
"scripts": {

src/bot/ws.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,26 @@ async function connect(){
2121
}
2222

2323
let ws = undefined
24+
let callback = (data) => {}
2425

2526
async function startWS(){
2627
try {
27-
const websocket = await connect()
28-
ws = websocket
28+
ws = await connect()
29+
ws.on('close', err => {
30+
console.warn(`go-cqhttp WebSocket 意外关闭,重连中...`)
31+
startWS()
32+
})
33+
ws.on('error', err => {
34+
console.warn(`连接 go-cqhttp 时出现错误: ${err?.message ?? err},重连中`)
35+
startWS()
36+
})
37+
ws.on('message', data => {
38+
try {
39+
callback(JSON.parse(data))
40+
} catch (e) {
41+
console.error(e)
42+
}
43+
})
2944
return ws
3045
}catch(err){
3146
console.warn('五秒后重连...')
@@ -34,6 +49,10 @@ async function startWS(){
3449
}
3550
}
3651

52+
function setListener(listener){
53+
callback = listener
54+
}
55+
3756
module.exports = {
3857
startWS,
3958
send(action, params) {
@@ -43,19 +62,5 @@ module.exports = {
4362
}
4463
ws.send(JSON.stringify({ action, params }))
4564
},
46-
async listen(callback) {
47-
if (!ws) {
48-
//console.warn(`WS 尚未连接,五秒后重试。`)
49-
await sleep(5000)
50-
await this.listen(callback)
51-
return
52-
}
53-
ws.on('message', data => {
54-
try {
55-
callback(JSON.parse(data))
56-
} catch (e) {
57-
console.error(e)
58-
}
59-
})
60-
}
65+
setListener
6166
}

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ console.log(`已设置管理员QQ号: ${owners}, 群管和管理员都可使用
1818
console.log('正在启动 vup monitors...')
1919
Promise.all([ws.startWS(), messager.connect()])
2020
.then(() => {
21-
ws.listen(data => {
21+
ws.setListener(data => {
2222
if (process.env.NODE_ENV === 'development') {
2323
console.log(data)
2424
}

0 commit comments

Comments
 (0)