Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
jxhczhl committed Apr 21, 2024
1 parent a747b24 commit 80249a6
Showing 1 changed file with 22 additions and 33 deletions.
55 changes: 22 additions & 33 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,29 +190,17 @@ func GetResult(c *gin.Context) {
GinJsonMsg(c, http.StatusBadRequest, "需要传入group")
return
}
groupClients := make([]*Clients, 0)
//循环读取syncMap 获取group名字的
hlSyncMap.Range(func(_, value interface{}) bool {
tmpClients, ok := value.(*Clients)
if !ok {
return true
}
if tmpClients.clientGroup == group {
groupClients = append(groupClients, tmpClients)
}
return true
})
if len(groupClients) == 0 {
GinJsonMsg(c, http.StatusBadRequest, "没有找到注入的group:"+group)
return
}
action := RequestParam.Action
if action == "" {
GinJsonMsg(c, http.StatusOK, "请传入action来调用客户端方法")
return
}
clientId := RequestParam.ClientId
client := GetRandomClient(group, clientId)
if client == nil {
GinJsonMsg(c, http.StatusBadRequest, "没有找到对应的group或clientId,请通过list接口查看现有的注入")
return
}
c2 := make(chan string, 1)
go GQueryFunc(client, action, RequestParam.Param, c2)
//把管道传过去,获得值就返回了
Expand All @@ -221,6 +209,16 @@ func GetResult(c *gin.Context) {
}

func GetRandomClient(group string, clientId string) *Clients {
var client *Clients
// 不传递clientId时候,从group分组随便拿一个
if clientId != "" {
clientName, ok := hlSyncMap.Load(group + "->" + clientId)
if ok == false {
return nil
}
client, _ = clientName.(*Clients)
return client
}
groupClients := make([]*Clients, 0)
//循环读取syncMap 获取group名字的
hlSyncMap.Range(func(_, value interface{}) bool {
Expand All @@ -236,23 +234,10 @@ func GetRandomClient(group string, clientId string) *Clients {
if len(groupClients) == 0 {
return nil
}

var client *Clients
// 不传递clientId时候,从group分组随便拿一个
if clientId == "" {
// 使用随机数发生器
r := rand.New(rand.NewSource(time.Now().UnixNano()))
randomIndex := r.Intn(len(groupClients))
client = groupClients[randomIndex]

} else {
clientName, ok := hlSyncMap.Load(group + "->" + clientId)
if ok == false {
return nil
}
//取一个ws客户端
client, _ = clientName.(*Clients)
}
// 使用随机数发生器
r := rand.New(rand.NewSource(time.Now().UnixNano()))
randomIndex := r.Intn(len(groupClients))
client = groupClients[randomIndex]
return client

}
Expand All @@ -277,6 +262,10 @@ func Execjs(c *gin.Context) {
}
clientId := RequestParam.ClientId
client := GetRandomClient(group, clientId)
if client == nil {
GinJsonMsg(c, http.StatusBadRequest, "没有找到对应的group或clientId,请通过list接口查看现有的注入")
return
}
c2 := make(chan string)
go GQueryFunc(client, Action, JsCode, c2)
c.JSON(200, gin.H{"status": "200", "group": client.clientGroup, "name": client.clientId, "data": <-c2})
Expand Down

0 comments on commit 80249a6

Please sign in to comment.