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 f636679 commit e2de04f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@


> [main.go](https://github.com/jxhczhl/JsRpc/blob/main/main.go) (服务器的主代码)
> [/resouces/JsEnv_De.js](https://github.com/jxhczhl/JsRpc/blob/main/resouces/JsEnv_Dev.js) (客户端注入js环境)
> [resouces/JsEnv_De.js](https://github.com/jxhczhl/JsRpc/blob/main/resouces/JsEnv_Dev.js) (客户端注入js环境)
> [config.yaml](https://github.com/jxhczhl/JsRpc/blob/main/config.yaml) (可选配置文件)

Expand All @@ -62,8 +62,7 @@

<img width="570" alt="image" src="https://github.com/jxhczhl/JsRpc/assets/41224971/2530274f-33b9-4ccd-8749-6431abea27b2">

如需更改部分配置,请下载config.yaml文件 并且放在服务程序的同级路径
[config.yaml](https://github.com/jxhczhl/JsRpc/blob/main/config.yaml)
[如需更改部分配置,请查看 "其他说明"](#其他说明)

**api 简介**

Expand Down Expand Up @@ -220,6 +219,8 @@ list接口可查看当前注入的客户端信息
## 其他说明
如果需要更改rpc服务的一些配置 比如端口号啊,https/wss服务,打印日志等
可以在执行文件的同路径 下载[config.yaml]([链接地址](https://github.com/jxhczhl/JsRpc/blob/main/config.yaml))文件配置
或使用-c参数指定配置文件路径
./JsRpc.exe -c config1.yaml
![image](https://github.com/jxhczhl/JsRpc/assets/41224971/ad023b16-65b5-418e-8494-e988bb02fb12)

group说明
Expand Down
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
func InitConf(path string, conf *ConfStruct) (err error) {
fileContent, err := os.ReadFile(path)
if err != nil {
return
return err
}
if err = yaml.Unmarshal(fileContent, &conf); err != nil {
return
return err
}
return
}
Expand Down
41 changes: 22 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"JsRpc/config"
"encoding/json"
"flag"
"fmt"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
Expand Down Expand Up @@ -193,13 +194,15 @@ func GetResult(c *gin.Context) {
if group == "" {
GinJsonMsg(c, http.StatusBadRequest, "需要传入group")
}
groupClients := make(map[string]*Clients)
groupClients := make([]*Clients, 0)
//循环读取syncMap 获取group名字的
hlSyncMap.Range(func(key, value interface{}) bool {
k := key.(string)
if strings.HasPrefix(k, group+"->") {
client := value.(*Clients)
groupClients[k] = client
hlSyncMap.Range(func(_, value interface{}) bool {
tmpClients, ok := value.(*Clients)
if !ok {
return true
}
if tmpClients.clientGroup == group {
groupClients = append(groupClients, tmpClients)
}
return true
})
Expand All @@ -216,15 +219,10 @@ func GetResult(c *gin.Context) {
var client *Clients
// 不传递clientId时候,从group分组随便拿一个
if clientId == "" {
groupKeys := make([]string, 0, len(groupClients))
for k := range groupClients {
groupKeys = append(groupKeys, k)
}
// 使用随机数发生器
r := rand.New(rand.NewSource(time.Now().UnixNano()))
randomIndex := r.Intn(len(groupKeys)) // 随机拿一个group开头的
clientName := groupKeys[randomIndex] // group->clientId
client = groupClients[clientName]
randomIndex := r.Intn(len(groupClients))
client = groupClients[randomIndex]

} else {
clientName, ok := hlSyncMap.Load(group + "->" + clientId)
Expand Down Expand Up @@ -310,13 +308,16 @@ func TlsHandler(HttpsHost string) gin.HandlerFunc {
func main() {
JsRpc := " __ _______..______ .______ ______ \n | | / || _ \\ | _ \\ / |\n | | | (----`| |_) | | |_) | | ,----'\n.--. | | \\ \\ | / | ___/ | | \n| `--' | .----) | | |\\ \\----.| | | `----.\n \\______/ |_______/ | _| `._____|| _| \\______|\n \n"
fmt.Print(JsRpc)

log.SetFormatter(&log.TextFormatter{
ForceColors: true, // 强制终端输出带颜色日志
FullTimestamp: true, // 显示完整时间戳
TimestampFormat: "2006-01-02 15:04:05",
})
ConfigPath := "./config.yaml"
var ConfigPath string
// 定义命令行参数-c,后面跟着的是默认值以及参数说明
flag.StringVar(&ConfigPath, "c", "config.yaml", "指定配置文件的路径")
// 解析命令行参数
flag.Parse()
MainConf := config.ConfStruct{
BasicListen: `:12080`,
HttpsServices: config.HttpsConfig{
Expand All @@ -325,8 +326,10 @@ func main() {
},
DefaultTimeOut: defaultTimeout,
}
_ = config.InitConf(ConfigPath, &MainConf)

err := config.InitConf(ConfigPath, &MainConf)
if err != nil {
log.Error("读取配置文件错误,将使用默认配置运行。 ", err.Error())
}
if MainConf.CloseWebLog {
// 将默认的日志输出器设置为空
gin.DefaultWriter = logWriter{}
Expand Down Expand Up @@ -364,7 +367,7 @@ func main() {
sb.WriteString("当前监听地址:")
sb.WriteString(MainConf.BasicListen)

sb.WriteString(" tls启用状态:")
sb.WriteString(" ssl启用状态:")
sb.WriteString(strconv.FormatBool(MainConf.HttpsServices.IsEnable))

if MainConf.HttpsServices.IsEnable {
Expand All @@ -373,7 +376,7 @@ func main() {
}
log.Infoln(sb.String())

err := r.Run(MainConf.BasicListen)
err = r.Run(MainConf.BasicListen)
if err != nil {
log.Error(err)
}
Expand Down

0 comments on commit e2de04f

Please sign in to comment.