cHttpServer 组件概述
简介
cHttpServer 是一个基于 VB6 的 HTTP 服务器组件,提供完整的 Web 服务器功能,支持路由、Session、Cookie、静态文件服务、SSE (Server-Sent Events)、跨域处理等特性。
特性
| 特性 | 说明 |
|---|---|
| HTTP 服务 | 支持 GET/POST/PUT/DELETE/OPTIONS 方法 |
| 路由系统 | 支持手动路由注册和自动路由 |
| Session 管理 | 支持内存、文件系统、数据库三种存储方式 |
| Cookie 处理 | 完整的 Cookie 解析和设置 |
| 静态文件 | 自动提供静态文件服务 |
| SSE 支持 | Server-Sent Events 实时推送 |
| 跨域处理 | 内置 CORS 跨域支持 |
| MVC 架构 | 支持控制器-视图-模式开发 |
| 粘包处理 | 自动处理 TCP 粘包问题 |
架构概览
┌─────────────────────────────────────────────────────────┐
│ cHttpServer │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Router │ │ Session │ │ SSE │ │
│ │ (路由系统) │ │ (会话管理) │ │(实时推送) │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Request │ │ Response │ │ Context │ │
│ │ (请求对象) │ │ (响应对象) │ │ (上下文) │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
└─────────────────────────────────────────────────────────┘快速开始
最小 HTTP 服务器
vb
Private WithEvents Server As cHttpServer
Private Sub Form_Load()
Set Server = New cHttpServer
' 注册路由
Call Server.Router.Reg("Home", New cHomeController)
Call Server.Router.Add("/", "Home@Index")
Call Server.Router.Add("/user", "Home@User", OnlyGet)
Call Server.Router.Add("/api/data", "Home@Api", OnlyPost)
' 启动服务器
If Server.Start(8080, "C:\WebRoot") Then
Debug.Print "服务器启动成功: http://localhost:8080"
Else
Debug.Print "启动失败: " & Server.LastError
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
Server.StopMe
End Sub
' 控制器示例
Public Sub Index(ctx As cHttpServerContext)
ctx.Response.Html "<h1>Hello World!</h1>"
End Sub
Public Sub Api(ctx As cHttpServerContext)
Dim data As New Scripting.Dictionary
data("message") = "Success"
data("time") = Now
ctx.Response.Json data, 0, "OK"
End SubSession 配置
vb
Private Sub Form_Load()
Set Server = New cHttpServer
' 配置 Session 存储方式
Server.SessionStorageType = SessionStorageFileSystem ' 文件存储
Server.SessionStoragePath = "C:\Sessions" ' 存储路径
Server.SessionCookieName = "MY_SESSIONID" ' Cookie 名称
' 或使用数据库存储
' Server.SessionStorageType = SessionStorageDatabase
' Server.SessionStoragePath = "sessions_table"
Call Server.Start(8080, "C:\WebRoot")
End Sub跨域配置
vb
Private Sub Form_Load()
Set Server = New cHttpServer
' 启用跨域
Server.CrossDomain.Enable = True
Server.CrossDomain.AllowOrigin = "*"
Server.CrossDomain.AllowMethods = "GET, POST, PUT, DELETE"
Server.CrossDomain.AllowHeaders = "Content-Type, Authorization"
Server.CrossDomain.AllowCredentials = True
Call Server.Start(8080)
End Sub文件结构
| 文件 | 说明 |
|---|---|
cHttpServer.cls | 主服务器类 |
cHttpServerRouter.cls | 路由器 |
cHttpServerRequest.cls | 请求对象 |
cHttpServerResponse.cls | 响应对象 |
cHttpServerSession.cls | Session 管理 |
cHttpServerCookies.cls | Cookie 管理 |
cHttpServerContext.cls | 上下文对象 |
cHttpServerClientInfo.cls | 客户端信息 |
cHttpServerRouteBefore.cls | 前置路由 |
cHttpServerRouterAfter.cls | 后置路由 |
cHttpCrossDomain.cls | 跨域处理 |
引用组件
cTlsReMaster.cls- TLS/网络通信cJson.cls- JSON 处理cScriptEngine.cls- 脚本引擎cDataBase.cls- 数据库(可选)cSSE.cls- SSE 支持
最后更新: 2026-05-17