mirror of
https://github.com/wonli/aqi.git
synced 2025-12-24 10:40:58 +08:00
action id
This commit is contained in:
11
ws/client.go
11
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())
|
||||
}
|
||||
|
||||
|
||||
@@ -5,9 +5,11 @@ import (
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
Id string
|
||||
Action string
|
||||
Params string
|
||||
|
||||
Client *Client
|
||||
Action string
|
||||
Params string
|
||||
Response *Action
|
||||
Server *Server
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
|
||||
3
ws/w.go
3
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"`
|
||||
}
|
||||
|
||||
29
ws/w_util.go
29
ws/w_util.go
@@ -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()
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user