From 54f8d69b50425c2585c959e56a75ac4abcef4f7f Mon Sep 17 00:00:00 2001 From: nieqing <919624032@qq.com> Date: Mon, 30 Aug 2021 09:35:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=B6=88=E6=81=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/bot.go b/bot.go index ea961b5..b22cdb8 100644 --- a/bot.go +++ b/bot.go @@ -263,14 +263,10 @@ func (b *Bot) getNewWechatMessage() error { } // 更新SyncKey并且重新存入storage b.Storage.Response.SyncKey = resp.SyncKey - // 遍历所有的新的消息,依次处理 - for _, message := range resp.AddMsgList { - // 根据不同的消息类型来进行处理,方便后续统一调用 - message.init(b) - // 调用自定义的处理方法 - if handler := b.MessageHandler; handler != nil { - handler(message) - } + // 异步执行,提升响应速度 + // 避免单个消息处理函数阻塞,让其他的消息得不到处理 + if b.MessageHandler != nil { + go b.handleMessage(resp.AddMsgList) } return nil } @@ -370,3 +366,12 @@ func PrintlnQrcodeUrl(uuid string) { println("访问下面网址扫描二维码登录") println(GetQrcodeUrl(uuid)) } + +func (b *Bot) handleMessage(messageList []*Message) { + for _, message := range messageList { + // 根据不同的消息类型来进行处理,方便后续统一调用 + message.init(b) + // 调用自定义的处理方法 + b.MessageHandler(message) + } +}