1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-05 03:23:49 +08:00

try to fix reverse ws delay. #52

This commit is contained in:
Mrs4s 2020-08-09 05:05:38 +08:00
parent 8524fbf1ce
commit c87836355f
2 changed files with 17 additions and 3 deletions

View File

@ -14,6 +14,7 @@ import (
"hash/crc32"
"path"
"sync"
"time"
)
type CQBot struct {
@ -173,7 +174,15 @@ func (bot *CQBot) Release() {
func (bot *CQBot) dispatchEventMessage(m MSG) {
for _, f := range bot.events {
f(m)
fn := f
go func() {
start := time.Now()
fn(m)
end := time.Now()
if end.Sub(start) > time.Second*5 {
log.Debugf("警告: 事件处理耗时超过 5 秒 (%v秒), 请检查应用是否有堵塞.", end.Sub(start)/time.Second)
}
}()
}
}

View File

@ -136,6 +136,7 @@ func (c *websocketClient) connectUniversal() {
log.Warnf("连接到反向Websocket Universal服务器 %v 时出现致命错误: %v", c.conf.ReverseUrl, err)
return
}
wsConf.Dialer.Timeout = time.Second * 5
wsConf.Header["X-Client-Role"] = []string{"Universal"}
wsConf.Header["X-Self-ID"] = []string{strconv.FormatInt(c.bot.Client.Uin, 10)}
wsConf.Header["User-Agent"] = []string{"CQHttp/4.15.0"}
@ -191,16 +192,20 @@ func (c *websocketClient) onBotPushEvent(m coolq.MSG) {
defer c.pushLock.Unlock()
if c.eventConn != nil {
log.Debugf("向WS服务器 %v 推送Event: %v", c.eventConn.RemoteAddr().String(), m.ToJson())
_ = c.eventConn.SetWriteDeadline(time.Now().Add(time.Second * 3))
if _, err := c.eventConn.Write([]byte(m.ToJson())); err != nil {
_ = c.eventConn.Close()
if c.conf.ReverseReconnectInterval != 0 {
time.Sleep(time.Millisecond * time.Duration(c.conf.ReverseReconnectInterval))
c.connectEvent()
go func() {
time.Sleep(time.Millisecond * time.Duration(c.conf.ReverseReconnectInterval))
c.connectEvent()
}()
}
}
}
if c.universalConn != nil {
log.Debugf("向WS服务器 %v 推送Event: %v", c.universalConn.RemoteAddr().String(), m.ToJson())
_ = c.universalConn.SetWriteDeadline(time.Now().Add(time.Second * 3))
_, _ = c.universalConn.Write([]byte(m.ToJson()))
}
}