diff --git a/ws/client.go b/ws/client.go index 99226a4..8ae257c 100644 --- a/ws/client.go +++ b/ws/client.go @@ -152,15 +152,8 @@ func (c *Client) SendMsg(msg []byte) { c.Send <- msg } -// SendRawMsg 构造消息再发送 -func (c *Client) SendRawMsg(code int, action, msg string, data any) { - a := &Action{ - Action: action, - Code: code, - Msg: msg, - Data: data, - } - +// SendActionMsg 构造消息再发送 +func (c *Client) SendActionMsg(a *Action) { c.SendMsg(a.Encode()) } diff --git a/ws/context.go b/ws/context.go index 1453dde..57ba010 100644 --- a/ws/context.go +++ b/ws/context.go @@ -5,9 +5,11 @@ import ( ) type Context struct { + Id string + Action string + Params string + Client *Client - Action string - Params string Response *Action Server *Server diff --git a/ws/context_send.go b/ws/context_send.go index f0592df..d765037 100644 --- a/ws/context_send.go +++ b/ws/context_send.go @@ -2,7 +2,7 @@ package ws // Send 发送数据给用户 func (c *Context) Send(data any) { - msg := New(c.Action).WithData(data) + msg := New(c.Action).WithId(c.Id).WithData(data) c.Response = msg c.Client.SendMsg(msg.Encode()) @@ -10,7 +10,7 @@ func (c *Context) Send(data any) { // SendOk 发送成功消息 func (c *Context) SendOk() { - msg := New(c.Action) + msg := New(c.Action).WithId(c.Id) c.Response = msg c.Client.SendMsg(msg.Encode()) @@ -30,7 +30,7 @@ func (c *Context) SendCode(code int, msg string) { } } - m := New(c.Action).WithCode(code).WithMsg(msg) + m := New(c.Action).WithId(c.Id).WithCode(code).WithMsg(msg) c.Response = m c.Client.SendMsg(m.Encode()) @@ -38,7 +38,7 @@ func (c *Context) SendCode(code int, msg string) { // SendMsg 发送消息给当前用户 func (c *Context) SendMsg(msg string) { - m := New(c.Action).WithMsg(msg) + m := New(c.Action).WithId(c.Id).WithMsg(msg) c.Response = m c.Client.SendMsg(m.Encode()) @@ -46,7 +46,7 @@ func (c *Context) SendMsg(msg string) { // SendActionData 发送数据给当前用户 func (c *Context) SendActionData(action string, data any) { - m := New(action).WithData(data) + m := New(action).WithId(c.Id).WithData(data) c.Response = m c.Client.SendMsg(m.Encode()) @@ -54,7 +54,7 @@ func (c *Context) SendActionData(action string, data any) { // SendActionMsg 发送消息给当前用户 func (c *Context) SendActionMsg(action, msg string) { - m := New(action).WithMsg(msg) + m := New(action).WithId(c.Id).WithMsg(msg) c.Response = m c.Client.SendMsg(m.Encode()) @@ -62,7 +62,7 @@ func (c *Context) SendActionMsg(action, msg string) { // SendTo 发送给指定用户 func (c *Context) SendTo(uid, action string, data any) { - m := New(action).WithData(data) + m := New(action).WithId(c.Id).WithData(data) c.Response = m user := c.Client.Hub.User(uid) diff --git a/ws/dispatcher.go b/ws/dispatcher.go index 87e534c..e99b49a 100644 --- a/ws/dispatcher.go +++ b/ws/dispatcher.go @@ -1,18 +1,27 @@ package ws import ( - "time" - "github.com/tidwall/gjson" + "time" ) func Dispatcher(c *Client, request string) { - t := time.Now() + var req struct { + Id string `json:"id"` + Action string `json:"action"` + Params string `json:"params"` + } + + result := gjson.Parse(request) + req.Id = result.Get("id").String() + req.Params = result.Get("params").String() + req.Action = result.Get("action").String() + //ping直接回应 - action := gjson.Get(request, "action").String() - if action == "ping" { + t := time.Now() + if req.Action == "ping" { c.LastHeartbeatTime = t - c.SendRawMsg(0, "ping", "pong", nil) + c.SendActionMsg(&Action{Action: "ping", Msg: "pong"}) return } @@ -20,14 +29,14 @@ func Dispatcher(c *Client, request string) { if c.User != nil { isBanned, bandTime := c.User.IsBanned() if isBanned { - c.SendRawMsg(-11, "sys.ban", "You have been ban", bandTime) + c.SendActionMsg(&Action{Action: "sys.ban", Code: -1001, Data: bandTime}) return } } //请求频率限制5毫秒 if t.Sub(c.LastRequestTime).Microseconds() <= 2 { - c.SendRawMsg(-13, "sys.requestLimit", "Your requests are too frequent", nil) + c.SendActionMsg(&Action{Action: "sys.requestLimit", Code: -1003, Msg: "requests are too frequent"}) return } else { //更新最后请求时间 @@ -40,16 +49,18 @@ func Dispatcher(c *Client, request string) { } } - handlers := InitManager().Handlers(action) + handlers := InitManager().Handlers(req.Action) if handlers == nil || len(handlers) == 0 { - c.SendRawMsg(-15, action, "Request not supported", nil) + c.SendActionMsg(&Action{Action: req.Action, Code: -1005, Msg: "request not supported"}) return } ctx := &Context{ + Id: req.Id, + Params: req.Params, + Action: req.Action, + Client: c, - Action: action, - Params: gjson.Get(request, "params").String(), Server: wss, handlers: handlers, diff --git a/ws/w.go b/ws/w.go index 15433e3..afacce8 100755 --- a/ws/w.go +++ b/ws/w.go @@ -10,9 +10,10 @@ import ( // Action Websocket通讯协议 type Action struct { + Code int `json:"code"` Action string `json:"action"` - Code int `json:"code"` + Id string `json:"id,omitempty"` Msg string `json:"msg,omitempty"` Data any `json:"data,omitempty"` } diff --git a/ws/w_util.go b/ws/w_util.go deleted file mode 100644 index 4acbf26..0000000 --- a/ws/w_util.go +++ /dev/null @@ -1,29 +0,0 @@ -package ws - -func Msg(action, msg string) []byte { - res := &Action{ - Action: action, - Msg: msg, - } - - return res.json() -} - -func Code(action string, code int, msg string) []byte { - res := &Action{ - Action: action, - Code: code, - Msg: msg, - } - - return res.json() -} - -func Data(action string, data any) []byte { - res := &Action{ - Action: action, - Data: data, - } - - return res.json() -} diff --git a/ws/w_util_chain.go b/ws/w_util_chain.go index 6bcb778..c276377 100644 --- a/ws/w_util_chain.go +++ b/ws/w_util_chain.go @@ -6,6 +6,11 @@ func New(action string) *Action { } } +func (m *Action) WithId(id string) *Action { + m.Id = id + return m +} + func (m *Action) WithCode(code int) *Action { m.Code = code return m