nonebot / aiocqhttp

A Python SDK with async I/O for CQHTTP (OneBot).
https://aiocqhttp.nonebot.dev
MIT License
280 stars 39 forks source link

fix error that indexes wrong websocket connection in call_action when… #43

Closed cleoold closed 3 years ago

cleoold commented 3 years ago

… in another irerelevant ws context

修復以下代碼報錯的問題:

    bot = CQHttp(...)

    @bot._server_app.websocket('/send_group_msg/<int:id>')
    async def sendmsg(id: int):
        while True:
            msg = await quart.websocket.receive()
            # ERROR vvv
            await bot.send_group_msg(group_id=id, message=msg)

    bot.run()
cleoold commented 3 years ago

这不太对吧…这里逻辑是从 event 里拿到 self id,然后去找 api ws,而不是把 event ws 当作 api ws

從全局導入的 event_ws 不一定是 onebot 的鏈接,也有可能是如代碼所示用戶自己的鏈接,此時就不一定有 self_id 之類的字段。可能需要再來一個集合來包括所有的 onebot event ws 做這個判斷。

stdrc commented 3 years ago

從全局導入的 event_ws 不一定是 onebot 的鏈接,也有可能是如代碼所示用戶自己的鏈接,此時就不一定有 self_id 之類的字段。可能需要再來一個集合來包括所有的 onebot event ws 做這個判斷。

就是应该这样,没有 X-Self-Id 就不应该是 onebot 连接,也就不应该放在 self._clients

    bot = CQHttp(...)

    @bot._server_app.websocket('/send_group_msg/<int:id>')
    async def sendmsg(id: int):
        while True:
            msg = await quart.websocket.receive()
            # ERROR vvv
            await bot.send_group_msg(group_id=id, message=msg)

    bot.run()

这个使用场景显然应该手动传参指定 self_id

cleoold commented 3 years ago

同樣的 有 X-self-id 也不代表這是 onebot 連接

On Fri., Jan. 8, 2021, 20:45 Richard Chien, notifications@github.com wrote:

從全局導入的 event_ws 不一定是 onebot 的鏈接,也有可能是如代碼所示用戶自己的鏈接,此時就不一定有 self_id 之類的字段。可能需要再來一個集合來包括所有的 onebot event ws 做這個判斷。

就是应该这样,没有 X-Self-Id 就不应该是 onebot 连接,也就不应该放在 self._clients 里

bot = CQHttp(...)

@bot._server_app.websocket('/send_group_msg/<int:id>')

async def sendmsg(id: int):

    while True:

        msg = await quart.websocket.receive()

        # ERROR vvv

        await bot.send_group_msg(group_id=id, message=msg)

bot.run()

这个使用场景显然应该手动传参指定 self_id

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/nonebot/aiocqhttp/pull/43#issuecomment-757074912, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADKGVBYVQ3KJ4T7AVPSWBY3SY6YJZANCNFSM4VPCAJQA .

stdrc commented 3 years ago

哦不过,直接获取 X-Self-Id 对于一般 ws 确实就 KeyError 了

可能需要再來一個集合來包括所有的 onebot event ws 做這個判斷

这个有一些道理,现在 event client 是没有存的,不过是不是真的有必要呢,我还是希望 aiocqhttp 保持简单,似乎没有必要搞太复杂,你所列的这个场景,我相信会用到的用户不到千分之一

stdrc commented 3 years ago

也许有更好的办法,比如在 _handle_wsr 里面给这个 ws object 做个标记,不一定要存下来(