Skip to content

Commit

Permalink
不再需要在配置文件中配置qq账号
Browse files Browse the repository at this point in the history
修复gui窗口刷新后不能接受的信息的bug
  • Loading branch information
huoxue1 committed Jun 27, 2021
1 parent d6fa787 commit f3a6037
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 37 deletions.
75 changes: 44 additions & 31 deletions gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ var upGrader = websocket.Upgrader{
},
}

var (
logConn *websocket.Conn
dataCoon *websocket.Conn
)

func InitWindow() {
defer func() {
err := recover()
Expand Down Expand Up @@ -104,16 +109,20 @@ func InitWindow() {
log.Errorln(err)
return
}
go func() {
for {
event := <-hook.LogChan
err = conn.WriteMessage(websocket.TextMessage, []byte(event))
if err != nil {
log.Debugln("前端日志消息发送失败" + err.Error())
continue
if logConn == nil {
logConn = conn
go func() {
for {
event := <-hook.LogChan
err = logConn.WriteMessage(websocket.TextMessage, []byte(event))
if err != nil {
log.Debugln("前端日志消息发送失败" + err.Error())
continue
}
}
}
}()
}()
}
logConn = conn
})

engine.POST("/send_msg", CallApi)
Expand All @@ -131,32 +140,36 @@ func data(ctx *gin.Context) {
return
}

go func() {
for {
_, message, err := conn.ReadMessage()
if err != nil {
log.Debugln("接收消息失败" + err.Error())
break
}
if string(message) == "ping" {
message = []byte("pong")
if dataCoon == nil {
dataCoon = conn
go func() {
for {
_, m, err := dataCoon.ReadMessage()
if err != nil {
log.Debugln("接收消息失败" + err.Error())
break
}
if string(m) == "ping" {
m = []byte("pong")
}
//写入ws数据
}
//写入ws数据
}
}()
}()

go func() {
for {
event := <-MessageChan
log.Debugln("已向前端发送信息")
err = conn.WriteJSON(&event)
if err != nil {
log.Debugln("消息发送失败" + err.Error())
continue
go func() {
for {
event := <-MessageChan
log.Debugln("已向前端发送信息")
err = dataCoon.WriteJSON(&event)
if err != nil {
log.Debugln("消息发送失败" + err.Error())
continue
}
}
}

}()
}()
}
dataCoon = conn

}

Expand Down
2 changes: 1 addition & 1 deletion init.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func initConfig(path string, fileType string) error {

hook.AddLevel(utils.GetLogLevel(DefaultConfig.LogLevel)...)
log.Infoln("已加载配置:" + string(data))
log.SetLevel(log.DebugLevel)
//log.SetLevel(log.DebugLevel)
return err
}

Expand Down
20 changes: 15 additions & 5 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,23 @@ func EventHandle(w http.ResponseWriter, r *http.Request) {
closeChan: make(chan byte),
}

// 将所有bot实例的client对象初始化
for _, bot := range DefaultConfig.Bots {
if bot.SelfId == selfId {
log.Infoln("bot:" + strconv.Itoa(bot.SelfId) + "已上线")
bot.Client = wscon
// 判断bot列表里面是否存在该bot,不存在则append进去,存在则将ws链接进行赋值
if a := func() bool {
for _, bot := range DefaultConfig.Bots {
if bot.SelfId == selfId {
bot.Client = wscon
return true
}
}
return false
}(); !a {
DefaultConfig.Bots = append(DefaultConfig.Bots, &Bot{
Name: "",
SelfId: selfId,
Client: wscon,
})
}
log.Infoln("bot:" + strconv.Itoa(selfId) + "已上线")
// 处理所有的connect事件
for _, handle := range ConnectHandles {
handle.handle(Connect{SelfID: selfId, ClientRole: role, Host: host}, GetBotById(selfId))
Expand Down

0 comments on commit f3a6037

Please sign in to comment.