Documentation ¶
Overview ¶
Package websocket @Link https://gitee.com/linngc/central-mirror @Copyright Copyright (c) 2022 central CLI @Author linngc @License
Package websocket @Link https://gitee.com/linngc/central-mirror @Copyright Copyright (c) 2022 central CLI @Author linngc @License
Package websocket @Link https://gitee.com/linngc/central-mirror @Copyright Copyright (c) 2022 central CLI @Author linngc @License
Package websocket @Link https://gitee.com/linngc/central-mirror @Copyright Copyright (c) 2022 central CLI @Author linngc @License
Package websocket @Link https://gitee.com/linngc/central-mirror @Copyright Copyright (c) 2022 central CLI @Author linngc @License
Package websocket @Link https://gitee.com/linngc/central-mirror @Copyright Copyright (c) 2022 central CLI @Author linngc @License
Package websocket @Link https://gitee.com/linngc/central-mirror @Copyright Copyright (c) 2022 central CLI @Author linngc @License
Index ¶
- Constants
- Variables
- func GetUniqueKey(unique uint64) (key string)
- func ProcessData(ctx context.Context, client *WssClient, message []byte, ...)
- func SendToAll(ctx context.Context, response *WResponse)
- func SendToClientId(ctx context.Context, id string, response *WResponse)
- func SendToCron(ctx context.Context, pattern, msgType string, ...)
- func SendToTag(ctx context.Context, tag string, response *WResponse)
- func SendToUnique(ctx context.Context, unique uint64, response *WResponse)
- func StartWebSocket(ctx context.Context)
- func Websocket(ctx context.Context, r *ghttp.Request, ...) error
- type ClientManager
- func (manager *ClientManager) AddClients(client *WssClient)
- func (manager *ClientManager) AddUniques(key string, client *WssClient)
- func (manager *ClientManager) ClientsRange(f func(client *WssClient, value bool) (result bool))
- func (manager *ClientManager) DelClients(client *WssClient)
- func (manager *ClientManager) DelUniques(client *WssClient) (result bool)
- func (manager *ClientManager) EventLogin(ctx context.Context, login *login)
- func (manager *ClientManager) EventRegister(ctx context.Context, client *WssClient)
- func (manager *ClientManager) EventUnregister(ctx context.Context, client *WssClient)
- func (manager *ClientManager) GetClients() (clients map[*WssClient]bool)
- func (manager *ClientManager) GetClientsLen() (clientsLen int)
- func (manager *ClientManager) GetUniqueClient(unique uint64) (client *WssClient)
- func (manager *ClientManager) GetUniquesLen() (uniqueLen int)
- func (manager *ClientManager) InClient(client *WssClient) (ok bool)
- type ClientWResponse
- type ReadFunc
- type TagWResponse
- type UWResponse
- type WRequest
- type WResponse
- type WssClient
Constants ¶
const ( MsgTypeAll = "msgTypeAll" //发送全部客户端 MsgTypeClientId = "msgTypeClientId" //发送单个客户端 MsgTypeUnique = "msgTypeUnique" //发送单个客户端 MsgTypeTag = "msgTypeTag" //发送某个标签 )
const ( Error = "error" //出错 Login = "login" //登陆 Join = "join" //加入 Quit = "quit" //退出 IsApp = "is_app" //是否是app Ping = "ping" //心跳检测 Msg = "msg" //消息发送 Connected = "connected" //连接成功 )
Variables ¶
var ( // Manager 客户端管理 Manager = NewClientManager() )
Functions ¶
func ProcessData ¶
func ProcessData(ctx context.Context, client *WssClient, message []byte, readFunc ...func(ctx context.Context, request *WRequest) error)
ProcessData 处理数据
func SendToClientId ¶
SendToClientId 发送单个客户端
func SendToCron ¶
func SendToCron(ctx context.Context, pattern, msgType string, writeCronFunc func(ctx context.Context, msgType string) (response *WResponse, err error))
SendToCron 定时发送消息 @param pattern "0 */10 * * * *" @writeCronFunc 获取读取的Process 数据
func SendToUnique ¶
SendToUnique 发送单个用户
func StartWebSocket ¶
StartWebSocket 初始化gcmd.Command时调用,并在g.Server()对象之前调用执行
Types ¶
type ClientManager ¶
type ClientManager struct { Clients map[*WssClient]bool // 全部的连接 ClientsLock sync.RWMutex // 读写锁 UniqueClient map[string]*WssClient // 登录的用户 // unique UniqueLock sync.RWMutex // 读写锁 Register chan *WssClient // 连接连接处理 Login chan *login // 用户登录处理 Unregister chan *WssClient // 断开连接处理程序 Broadcast chan *WResponse // 广播 向全部成员发送数据 ClientBroadcast chan *ClientWResponse // 广播 向某个客户端发送数据 TagBroadcast chan *TagWResponse // 广播 向某个标签成员发送数据 UBroadcast chan *UWResponse // 广播 向某个用户的所有链接发送数据 }
ClientManager 客户端管理
func NewClientManager ¶
func NewClientManager() (clientManager *ClientManager)
func (*ClientManager) AddClients ¶
func (manager *ClientManager) AddClients(client *WssClient)
AddClients 添加客户端
func (*ClientManager) AddUniques ¶
func (manager *ClientManager) AddUniques(key string, client *WssClient)
AddUniques 添加用户
func (*ClientManager) ClientsRange ¶
func (manager *ClientManager) ClientsRange(f func(client *WssClient, value bool) (result bool))
ClientsRange 遍历
func (*ClientManager) DelClients ¶
func (manager *ClientManager) DelClients(client *WssClient)
DelClients 删除客户端
func (*ClientManager) DelUniques ¶
func (manager *ClientManager) DelUniques(client *WssClient) (result bool)
DelUniques 删除用户
func (*ClientManager) EventLogin ¶
func (manager *ClientManager) EventLogin(ctx context.Context, login *login)
EventLogin 用户登录事件
func (*ClientManager) EventRegister ¶
func (manager *ClientManager) EventRegister(ctx context.Context, client *WssClient)
EventRegister 用户建立连接事件
func (*ClientManager) EventUnregister ¶
func (manager *ClientManager) EventUnregister(ctx context.Context, client *WssClient)
EventUnregister 用户断开连接事件
func (*ClientManager) GetClients ¶
func (manager *ClientManager) GetClients() (clients map[*WssClient]bool)
GetClients 获取所有客户端
func (*ClientManager) GetClientsLen ¶
func (manager *ClientManager) GetClientsLen() (clientsLen int)
GetClientsLen 获取客户端总数
func (*ClientManager) GetUniqueClient ¶
func (manager *ClientManager) GetUniqueClient(unique uint64) (client *WssClient)
GetUniqueClient 获取用户的连接
func (*ClientManager) GetUniquesLen ¶
func (manager *ClientManager) GetUniquesLen() (uniqueLen int)
GetUniquesLen 已登录用户数
func (*ClientManager) InClient ¶
func (manager *ClientManager) InClient(client *WssClient) (ok bool)
InClient 客户端是否存在
type ClientWResponse ¶
type TagWResponse ¶
type UWResponse ¶
type WRequest ¶
type WRequest struct { Event string `json:"event"` //事件名称 FromId interface{} `json:"fromId"` //发送人id ToId interface{} `json:"toId"` //接收人id Data g.Map `json:"data"` //数据 }
WRequest 当前输入对象
type WResponse ¶
type WResponse struct { Event string `json:"event"` //事件名称 FromId interface{} `json:"fromId"` //发送人id ToId interface{} `json:"toId"` //接收人id Data interface{} `json:"data"` //数据 }
WResponse 输出对象
type WssClient ¶
type WssClient struct { Addr string // 客户端地址 ID string // 连接唯一标识 Socket *websocket.Conn // 用户连接 Send chan *WResponse // 待发送的数据 SendClose bool // 发送是否关闭 Unique uint64 // 用户ID,用户登录以后才有 FirstTime uint64 // 首次连接事件 HeartbeatTime uint64 // 用户上次心跳时间 LoginTime uint64 // 登录时间 登录以后才有 // contains filtered or unexported fields }
WssClient 客户端连接
func NewUniqueClient ¶
func NewUniqueClient(addr string, socket *websocket.Conn, unique uint64, firstTime uint64) (client *WssClient)
NewUniqueClient 初始化
func (*WssClient) IsHeartbeatTimeout ¶
IsHeartbeatTimeout 心跳是否超时