websocket

package module
v0.0.0-...-fc70b83 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 27, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

README

一.插件描述

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

View Source
const (
	MsgTypeAll      = "msgTypeAll"      //发送全部客户端
	MsgTypeClientId = "msgTypeClientId" //发送单个客户端
	MsgTypeUnique   = "msgTypeUnique"   //发送单个客户端
	MsgTypeTag      = "msgTypeTag"      //发送某个标签
)
View Source
const (
	Error     = "error"     //出错
	Login     = "login"     //登陆
	Join      = "join"      //加入
	Quit      = "quit"      //退出
	IsApp     = "is_app"    //是否是app
	Ping      = "ping"      //心跳检测
	Msg       = "msg"       //消息发送
	Connected = "connected" //连接成功
)

Variables

View Source
var (
	// Manager 客户端管理
	Manager = NewClientManager()
)

Functions

func GetUniqueKey

func GetUniqueKey(unique uint64) (key string)

GetUniqueKey 获取用户key

func ProcessData

func ProcessData(ctx context.Context, client *WssClient, message []byte, readFunc ...func(ctx context.Context, request *WRequest) error)

ProcessData 处理数据

func SendToAll

func SendToAll(ctx context.Context, response *WResponse)

SendToAll 发送全部客户端

func SendToClientId

func SendToClientId(ctx context.Context, id string, response *WResponse)

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 SendToTag

func SendToTag(ctx context.Context, tag string, response *WResponse)

SendToTag 发送某个标签

func SendToUnique

func SendToUnique(ctx context.Context, unique uint64, response *WResponse)

SendToUnique 发送单个用户

func StartWebSocket

func StartWebSocket(ctx context.Context)

StartWebSocket 初始化gcmd.Command时调用,并在g.Server()对象之前调用执行

func Websocket

func Websocket(ctx context.Context, r *ghttp.Request, readFunc ...func(ctx context.Context, request *WRequest) error) error

Websocket 初始化websocket方法 调用说明 ws://127.0.0.1:10010/v1.api/wss/sysMonitor?unique=111 使用提醒:使用通道操作,对数据进行读取和发送

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 ClientWResponse struct {
	ID        string     //客户端id
	WResponse *WResponse //输出对象
}

type ReadFunc

type ReadFunc func(ctx context.Context, request *WRequest) error

ReadFunc 获取读取的Process 数据

type TagWResponse

type TagWResponse struct {
	Tag       string     //标签名称
	WResponse *WResponse //输出对象
}

type UWResponse

type UWResponse struct {
	Unique    uint64     //用户id
	WResponse *WResponse //输出对象
}

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 NewClient

func NewClient(addr string, socket *websocket.Conn, firstTime uint64) (client *WssClient)

NewClient 初始化

func NewUniqueClient

func NewUniqueClient(addr string, socket *websocket.Conn, unique uint64, firstTime uint64) (client *WssClient)

NewUniqueClient 初始化

func (*WssClient) Heartbeat

func (c *WssClient) Heartbeat(currentTime uint64)

Heartbeat 心跳更新

func (*WssClient) IsHeartbeatTimeout

func (c *WssClient) IsHeartbeatTimeout(currentTime uint64) (timeout bool)

IsHeartbeatTimeout 心跳是否超时

func (*WssClient) SendMsg

func (c *WssClient) SendMsg(ctx context.Context, msg *WResponse)

SendMsg 发送数据

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL