Skip to content

Commit 1e7ae6e

Browse files
committed
init repo.
0 parents  commit 1e7ae6e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2811
-0
lines changed

.github/workflows/build.yml

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
name: Build and Test
2+
3+
on:
4+
push:
5+
branches: [ main, dev ]
6+
paths:
7+
- '.github/workflows/build.yml'
8+
- 'src/**'
9+
- 'build.gradle.kts'
10+
tags:
11+
- 'v*' # 匹配 v 开头的标签
12+
pull_request:
13+
branches: [ main ]
14+
paths:
15+
- '.github/workflows/build.yml'
16+
- 'src/**'
17+
- 'build.gradle.kts'
18+
tags:
19+
- 'v*' # 匹配 v 开头的标签
20+
21+
jobs:
22+
build:
23+
runs-on: ubuntu-latest
24+
25+
steps:
26+
- uses: actions/checkout@v4
27+
28+
- name: Set up JDK 21
29+
uses: actions/setup-java@v4
30+
with:
31+
java-version: '21'
32+
distribution: 'temurin'
33+
34+
- name: Setup Gradle
35+
uses: gradle/actions/setup-gradle@v3
36+
37+
- name: Build with Gradle
38+
run: ./gradlew build
39+
40+
- name: Run tests
41+
run: ./gradlew test
42+
43+
- name: Create shadow JAR
44+
run: ./gradlew shadowJar
45+
46+
- name: Upload JAR artifact
47+
uses: actions/upload-artifact@v4
48+
with:
49+
name: huhobot-server
50+
path: build/libs/huhobotServer-all.jar
51+
retention-days: 1 # 仅保留1天临时文件
52+
53+
release:
54+
name: Create Release
55+
needs: build
56+
if: startsWith(github.ref, 'refs/tags/')
57+
runs-on: ubuntu-latest
58+
# 新增权限配置
59+
permissions:
60+
contents: write # 必须的权限
61+
actions: read
62+
packages: write
63+
64+
steps:
65+
- name: Checkout code
66+
uses: actions/checkout@v4
67+
with:
68+
fetch-depth: 0
69+
70+
- name: Extract release info
71+
id: changelog
72+
run: |
73+
TAG_NAME=${GITHUB_REF#refs/tags/}
74+
75+
# 修复:使用正确的标签格式匹配
76+
VERSION=${TAG_NAME#v} # 去除v前缀(如果CHANGELOG使用纯版本号)
77+
78+
CHANGELOG_CONTENT=$(awk -v version="[v$VERSION]" '
79+
BEGIN {RS="## "; FS="\n"}
80+
$1 ~ version {
81+
sub(/\[.*\] - .*\n/, "")
82+
gsub(/`/, "\\`")
83+
gsub(/"/, "\\\"")
84+
print
85+
exit
86+
}
87+
' CHANGELOG.md)
88+
89+
EOF_MARKER=$(openssl rand -base64 12)
90+
echo "body<<$EOF_MARKER" >> $GITHUB_OUTPUT
91+
echo "$CHANGELOG_CONTENT" >> $GITHUB_OUTPUT
92+
echo "$EOF_MARKER" >> $GITHUB_OUTPUT
93+
94+
# 添加标签名输出
95+
echo "tag_name=$TAG_NAME" >> $GITHUB_OUTPUT
96+
97+
- name: Download Artifacts
98+
uses: actions/download-artifact@v4
99+
with:
100+
name: huhobot-server
101+
path: artifacts
102+
103+
- name: Setup workspace
104+
run: |
105+
VERSION=${GITHUB_REF_NAME#v} # 去掉v前缀
106+
echo '{"latest":"$VERSION"}' > artifacts/latest.json
107+
108+
- name: Upload to R2
109+
uses: ryand56/r2-upload-action@latest
110+
with:
111+
r2-account-id: ${{ secrets.R2_ACCOUNT_ID }}
112+
r2-access-key-id: ${{ secrets.R2_ACCESS_KEY_ID }}
113+
r2-secret-access-key: ${{ secrets.R2_SECRET_ACCESS_KEY }}
114+
r2-bucket: ${{ secrets.R2_BUCKET }}
115+
source-dir: artifacts
116+
destination-dir: mainServer
117+
118+
- name: Get timestamp
119+
id: get-time
120+
run: echo "TIME=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
121+
122+
- name: Create Release
123+
uses: softprops/action-gh-release@v1 # 改用更可靠的 Action
124+
with:
125+
tag_name: ${{ steps.changelog.outputs.tag_name }}
126+
name: HuHoBot MainServer ${{ steps.changelog.outputs.tag_name }}
127+
body: |
128+
${{ steps.changelog.outputs.body }}
129+
130+
### 构建信息
131+
- 构建时间: ${{ steps.get-time.outputs.TIME }}
132+
- 提交哈希: [${{ github.sha }}](${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }})
133+
files: |
134+
artifacts/*.jar

.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
.gradle
2+
build/
3+
!gradle/wrapper/gradle-wrapper.jar
4+
!**/src/main/**/build/
5+
!**/src/test/**/build/
6+
**/config.json
7+
**/data/
8+
9+
### STS ###
10+
.apt_generated
11+
.classpath
12+
.factorypath
13+
.project
14+
.settings
15+
.springBeans
16+
.sts4-cache
17+
bin/
18+
!**/src/main/**/bin/
19+
!**/src/test/**/bin/
20+
21+
### IntelliJ IDEA ###
22+
.idea
23+
*.iws
24+
*.iml
25+
*.ipr
26+
out/
27+
!**/src/main/**/out/
28+
!**/src/test/**/out/
29+
30+
### NetBeans ###
31+
/nbproject/private/
32+
/nbbuild/
33+
/dist/
34+
/nbdist/
35+
/.nb-gradle/
36+
37+
### VS Code ###
38+
.vscode/

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# HuHoBot-Websocket-Kotlin v0.0.1
2+
3+
- 初始化项目
4+

README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# HuHoBot WebSocket-Kotlin 服务端
2+
3+
[![GitHub Release](https://img.shields.io/github/v/release/HuHoBot/KtWebSocketServer?style=flat-square)](https://github.com/HuHoBot/KtWebSocketServer/releases)
4+
[![License](https://img.shields.io/github/license/HuHoBot/KtWebSocketServer?style=flat-square)](https://github.com/HuHoBot/KtWebSocketServer/blob/main/LICENSE)
5+
6+
轻量级 Minecraft 服务器通信中台,实现 WebSocket 协议的双向通信桥梁。
7+
8+
## 核心能力
9+
10+
### 架构特性
11+
12+
- **双端连接管理**:同时处理 Bot客户端 和 服务器端 的 WebSocket 连接
13+
- **事件驱动模型**:内置 9 种核心事件处理器
14+
- **异步响应机制**:基于 CompletableFuture 的异步回调系统
15+
- **会话生命周期管理**:自动清理失效连接
16+
17+
### 协议特征
18+
19+
- **心跳维持**`heart` 事件保持长连接
20+
- **消息分拣**:通过 header.type 自动路由消息
21+
- **唯一标识**:通过 packId 实现请求-响应匹配
22+
23+
## 核心事件处理
24+
25+
| 事件类型 | 处理器类 | 功能描述 |
26+
|-----------------------|--------------------------|-------------|
27+
| heart | handleHeart | 心跳包维持连接 |
28+
| success/error | handleResponeMsg | 通用响应处理 |
29+
| queryWl | handleResponeWhiteList | 白名单查询响应 |
30+
| queryOnline | handleResponeOnlineList | 在线玩家查询响应 |
31+
| bindConfirm | handleBindConfirm | 服务器绑定确认处理 |
32+
| SEND_MSG_BY_SERVER_ID | handleBotSendPack2Server | 机器人消息转发到服务器 |
33+
| QUERY_CLIENT_LIST | handleBotQueryClientList | 客户端列表查询 |
34+
| shakeHand | handleShakeHand | 握手协议处理 |
35+
36+
## 消息处理流程
37+
38+
```mermaid
39+
graph TD;
40+
A[WebSocket连接建立] --> B{身份判断};
41+
B -->|Server端| C[注册服务器实例];
42+
B -->|Bot端| D[注册机器人实例];
43+
C --> E[等待业务消息];
44+
D --> E;
45+
E --> F{消息类型判断};
46+
F -->|心跳包| G[更新心跳时间];
47+
F -->|命令响应| H[触发回调Future];
48+
F -->|业务请求| I[路由到对应处理器];
49+
```
50+
51+
## 配置说明
52+
53+
### 环境要求
54+
55+
- JDK 21+
56+
- Ktor 3.3.0
57+
- Fastjson2 2.0+
58+
59+
### 如何搭建
60+
1. 创建`config.json`文件,并放置在项目根目录下
61+
```json5
62+
{
63+
"key": "XXXXXXXXXXXXXXXXXXXXXXXXXXX", // BotClient密钥
64+
"allowed-ip": "127.0.0.1", // 允许的BotClient IP
65+
"clientVersion": { // 客户端版本信息,低于该版本将拒绝连接
66+
"bds": "0.1.6",
67+
"spigot": "1.2.4",
68+
"nukkit": "1.0.2",
69+
"unknown": "",
70+
"endstone": "0.0.3",
71+
"pmmp": "1.0.0"
72+
}
73+
}
74+
```
75+
76+
2. 启动jar文件,无需特别参数(端口在2087)
77+
78+

build.gradle.kts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
plugins {
2+
kotlin("jvm") version "2.2.0"
3+
id("io.ktor.plugin") version "3.3.0"
4+
application
5+
}
6+
7+
group = "cn.huohuas001"
8+
version = "0.0.1"
9+
10+
val kotlin = "2.2.20"
11+
val ktor = "3.3.0"
12+
val logback = "1.4.14"
13+
val fastjson2 = "2.0.59"
14+
val slf4j = "2.0.13"
15+
val sqlite = "3.42.0.0"
16+
17+
18+
application {
19+
mainClass = "io.ktor.server.netty.EngineMain"
20+
}
21+
22+
dependencies {
23+
implementation("io.ktor:ktor-server-core-jvm:$ktor")
24+
implementation("io.ktor:ktor-server-netty:$ktor")
25+
implementation("io.ktor:ktor-server-websockets:$ktor")
26+
27+
implementation("ch.qos.logback:logback-classic:$logback")
28+
implementation("org.slf4j:slf4j-api:$slf4j")
29+
30+
implementation("io.ktor:ktor-server-config-yaml:$ktor")
31+
implementation("org.xerial:sqlite-jdbc:${sqlite}")
32+
33+
implementation("com.alibaba.fastjson2:fastjson2:$fastjson2")
34+
35+
36+
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin")
37+
testImplementation("io.ktor:ktor-server-test-host:$ktor")
38+
}
39+
40+
tasks.withType<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar> {
41+
// 启用最小化依赖
42+
minimize {
43+
exclude(dependency("org.slf4j:.*"))
44+
exclude(dependency("ch.qos.logback:.*"))
45+
}
46+
47+
// 排除不必要的文件
48+
exclude("META-INF/*.SF")
49+
exclude("META-INF/*.DSA")
50+
exclude("META-INF/*.RSA")
51+
exclude("META-INF/*.MF")
52+
53+
// 合并服务文件
54+
mergeServiceFiles()
55+
56+
// 压缩配置
57+
isZip64 = true
58+
}
59+
60+
61+

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kotlin.code.style=official

gradle/wrapper/gradle-wrapper.jar

42.7 KB
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.3-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
6+
zipStoreBase=GRADLE_USER_HOME
7+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)