Skip to content

Commit

Permalink
websocket ping
Browse files Browse the repository at this point in the history
  • Loading branch information
lz1998 committed Oct 22, 2020
1 parent ab958eb commit 74b4d70
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
22 changes: 21 additions & 1 deletion service/bot/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func ConnectUniversal(cli *client.QQClient) {
continue
} else {
log.Infof("已连接Websocket %v", WsUrl)
go ping(cli, conn)
go listenApi(cli, conn)
Conn = conn
time.Sleep(1 * time.Second)
Expand All @@ -53,15 +54,34 @@ func ConnectUniversal(cli *client.QQClient) {
}
}

func ping(cli *client.QQClient, conn *websocket.Conn) {
errCount := 0
for errCount < 5 {
if err := conn.WriteMessage(websocket.PingMessage, []byte("ping")); err != nil {
log.Warnf("websocket ping失败")
errCount++
} else {
errCount = 0
}
time.Sleep(10 * time.Second)
}
log.Warnf("websocket 连续ping失败5次,断开连接")
_ = conn.Close()
ConnectUniversal(cli)
}

func listenApi(cli *client.QQClient, conn *websocket.Conn) {
defer conn.Close()

for {
_, buf, err := conn.ReadMessage()
messageType, buf, err := conn.ReadMessage()
if err != nil {
log.Warnf("监听反向WS API时出现错误: %v", err)
break
}
if messageType == websocket.PingMessage || messageType == websocket.PongMessage {
continue
}

var req onebot.Frame
err = proto.Unmarshal(buf, &req)
Expand Down
2 changes: 1 addition & 1 deletion service/handler/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,5 @@ func CreateBotImpl(uin int64, password string) {

bot.ConnectUniversal(bot.Cli)

bot.SetRelogin(bot.Cli, 30, 30)
bot.SetRelogin(bot.Cli, 10, 30)
}

0 comments on commit 74b4d70

Please sign in to comment.