-
Notifications
You must be signed in to change notification settings - Fork 41
feat: support forward ws #74
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,8 +19,8 @@ | |||||||||||
| from quart import Quart, request, abort, jsonify, websocket, Response | ||||||||||||
|
|
||||||||||||
| from .api import AsyncApi, SyncApi | ||||||||||||
| from .api_impl import (SyncWrapperApi, HttpApi, WebSocketReverseApi, | ||||||||||||
| UnifiedApi, ResultStore) | ||||||||||||
| from .api_impl import (SyncWrapperApi, HttpApi, WebSocketReverseApi, UnifiedApi, | ||||||||||||
| ResultStore, WebSocketForwardApi, _is_websocket_url) | ||||||||||||
| from .bus import EventBus | ||||||||||||
| from .exceptions import Error, TimingError | ||||||||||||
| from .event import Event | ||||||||||||
|
|
@@ -105,8 +105,10 @@ def __init__(self, | |||||||||||
| ``import_name`` 参数为当前模块(使用 `CQHttp` 的模块)的导入名,通常传入 | ||||||||||||
| ``__name__`` 或不传入。 | ||||||||||||
|
|
||||||||||||
| ``api_root`` 参数为 OneBot API 的 URL,``access_token`` 和 | ||||||||||||
| ``secret`` 参数为 OneBot 配置中填写的对应项。 | ||||||||||||
| ``api_root`` 参数为 OneBot API 的地址,支持 HTTP URL(如 | ||||||||||||
| ``http://127.0.0.1:5700``)或正向 WebSocket URL(如 | ||||||||||||
| ``ws://127.0.0.1:6700/``)。``access_token`` 和 ``secret`` 参数为 OneBot | ||||||||||||
| 配置中填写的对应项。 | ||||||||||||
|
|
||||||||||||
| ``message_class`` 参数为要用来对 `Event.message` 进行转换的消息类,可使用 | ||||||||||||
| `Message`,例如: | ||||||||||||
|
|
@@ -155,12 +157,35 @@ def _configure(self, | |||||||||||
| api_timeout_sec = api_timeout_sec or 60 # wait for 60 secs by default | ||||||||||||
| self._access_token = access_token | ||||||||||||
| self._secret = secret | ||||||||||||
| self._api._http_api = HttpApi(api_root, access_token, api_timeout_sec) | ||||||||||||
|
|
||||||||||||
| # Configure API implementations based on api_root type | ||||||||||||
| http_api = None | ||||||||||||
| wsf_api = None | ||||||||||||
|
|
||||||||||||
| if _is_websocket_url(api_root): | ||||||||||||
| # Forward WebSocket mode | ||||||||||||
| try: | ||||||||||||
| wsf_api = WebSocketForwardApi(ws_url=api_root, | ||||||||||||
| access_token=access_token, | ||||||||||||
| timeout_sec=api_timeout_sec, | ||||||||||||
| event_handler=self._handle_event) | ||||||||||||
| except ImportError as e: | ||||||||||||
| self.logger.error(f"Failed to create WebSocketForwardApi: {e}") | ||||||||||||
|
||||||||||||
| self.logger.error(f"Failed to create WebSocketForwardApi: {e}") | |
| self.logger.error(f"Failed to create WebSocketForwardApi: {e}") | |
| # Avoid leaving the unified API in a potentially inconsistent state | |
| # if forward WebSocket support cannot be initialized. | |
| self._api = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里建议 catch 所有 Exception 然后日志里会显示具体原因