ws_server

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultAppId = 101 // 默认平台Id
)

Variables

This section is empty.

Functions

func AllSendMessages

func AllSendMessages(appId uint32, userId string, data string)

全员广播

func ClearTimeoutConnections

func ClearTimeoutConnections()

定时清理超时连接

func GetManagerInfo

func GetManagerInfo(isDebug string) (managerInfo map[string]interface{})

************************* manager info ************************************** 获取管理者信息

func GetUserKey

func GetUserKey(appId uint32, userId string) (key string)

获取用户key

func GetUserList

func GetUserList(appId uint32) (userList []string)

获取全部用户

func InitClientManager

func InitClientManager(cb func(client *Client, message []byte))

func Register

func Register(key string, value ICmdWsHandler)

func WsBeegoPage

func WsBeegoPage(w http.ResponseWriter, req *http.Request)

WsBeegoPage is beego websocket handler

func WsGinPage

func WsGinPage(c *gin.Context)

WsPage is gin websocket handler

Types

type Client

type Client struct {
	Addr          string          // 客户端地址
	Socket        *websocket.Conn // 用户连接
	Send          chan []byte     // 待发送的数据
	AppId         uint32          // 登录的平台Id app/web/ios
	UserId        string          // 用户Id,用户登录以后才有
	FirstTime     int64           // 首次连接事件
	HeartbeatTime int64           // 用户上次心跳时间
	LoginTime     int64           // 登录时间 登录以后才有
}

用户连接

func GetUserClient

func GetUserClient(userId string) (client *Client)

获取用户所在的连接

func NewClient

func NewClient(appid uint32, addr string, socket *websocket.Conn, firstTime int64) (client *Client)

初始化

func (*Client) GetKey

func (c *Client) GetKey() (key string)

读取客户端数据

func (*Client) Heartbeat

func (c *Client) Heartbeat(currentTime int64)

用户心跳

func (*Client) IsHeartbeatTimeout

func (c *Client) IsHeartbeatTimeout(currentTime int64) (timeout bool)

心跳超时

func (*Client) IsLogin

func (c *Client) IsLogin() (isLogin bool)

是否登录了

func (*Client) Login

func (c *Client) Login(appId uint32, userId string, loginTime int64)

用户登录

func (*Client) OnPacket

func (c *Client) OnPacket(addr string, data []byte)

func (*Client) ProcessData

func (c *Client) ProcessData(message []byte)

proc data

func (*Client) SendMsg

func (c *Client) SendMsg(msg []byte)

func (*Client) SendPbMsg

func (c *Client) SendPbMsg(data1 []byte, data2 []byte)

发送pb数据

type ClientManager

type ClientManager struct {
	Clients     map[*Client]bool   // 全部的连接
	ClientsLock sync.RWMutex       // 读写锁
	Users       map[string]*Client // 登录的用户 // appId+uuid
	UserLock    sync.RWMutex       // 读写锁
	Register    chan *Client       // 连接连接处理
	Login       chan *login        // 用户登录处理
	Unregister  chan *Client       // 断开连接处理程序
	Broadcast   chan []byte        // 广播 向全部成员发送数据
	HandlerFunc func(client *Client, message []byte)
}

连接管理

func NewClientManager

func NewClientManager() (clientManager *ClientManager)

func NewWsClientMgr

func NewWsClientMgr() *ClientManager

func (*ClientManager) AddClients

func (manager *ClientManager) AddClients(client *Client)

添加客户端

func (*ClientManager) AddUsers

func (manager *ClientManager) AddUsers(key string, client *Client)

添加用户

func (*ClientManager) BroadcastByZone

func (manager *ClientManager) BroadcastByZone(zone int, header []byte, body []byte)

func (*ClientManager) ClientsRange

func (manager *ClientManager) ClientsRange(f func(client *Client, value bool) (result bool))

遍历

func (*ClientManager) DelClients

func (manager *ClientManager) DelClients(client *Client)

删除客户端

func (*ClientManager) DelUsers

func (manager *ClientManager) DelUsers(client *Client) (result bool)

删除用户

func (*ClientManager) EventLogin

func (manager *ClientManager) EventLogin(login *login)

用户登录

func (*ClientManager) EventRegister

func (manager *ClientManager) EventRegister(client *Client)

用户建立连接事件

func (*ClientManager) EventUnregister

func (manager *ClientManager) EventUnregister(client *Client)

用户断开连接

func (*ClientManager) GetClients

func (manager *ClientManager) GetClients() (clients map[*Client]bool)

GetClients

func (*ClientManager) GetClientsLen

func (manager *ClientManager) GetClientsLen() (clientsLen int)

GetClientsLen

func (*ClientManager) GetUserClient

func (manager *ClientManager) GetUserClient(appId uint32, userId string) (client *Client)

获取用户的连接

func (*ClientManager) GetUserClients

func (manager *ClientManager) GetUserClients() (clients []*Client)

获取用户的key

func (*ClientManager) GetUserKeys

func (manager *ClientManager) GetUserKeys() (userKeys []string)

获取用户的key

func (*ClientManager) GetUserList

func (manager *ClientManager) GetUserList(appId uint32) (userList []string)

获取用户的key

func (*ClientManager) GetUsersLen

func (manager *ClientManager) GetUsersLen() (userLen int)

GetClientsLen

func (*ClientManager) InClient

func (manager *ClientManager) InClient(client *Client) (ok bool)

func (*ClientManager) KickById

func (manager *ClientManager) KickById(uid uint64, reason g1_protocol.EKickOutReason)

kick

func (*ClientManager) SendByUid

func (manager *ClientManager) SendByUid(uid uint64, data1 []byte, data2 []byte) error

type ICmdWsHandler

type ICmdWsHandler interface {
	ProcessCmd(client *Client, data []byte) int
}

func GetHandlers

func GetHandlers(key string) (value ICmdWsHandler, ok bool)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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