1 Star 0 Fork 0

linngc / central-mirror

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
websocket_control.go 2.38 KB
一键复制 编辑 原始数据 按行查看 历史
linngc 提交于 2024-03-08 18:15 . add:添加pprof方法
// Package websocket
// @Link https://gitee.com/linngc/central-mirror
// @Copyright Copyright (c) 2024 central-mirror CLI
// @Author linngc
// @License
package websocket
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcron"
"github.com/gogf/gf/v2/util/gconv"
)
const (
MsgTypeAll = "msgTypeAll" //发送全部客户端
MsgTypeClientId = "msgTypeClientId" //发送单个客户端
MsgTypeUnique = "msgTypeUnique" //发送单个客户端
MsgTypeTag = "msgTypeTag" //发送某个标签
)
// SendToCron 定时发送消息
// @param pattern "0 */10 * * * *"
// @writeCronFunc 获取读取的Process 数据
func SendToCron(ctx context.Context, pattern, msgType string,
writeCronFunc func(ctx context.Context, msgType string) (response *WResponse, err error)) {
//定时任务,执行定时发送消息
gcron.Add(ctx, pattern, func(ctx context.Context) {
response, err := writeCronFunc(ctx, msgType)
if err != nil {
g.Log().Error(ctx, "组装定时发送消息数据出错", err)
return
}
toId := response.ToId
switch msgType {
case MsgTypeAll:
response.ToId = MsgTypeAll
response.FromId = MsgTypeAll
SendToAll(ctx, response)
break
case MsgTypeClientId:
if nil == response.ToId {
response.ToId = gconv.String(toId)
}
SendToClientId(ctx, gconv.String(toId), response)
break
case MsgTypeUnique:
if nil == response.ToId {
response.ToId = gconv.Uint64(toId)
}
SendToUnique(ctx, gconv.Uint64(toId), response)
break
case MsgTypeTag:
if nil == response.ToId {
response.ToId = gconv.String(toId)
}
SendToTag(ctx, gconv.String(toId), response)
break
}
})
}
// SendToAll 发送全部客户端
func SendToAll(ctx context.Context, response *WResponse) {
Manager.Broadcast <- response
}
// SendToClientId 发送单个客户端
func SendToClientId(ctx context.Context, id string, response *WResponse) {
clientRes := &ClientWResponse{ID: id, WResponse: response}
Manager.ClientBroadcast <- clientRes
}
// SendToUnique 发送单个用户
func SendToUnique(ctx context.Context, unique uint64, response *WResponse) {
uRes := &UWResponse{Unique: unique, WResponse: response}
Manager.UBroadcast <- uRes
}
// SendToTag 发送某个标签
func SendToTag(ctx context.Context, tag string, response *WResponse) {
tagRes := &TagWResponse{Tag: tag, WResponse: response}
Manager.TagBroadcast <- tagRes
}
Go
1
https://gitee.com/linngc/central-mirror.git
git@gitee.com:linngc/central-mirror.git
linngc
central-mirror
central-mirror
4aaf4b1e930c

搜索帮助