Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
O
Onebot Lb
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
3rdeye
Onebot Lb
Commits
b6ccbd7a
Commit
b6ccbd7a
authored
Oct 29, 2021
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improve
parent
45ebb911
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
45 additions
and
24 deletions
+45
-24
README.md
README.md
+9
-1
config.example.yaml
config.example.yaml
+18
-9
src/main.ts
src/main.ts
+6
-2
src/reverse-ws/reverse-ws.service.ts
src/reverse-ws/reverse-ws.service.ts
+9
-11
src/route/Route.ts
src/route/Route.ts
+1
-1
src/utility/config.ts
src/utility/config.ts
+2
-0
No files found.
README.md
View file @
b6ccbd7a
...
...
@@ -10,4 +10,12 @@
## Docker
Docker 容器镜像位于
`git-registry.mycard.moe/3rdeye/onebot-lb`
。使用时把
`config.yaml`
挂载到
`/usr/src/app/config.yaml`
即可。
\ No newline at end of file
Docker 容器镜像位于
`git-registry.mycard.moe/3rdeye/onebot-lb`
。使用时把
`config.yaml`
挂载到
`/usr/src/app/config.yaml`
即可。
## 运行
### http 路径
*
`/onebot`
OneBot 后端 (http 或反向 WebSocket) 连接的路径。
*
`/route/<app name>`
app 后端连接的路径。
\ No newline at end of file
config.example.yaml
View file @
b6ccbd7a
onebot
:
# 配置模式请参照 [Koishi 文档](https://koishi.js.org/v4/plugins/adapter/onebot.html#%E6%9C%BA%E5%99%A8%E4%BA%BA%E9%80%89%E9%A1%B9)
host
:
'
localhost'
# 监听地址
port
:
3000
# 监听端口
# OntBot 后端配置。
## 配置模式请参照 [Koishi 文档](https://koishi.js.org/v4/plugins/adapter/onebot.html#%E6%9C%BA%E5%99%A8%E4%BA%BA%E9%80%89%E9%A1%B9)
onebot
:
path
:
/onebot
# http 或反向 WebSocket 监听路径
secret
:
'
secret'
# 接收信息时用于验证的字段,应与 OneBot 的 `secret` 配置保持一致。
bots
:
-
protocol
:
'
ws'
endpoint
:
'
ws://localhost:6700'
selfId
:
'
1111111111'
token
:
'
token'
routes
:
# 路由配置。对于每个消息或事件,负载均衡器会发给所有路由的依照策略的某一个连接。
-
protocol
:
'
ws'
# 可选值: http, ws, ws-reverse
endpoint
:
'
ws://localhost:6700'
# http 或正向 WebSocket 连接地址
selfId
:
'
1111111111'
# 机器人 id
token
:
'
token'
# 发送信息时用于验证的字段。
# app 后端路由配置。
## app 后端只支持正向和反向 WebSocket
## 对于每个消息或事件,负载均衡器会发给所有路由的依照策略的某一个连接。
routes
:
-
name
:
default
# 必填。路由名称。机器人连接的 ws 路径为 ws://<地址>/routes/<name>
selfId
:
'
3221204940'
# 必填。机器人 ID,和 OneBot 配置的 selfId 一致
token
:
'
token'
# 连接 token
...
...
@@ -14,7 +23,7 @@ routes: # 路由配置。对于每个消息或事件,负载均衡器会发给
balancePolicy
:
hash
select
:
false
# 作用域,详见 [Koishi 文档](https://koishi.js.org/v4/guide/plugin/context.html#%E5%9C%A8%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E4%B8%AD%E4%BD%BF%E7%94%A8%E9%80%89%E6%8B%A9%E5%99%A8)
heartbeat
:
3000
# 心跳包的间隔。0 或不填为禁用心跳包。
wsReverse
:
# 该路由的反向 WebSocket 配置
-
url
:
'
ws://localhost:8080'
token
:
'
token'
;
wsReverse
:
# 该路由的反向 WebSocket 配置
。可以配置多个。
-
endpoint
:
'
ws://localhost:8080'
token
:
'
token'
reconnectInterval
:
60000
# 重连间隔
src/main.ts
View file @
b6ccbd7a
...
...
@@ -2,6 +2,7 @@ import { NestFactory } from '@nestjs/core';
import
{
NestExpressApplication
}
from
'
@nestjs/platform-express
'
;
import
{
AppModule
}
from
'
./app.module
'
;
import
{
KoishiWsAdapter
}
from
'
koishi-nestjs
'
;
import
{
ConfigService
}
from
'
@nestjs/config
'
;
async
function
bootstrap
()
{
const
app
=
await
NestFactory
.
create
<
NestExpressApplication
>
(
AppModule
);
...
...
@@ -20,7 +21,10 @@ async function bootstrap() {
const document = SwaggerModule.createDocument(app, documentConfig);
SwaggerModule.setup('docs', app, document);
*/
await
app
.
listen
(
3000
);
const
config
=
app
.
get
(
ConfigService
);
await
app
.
listen
(
config
.
get
<
number
>
(
'
port
'
)
||
3000
,
config
.
get
<
string
>
(
'
host
'
)
||
'
::
'
,
);
}
bootstrap
();
src/reverse-ws/reverse-ws.service.ts
View file @
b6ccbd7a
...
...
@@ -18,19 +18,17 @@ export class ReverseWsService extends ConsoleLogger {
if
(
revConfig
.
token
)
{
headers
[
'
Authorization
'
]
=
`Bearer
${
revConfig
.
token
}
`
;
}
const
ws
=
new
WebSocket
(
revConfig
.
url
,
{
headers
});
const
interval
=
revConfig
.
reconnectInterval
||
5000
;
let
initialized
=
false
;
const
ws
=
new
WebSocket
(
revConfig
.
endpoint
,
{
headers
});
ws
.
on
(
'
error
'
,
(
err
)
=>
{
this
.
warn
(
`Socket from
${
route
.
name
}
error:
${
err
.
toString
()}
`
);
if
(
!
initialized
)
{
this
.
warn
(
`Will retry after
${
interval
}
ms.`
);
setTimeout
(()
=>
this
.
initializeReverseWs
(
route
,
revConfig
),
interval
);
}
this
.
warn
(
`Socket
${
revConfig
.
endpoint
}
from
$
{
route
.
name
}
error:
${
err
.
toString
()}
`
,
);
});
ws
.
on
(
'
open
'
,
()
=>
{
initialized
=
true
;
this
.
log
(
`Route
${
route
.
name
}
connected to
${
revConfig
.
url
}
.`
);
//
initialized = true;
this
.
log
(
`Route
${
route
.
name
}
connected to
${
revConfig
.
endpoint
}
.`
);
route
.
addConnection
(
ws
);
this
.
meesageService
.
registerWsEvent
(
ws
,
route
);
});
...
...
@@ -38,7 +36,7 @@ export class ReverseWsService extends ConsoleLogger {
route
.
removeConnection
(
ws
);
const
interval
=
revConfig
.
reconnectInterval
||
5000
;
this
.
warn
(
`
Route
${
route
.
name
}
disconnected from
${
revConfig
.
url
}
:
${
code
}
:
${
msg
}
. Will retry after
${
interval
}
ms.`
,
`
Socket
${
revConfig
.
endpoint
}
from
${
route
.
name
}
disconnected
:
${
code
}
:
${
msg
}
. Will retry after
${
interval
}
ms.`
,
);
setTimeout
(()
=>
this
.
initializeReverseWs
(
route
,
revConfig
),
interval
);
});
...
...
src/route/Route.ts
View file @
b6ccbd7a
...
...
@@ -7,7 +7,7 @@ import { createHash } from 'crypto';
export
type
BalancePolicy
=
'
broadcast
'
|
'
random
'
|
'
round-robin
'
|
'
hash
'
;
export
interface
ReverseWsConfig
{
url
:
string
;
endpoint
:
string
;
token
?:
string
;
reconnectInterval
?:
number
;
}
...
...
src/utility/config.ts
View file @
b6ccbd7a
...
...
@@ -6,6 +6,8 @@ import { AdapterConfig } from '@koishijs/plugin-adapter-onebot/lib/utils';
import
{
BotConfig
}
from
'
@koishijs/plugin-adapter-onebot/lib/bot
'
;
export
interface
LbConfig
{
host
:
string
;
port
:
number
;
onebot
:
Adapter
.
PluginConfig
<
AdapterConfig
,
BotConfig
>
;
routes
:
RouteConfig
[];
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment