Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP SSE 消息上报模式 #701

Merged
merged 2 commits into from
Jan 13, 2025
Merged

HTTP SSE 消息上报模式 #701

merged 2 commits into from
Jan 13, 2025

Conversation

Stapxs
Copy link
Collaborator

@Stapxs Stapxs commented Jan 9, 2025

支持使用更轻量的 http sse 进行消息上报,作为 http client 反向连接的可选替代品。

image

新增连接器:httpSseServers

配置文件继承自 httpServers,增加 reportSelfMessage 配置:

"httpSseServers": [
  {
    "name": "http-sse",
    "enable": true,
    "port": 3000,
    "host": "0.0.0.0",
    "enableCors": true,
    "enableWebsocket": false,
    "messagePostFormat": "array",
    "token": "",
    "debug": false,
    "reportSelfMessage": true,
    "type": "HTTP-SSE 服务器"
  }
]

Summary by Sourcery

新功能:

  • 引入 HTTP SSE 作为消息报告的轻量级 HTTP 客户端反向连接替代方案。
Original summary in English

Summary by Sourcery

New Features:

  • Introduce HTTP SSE as a lightweight alternative to HTTP client reverse connections for message reporting.

Copy link

sourcery-ai bot commented Jan 9, 2025

审阅者指南 by Sourcery

该拉取请求实现了一种新的 HTTP SSE 消息报告模式,作为现有 HTTP 客户端反向连接方法的替代方案。它引入了一个新的 "httpSseServers" 连接器配置,类似于 "httpServers" 配置,并增加了 "reportSelfMessage" 选项。实现包括处理 SSE 连接、事件分发以及与现有 OneBot 组件的集成。

HTTP SSE 消息报告流程的序列图

sequenceDiagram
    participant Client as SSE Client
    participant Server as HTTP SSE Server
    participant Adapter as OB11ActiveHttpSSEAdapter
    participant Core as NapCat Core

    Client->>Server: GET /_events
    Server->>Adapter: createSseSupport()
    Adapter->>Client: Establish SSE Connection
    Note over Adapter: Add client to sseClients[]

    loop Event Emission
        Core->>Adapter: onEvent(event)
        loop For each connected client
            Adapter->>Client: Send SSE event data
        end
    end

    Client-->>Adapter: Connection closed
    Note over Adapter: Remove client from sseClients[]
Loading

HTTP SSE 实现的类图

classDiagram
    class IOB11NetworkAdapter {
        <<interface>>
        +onEvent(event)
    }
    class OB11PassiveHttpAdapter {
        +handleRequest(req, res)
        +onEvent(event)
    }
    class OB11ActiveHttpSSEAdapter {
        -sseClients: Response[]
        +handleRequest(req, res)
        +onEvent(event)
        -createSseSupport(req, res)
    }
    class HttpSseServerConfig {
        +name: string
        +enable: boolean
        +reportSelfMessage: boolean
    }

    IOB11NetworkAdapter <|-- OB11PassiveHttpAdapter
    OB11PassiveHttpAdapter <|-- OB11ActiveHttpSSEAdapter
    OB11ActiveHttpSSEAdapter ..> HttpSseServerConfig : uses
Loading

文件级变更

变更 详情 文件
添加新的 httpSseServers 连接器配置。
  • 添加 httpSseServerDefaultConfigs 以定义 HTTP SSE 服务器的默认配置。
  • httpSseServers 属性添加到 NetworkConfig 接口。
  • defaultOneBotConfigs 中包含 httpSseServers 属性。
src/onebot/config/config.ts
实现 HTTP SSE 服务器逻辑。
  • 创建新的 OB11ActiveHttpSSEAdapter 类以处理 HTTP SSE 连接和事件。
  • 在启用 httpSseServers 时,在 NapCatOneBot11Adapter 类中注册 OB11ActiveHttpSSEAdapter
  • 实现 handleRequest 方法以处理传入的请求,包括 SSE 连接和常规 API 请求。
  • 实现 createSseSupport 方法以管理 SSE 连接并向连接的客户端发送事件。
  • 实现 onEvent 方法以将事件分发到 SSE 客户端。
src/onebot/network/active-http-sse.ts
src/onebot/index.ts
将 HTTP SSE 服务器与现有 OneBot 组件集成。
  • NapCatOneBot11Adapter 类中为 HTTP SSE 服务器添加日志记录。
  • 修改 OB11PassiveHttpAdapter 中的 handleRequest 方法,以通用方式处理请求并支持 SSE 连接。
  • 更新 OB11PassiveHttpAdapteronEvent 方法的签名以支持通用事件类型。
src/onebot/index.ts
src/onebot/network/passive-http.ts

提示和命令

与 Sourcery 交互

  • 触发新的审阅: 在拉取请求中评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审阅评论。
  • 从审阅评论生成 GitHub 问题: 通过回复评论,要求 Sourcery 创建一个问题。
  • 生成拉取请求标题: 在拉取请求标题的任何位置写 @sourcery-ai 以随时生成标题。
  • 生成拉取请求摘要: 在拉取请求正文的任何位置写 @sourcery-ai summary 以随时生成 PR 摘要。您还可以使用此命令指定摘要的插入位置。

自定义您的体验

访问您的仪表板以:

  • 启用或禁用审阅功能,如 Sourcery 生成的拉取请求摘要、审阅者指南等。
  • 更改审阅语言。
  • 添加、删除或编辑自定义审阅说明。
  • 调整其他审阅设置。

获取帮助

Original review guide in English

Reviewer's Guide by Sourcery

This pull request implements a new HTTP SSE message reporting mode as an alternative to the existing HTTP client reverse connection method. It introduces a new "httpSseServers" connector configuration, similar to the "httpServers" configuration, with an additional "reportSelfMessage" option. The implementation includes handling SSE connections, event dispatching, and integration with existing OneBot components.

Sequence diagram for HTTP SSE message reporting flow

sequenceDiagram
    participant Client as SSE Client
    participant Server as HTTP SSE Server
    participant Adapter as OB11ActiveHttpSSEAdapter
    participant Core as NapCat Core

    Client->>Server: GET /_events
    Server->>Adapter: createSseSupport()
    Adapter->>Client: Establish SSE Connection
    Note over Adapter: Add client to sseClients[]

    loop Event Emission
        Core->>Adapter: onEvent(event)
        loop For each connected client
            Adapter->>Client: Send SSE event data
        end
    end

    Client-->>Adapter: Connection closed
    Note over Adapter: Remove client from sseClients[]
Loading

Class diagram for HTTP SSE implementation

classDiagram
    class IOB11NetworkAdapter {
        <<interface>>
        +onEvent(event)
    }
    class OB11PassiveHttpAdapter {
        +handleRequest(req, res)
        +onEvent(event)
    }
    class OB11ActiveHttpSSEAdapter {
        -sseClients: Response[]
        +handleRequest(req, res)
        +onEvent(event)
        -createSseSupport(req, res)
    }
    class HttpSseServerConfig {
        +name: string
        +enable: boolean
        +reportSelfMessage: boolean
    }

    IOB11NetworkAdapter <|-- OB11PassiveHttpAdapter
    OB11PassiveHttpAdapter <|-- OB11ActiveHttpSSEAdapter
    OB11ActiveHttpSSEAdapter ..> HttpSseServerConfig : uses
Loading

File-Level Changes

Change Details Files
Added a new httpSseServers connector configuration.
  • Added httpSseServerDefaultConfigs to define the default configuration for HTTP SSE servers.
  • Added the httpSseServers property to the NetworkConfig interface.
  • Included the httpSseServers property in the defaultOneBotConfigs.
src/onebot/config/config.ts
Implemented the HTTP SSE server logic.
  • Created a new class OB11ActiveHttpSSEAdapter to handle HTTP SSE connections and events.
  • Registered the OB11ActiveHttpSSEAdapter in the NapCatOneBot11Adapter class when the httpSseServers are enabled.
  • Implemented the handleRequest method to handle incoming requests, including SSE connections and regular API requests.
  • Implemented the createSseSupport method to manage SSE connections and send events to connected clients.
  • Implemented the onEvent method to dispatch events to SSE clients.
src/onebot/network/active-http-sse.ts
src/onebot/index.ts
Integrated the HTTP SSE server with the existing OneBot components.
  • Added logging for HTTP SSE servers in the NapCatOneBot11Adapter class.
  • Modified the handleRequest method in OB11PassiveHttpAdapter to handle requests generically and support SSE connections.
  • Updated the onEvent method signature in OB11PassiveHttpAdapter to support generic event types.
src/onebot/index.ts
src/onebot/network/passive-http.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Stapxs - 我已经审查了你的更改并发现了一些需要解决的问题。

阻塞性问题

  • HTTP 方法比较应该使用大写的 'GET'(链接

整体评论

  • 考虑为 SSE 客户端实施最大连接限制,以防止无界连接可能导致的内存问题
  • 为 SSE 写入操作添加错误处理,以优雅地处理写入失败并防止影响其他客户端连接
以下是我在审查期间关注的内容
  • 🔴 一般性问题:1 个阻塞性问题,1 个其他问题
  • 🟢 安全性:一切看起来都很好
  • 🟢 测试:一切看起来都很好
  • 🟢 复杂性:一切看起来都很好
  • 🟢 文档:一切看起来都很好

Sourcery 对开源项目是免费的 - 如果你喜欢我们的评论,请考虑分享 ✨
帮助我变得更有用!请在每条评论上点击 👍 或 👎,我将使用这些反馈来改进你的评论。
Original comment in English

Hey @Stapxs - I've reviewed your changes and found some issues that need to be addressed.

Blocking issues:

  • HTTP method comparison should use uppercase 'GET' (link)

Overall Comments:

  • Consider implementing a maximum limit for SSE clients to prevent potential memory issues with unbounded connections
  • Add error handling for SSE write operations to gracefully handle failed writes and prevent affecting other client connections
Here's what I looked at during the review
  • 🔴 General issues: 1 blocking issue, 1 other issue
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

src/onebot/network/active-http-sse.ts Show resolved Hide resolved
src/onebot/network/active-http-sse.ts Outdated Show resolved Hide resolved
Copy link

sonarqubecloud bot commented Jan 9, 2025

@MliKiowa MliKiowa self-assigned this Jan 13, 2025
@MliKiowa MliKiowa added the enhancement New feature or request label Jan 13, 2025
@MliKiowa MliKiowa merged commit 3171640 into NapNeko:main Jan 13, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants