Skip to content

Commit d000064

Browse files
committed
update
1 parent a7cd842 commit d000064

File tree

4 files changed

+50
-47
lines changed

4 files changed

+50
-47
lines changed

core/api.go

+25-18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package core
33
import (
44
"JsRpc/config"
55
"JsRpc/utils"
6+
"encoding/json"
67
"github.com/gin-gonic/gin"
78
"github.com/gorilla/websocket"
89
log "github.com/sirupsen/logrus"
@@ -23,10 +24,15 @@ var (
2324

2425
// Message 请求和传递请求
2526
type Message struct {
26-
Action string `json:"action"`
27-
Param string `json:"param"`
27+
Action string `json:"action"`
28+
MessageId string `json:"message_id"`
29+
Param string `json:"param"`
30+
}
31+
type MessageResponse struct {
32+
Action string `json:"action"`
33+
MessageId string `json:"message_id"`
34+
ResponseData string `json:"response_data"`
2835
}
29-
3036
type ApiParam struct {
3137
GroupName string `form:"group" json:"group"`
3238
ClientId string `form:"clientId" json:"clientId"`
@@ -39,7 +45,7 @@ type ApiParam struct {
3945
type Clients struct {
4046
clientGroup string
4147
clientId string
42-
actionData map[string]chan string
48+
actionData map[string]map[string]chan string // {"action":{"消息id":消息管道}}
4349
clientWs *websocket.Conn
4450
}
4551

@@ -48,7 +54,7 @@ func NewClient(group string, uid string, ws *websocket.Conn) *Clients {
4854
return &Clients{
4955
clientGroup: group,
5056
clientId: uid,
51-
actionData: make(map[string]chan string, 1), // action有消息后就保存到chan里
57+
actionData: make(map[string]map[string]chan string), // action有消息后就保存到chan里
5258
clientWs: ws,
5359
}
5460
}
@@ -83,20 +89,21 @@ func ws(c *gin.Context) {
8389
if err != nil {
8490
break
8591
}
86-
msg := string(message)
87-
check := []uint8{104, 108, 94, 95, 94}
88-
strIndex := strings.Index(msg, string(check))
89-
if strIndex >= 1 {
90-
action := msg[:strIndex]
91-
client.actionData[action] <- msg[strIndex+5:]
92-
if len(msg) > 100 {
93-
utils.LogPrint("get_message:", msg[strIndex+5:101]+"......")
94-
} else {
95-
utils.LogPrint("get_message:", msg[strIndex+5:])
96-
}
97-
92+
// 将得到的数据转成结构体
93+
messageStruct := MessageResponse{}
94+
err = json.Unmarshal(message, &messageStruct)
95+
if err != nil {
96+
log.Error("接收到的消息不是设定的格式 不做处理", err)
97+
}
98+
action := messageStruct.Action
99+
messageId := messageStruct.MessageId
100+
msg := messageStruct.ResponseData
101+
// 这里直接给管道塞数据,那么之前发送的时候要初始化好
102+
client.actionData[action][messageId] <- msg
103+
if len(msg) > 100 {
104+
utils.LogPrint("get_message:", msg[:101]+"......")
98105
} else {
99-
log.Error(msg, "message error")
106+
utils.LogPrint("get_message:", msg)
100107
}
101108

102109
}

core/engine.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package core
22

33
import (
44
"JsRpc/config"
5+
"JsRpc/utils"
56
"encoding/json"
67
"fmt"
78
"math/rand"
@@ -10,11 +11,16 @@ import (
1011

1112
// GQueryFunc 发送请求到客户端
1213
func (c *Clients) GQueryFunc(funcName string, param string, resChan chan<- string) {
13-
WriteData := Message{Param: param, Action: funcName}
14+
MessageId := utils.GetUUID()
15+
WriteData := Message{Param: param, MessageId: MessageId, Action: funcName}
1416
data, _ := json.Marshal(WriteData)
1517
clientWs := c.clientWs
18+
// 先判断action是否需要初始化
1619
if c.actionData[funcName] == nil {
17-
c.actionData[funcName] = make(chan string, 1) //此次action初始化1个消息
20+
c.actionData[funcName] = make(map[string]chan string)
21+
}
22+
if c.actionData[funcName][MessageId] == nil {
23+
c.actionData[funcName][MessageId] = make(chan string, 1) //此次action初始化1个消息
1824
}
1925
gm.Lock()
2026
err := clientWs.WriteMessage(1, data)
@@ -24,8 +30,8 @@ func (c *Clients) GQueryFunc(funcName string, param string, resChan chan<- strin
2430
}
2531
resultFlag := false
2632
for i := 0; i < config.DefaultTimeout*10; i++ {
27-
if len(c.actionData[funcName]) > 0 {
28-
res := <-c.actionData[funcName]
33+
if len(c.actionData[funcName][MessageId]) > 0 {
34+
res := <-c.actionData[funcName][MessageId]
2935
resChan <- res
3036
resultFlag = true
3137
break
@@ -38,6 +44,7 @@ func (c *Clients) GQueryFunc(funcName string, param string, resChan chan<- strin
3844
}
3945
defer func() {
4046
close(resChan)
47+
delete(c.actionData[funcName], MessageId)
4148
}()
4249
}
4350

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.22.1
44

55
require (
66
github.com/gin-gonic/gin v1.9.1
7+
github.com/google/uuid v1.6.0
78
github.com/gorilla/websocket v1.5.1
89
github.com/sirupsen/logrus v1.9.3
910
github.com/unrolled/secure v1.14.0
@@ -19,7 +20,6 @@ require (
1920
github.com/go-playground/universal-translator v0.18.1 // indirect
2021
github.com/go-playground/validator/v10 v10.14.0 // indirect
2122
github.com/goccy/go-json v0.10.2 // indirect
22-
github.com/google/uuid v1.6.0 // indirect
2323
github.com/json-iterator/go v1.1.12 // indirect
2424
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
2525
github.com/leodido/go-urn v1.2.4 // indirect

resouces/JsEnv_Dev.js

+13-24
Original file line numberDiff line numberDiff line change
@@ -69,60 +69,49 @@ Hlclient.prototype.handlerRequest = function (requestJson) {
6969
try {
7070
var result = JSON.parse(requestJson)
7171
} catch (error) {
72-
console.log("catch error", requestJson);
73-
result = transjson(requestJson)
72+
console.log("请求信息解析错误", requestJson);
73+
return
7474
}
75-
//console.log(result)
76-
if (!result['action']) {
77-
this.sendResult('', 'need request param {action}');
75+
if (!result['action'] || !result["message_id"]) {
76+
console.warn('没有方法或者消息id,不处理');
7877
return
7978
}
80-
var action = result["action"]
79+
var action = result["action"], message_id = result["message_id"]
8180
var theHandler = this.handlers[action];
8281
if (!theHandler) {
83-
this.sendResult(action, 'action not found');
82+
this.sendResult(action, message_id, 'action没找到');
8483
return
8584
}
8685
try {
8786
if (!result["param"]) {
8887
theHandler(function (response) {
89-
_this.sendResult(action, response);
88+
_this.sendResult(action, message_id, response);
9089
})
9190
return
9291
}
9392
var param = result["param"]
9493
try {
9594
param = JSON.parse(param)
96-
} catch (e) {}
95+
} catch (e) {
96+
}
9797
theHandler(function (response) {
98-
_this.sendResult(action, response);
98+
_this.sendResult(action, message_id, response);
9999
}, param)
100100

101101
} catch (e) {
102102
console.log("error: " + e);
103-
_this.sendResult(action, e);
103+
_this.sendResult(action, message_id, e);
104104
}
105105
}
106106

107-
Hlclient.prototype.sendResult = function (action, e) {
107+
Hlclient.prototype.sendResult = function (action, message_id, e) {
108108
if (typeof e === 'object' && e !== null) {
109109
try {
110110
e = JSON.stringify(e)
111111
} catch (v) {
112112
console.log(v)//不是json无需操作
113113
}
114114
}
115-
this.send(action + atob("aGxeX14") + e);
115+
this.send(JSON.stringify({"action": action, "message_id": message_id, "response_data": e}));
116116
}
117117

118-
function transjson(formdata) {
119-
var regex = /"action":(?<actionName>.*?),/g
120-
var actionName = regex.exec(formdata).groups.actionName
121-
stringfystring = formdata.match(/{..data..:.*..\w+..:\s...*?..}/g).pop()
122-
stringfystring = stringfystring.replace(/\\"/g, '"')
123-
paramstring = JSON.parse(stringfystring)
124-
tens = `{"action":` + actionName + `,"param":{}}`
125-
tjson = JSON.parse(tens)
126-
tjson.param = paramstring
127-
return tjson
128-
}

0 commit comments

Comments
 (0)