ws

package
v1.1.45 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2024 License: GPL-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const GroupKey = "group"
View Source
const SubscribeKey = "subscribe"
View Source
const UnSubscribeKey = "unsubscribe"

Variables

UnExceptedCloseErr 不处理的关闭错误

Functions

func NewFeatureWsConnector

func NewFeatureWsConnector(url string, retry int) *wsConnector

NewFeatureWsConnector ws辅助连接器

func SetLogger

func SetLogger(log v1log.ILog)

Types

type ActionEvent

type ActionEvent struct {
	// contains filtered or unexported fields
}

func NewActionEvent

func NewActionEvent(name string, f func(client *Client, data []byte)) *ActionEvent

func (*ActionEvent) Do

func (act *ActionEvent) Do(client *Client, data []byte)

func (*ActionEvent) Name

func (act *ActionEvent) Name() string

type BroadcastFeature

type BroadcastFeature struct{}

BroadcastFeature 广播辅助功能

func (BroadcastFeature) Send

func (b BroadcastFeature) Send(clients []*Client, msg interface{})

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a middleman between the websocket connection and the hub.

func NewServerClient

func NewServerClient(conn *websocket.Conn, hub *Hub) *Client

func (*Client) Close

func (c *Client) Close()

Close 主动断开

func (*Client) ID

func (c *Client) ID() string

func (*Client) SendMessage

func (c *Client) SendMessage(message interface{})

type ClientsMgr

type ClientsMgr struct {
	// contains filtered or unexported fields
}

ClientsMgr 联机客户端管理

func NewClientsMgr

func NewClientsMgr() *ClientsMgr

func (*ClientsMgr) AddClient

func (w *ClientsMgr) AddClient(c *Client)

func (*ClientsMgr) Clients

func (w *ClientsMgr) Clients() []*Client

func (*ClientsMgr) Conns

func (w *ClientsMgr) Conns() int64

func (*ClientsMgr) DeleteClient

func (w *ClientsMgr) DeleteClient(c *Client)

type ConnectorEvent added in v1.1.44

type ConnectorEvent string

ConnectorEvent 连接器事件

const (
	ConnectNotify ConnectorEvent = "connect notify"
)

type FeatureActionsMgr

type FeatureActionsMgr struct {
	// contains filtered or unexported fields
}

FeatureActionsMgr ws操作事件管理器

func NewFeatureActionsMgr

func NewFeatureActionsMgr(actions ...IActionEvent) *FeatureActionsMgr

func (*FeatureActionsMgr) GetAction

func (mgr *FeatureActionsMgr) GetAction(name string) (IActionEvent, error)

GetAction 获取对应action

func (*FeatureActionsMgr) Parse

func (mgr *FeatureActionsMgr) Parse(data []byte) (IActionEvent, error)

Parse 解析数据,数据格式满足一下

func (*FeatureActionsMgr) ParseDo

func (mgr *FeatureActionsMgr) ParseDo(client *Client, data []byte) (IActionEvent, error)

ParseDo 解析数据并且执行

type FeatureSubscribeGroup

type FeatureSubscribeGroup struct {
	IClientsMgr
	// contains filtered or unexported fields
}

FeatureSubscribeGroup ws订阅Group功能

func NewFeatureSubscribeGroup

func NewFeatureSubscribeGroup(name ...string) *FeatureSubscribeGroup

func (*FeatureSubscribeGroup) Name

func (g *FeatureSubscribeGroup) Name() string

func (*FeatureSubscribeGroup) SetName

func (g *FeatureSubscribeGroup) SetName(n string)

type FeatureSubscribeMgr

type FeatureSubscribeMgr struct {
	// contains filtered or unexported fields
}

FeatureSubscribeMgr ws订阅功能,负责解析和管理订阅Group

func NewFeatureSubscribeMgr

func NewFeatureSubscribeMgr(groups ...ISubscribeGroup) *FeatureSubscribeMgr

func (*FeatureSubscribeMgr) Group

func (sub *FeatureSubscribeMgr) Group(name string) ISubscribeGroup

Group 指定group

func (*FeatureSubscribeMgr) Groups

func (sub *FeatureSubscribeMgr) Groups() map[string]ISubscribeGroup

Groups 所有groups

func (*FeatureSubscribeMgr) JoinGroup

func (sub *FeatureSubscribeMgr) JoinGroup(client *Client, name string) bool

JoinGroup 加入指定group

func (*FeatureSubscribeMgr) Parse

func (sub *FeatureSubscribeMgr) Parse(data []byte) (ISubscribeGroup, bool)

Parse 解析订阅数据,返回参数:

 group,对应group
 bool,true:加入订阅,false:退出订阅
如果对应数据格式是group的则返回的bool不准确,由上层自己判断
	支持的数据格式1: {"action":"subscribe","group":"Trade"}
       上面这个数据格式适用于配合 FeatureActionsMgr 使用
	支持的数据格式2: {"subscribe":"Trade"}
	支持的数据格式3: {"unsubscribe":"Trade"}

func (*FeatureSubscribeMgr) ParseJoinGroup

func (sub *FeatureSubscribeMgr) ParseJoinGroup(client *Client, data []byte) (ISubscribeGroup, bool)

ParseJoinGroup 解析并自动加入group,返回:

group, 对应group名称, group为nil表示group不存在
join, true:订阅加入,false:订阅退出

func (*FeatureSubscribeMgr) QuitAllGroup

func (sub *FeatureSubscribeMgr) QuitAllGroup(client *Client)

QuitAllGroup 退出所有group

func (*FeatureSubscribeMgr) QuitGroup

func (sub *FeatureSubscribeMgr) QuitGroup(client *Client, name string) bool

QuitGroup 退出指定group

func (*FeatureSubscribeMgr) Subscribe

func (sub *FeatureSubscribeMgr) Subscribe(client *Client, data []byte) ISubscribeGroup

Subscribe 订阅方法,用于只处理订阅数据

func (*FeatureSubscribeMgr) SubscribeAction

func (sub *FeatureSubscribeMgr) SubscribeAction(client *Client, data []byte)

func (*FeatureSubscribeMgr) UnSubscribe

func (sub *FeatureSubscribeMgr) UnSubscribe(client *Client, data []byte) ISubscribeGroup

UnSubscribe 取消订阅方法,用于只处理取消订阅数据

func (*FeatureSubscribeMgr) UnSubscribeAction

func (sub *FeatureSubscribeMgr) UnSubscribeAction(client *Client, data []byte)

type Hub

type Hub struct {
	IClientsMgr
	// contains filtered or unexported fields
}

func NewHub

func NewHub() *Hub

func (*Hub) PongHandle

func (h *Hub) PongHandle(messageType int, message []byte) (bool, []byte)

func (*Hub) Run

func (h *Hub) Run()

func (*Hub) ServeHTTP

func (h *Hub) ServeHTTP(w http.ResponseWriter, r *http.Request)

用于ws联机的http方法

func (*Hub) SetWorker

func (h *Hub) SetWorker(worker IWorkerMgr)

func (*Hub) Stop

func (h *Hub) Stop()

type IActionEvent

type IActionEvent interface {
	Name() string
	Do(client *Client, data []byte)
}

IActionEvent 定义单个action操作时间

type IClientsMgr

type IClientsMgr interface {
	AddClient(c *Client)
	DeleteClient(c *Client)

	Clients() []*Client //return all clients
	Conns() int64       //return clients num
}

type IOnline

type IOnline interface {
	AddClient(c *Client)
	DeleteClient(c *Client)

	Clients() []*Client //return all clients
	Conns() int64       //return clients num
}

IOnline 联机在线管理(已废弃,请使用 IClientsMgr )

type ISubscribeGroup

type ISubscribeGroup interface {
	Name() string
	IClientsMgr
}

ISubscribeGroup 订阅频道组功能

type IWorkerMgr

type IWorkerMgr interface {
	SetHub(h *Hub)         //设置hub
	Online(client *Client) //client 上线
	Close(client *Client)  //client close

	Pong(messageType int, message []byte) (bool, []byte) //判断消息是否ping消息并返回pong应答消息体
	Receive(cmd *WebSocketMessage)                       //接收消息处理
}

type OnlineWorker

type OnlineWorker struct {
	// contains filtered or unexported fields
}

OnlineWorker 联机管理worker

func NewOnlineWorker

func NewOnlineWorker() *OnlineWorker

func (*OnlineWorker) AddClient

func (w *OnlineWorker) AddClient(c *Client)

func (*OnlineWorker) Clients

func (w *OnlineWorker) Clients() []*Client

func (*OnlineWorker) Conns

func (w *OnlineWorker) Conns() int64

func (*OnlineWorker) DeleteClient

func (w *OnlineWorker) DeleteClient(c *Client)

type WebSocketMessage

type WebSocketMessage struct {
	MessageType int    `json:"-"`
	Message     []byte `json:"-"`

	Client *Client `json:"-"`
}

WebSocketMessage ws接收消息

type WsConnector

type WsConnector interface {
	SendJson(msg chan interface{})
	Send(msg []byte)
	Start(reader chan []byte) error
	Conn() *websocket.Conn
}

Jump to

Keyboard shortcuts

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