wsserver

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const ActionSepChar = "." //action 分隔字符
View Source
const GroupKey = "group"
View Source
const SubscribeKey = "subscribe"
View Source
const UnSubscribeKey = "unsubscribe"

Variables

View Source
var ActOk = ActRet{Ok: true}
View Source
var AuthFailFrame = &ClientAnswer{Code: 2, Group: "system", Message: "auth fail"}
View Source
var AuthRepeatFrame = &ClientAnswer{Code: 2, Group: "system", Message: "auth repeat"}
View Source
var CmdErrFrame = &ClientAnswer{Code: 1, Group: "system", Message: "cmd not support"}
View Source
var NotAuthFrame = &ClientAnswer{Code: 2, Group: "system", Message: "not auth"}
View Source
var NotSupportFrame = &ClientAnswer{Code: 1, Group: "system", Message: "not support"}
View Source
var PingCmd = &ClientCmd{Action: "ping"}
View Source
var PongFrame = &ClientAnswer{Group: "pong", Message: "success"}
View Source
var PongMessage = []byte("{\"group\":\"pong\"}")
View Source
var SaveInvalidFrame = &ClientAnswer{Code: 2, Group: "system", Message: "please choose right save"}
View Source
var SaveNotChooseFrame = &ClientAnswer{Code: 2, Group: "system", Message: "please choose save"}
View Source
var SysErrFame = &ClientAnswer{Code: 1, Group: "system", Message: "is busy"}

UnExceptedCloseErr 不处理的关闭错误

Functions

func BuildActionName added in v1.0.5

func BuildActionName(s ...string) string

BuildActionName 构建action名称

func ConvertCmdArgs added in v1.0.5

func ConvertCmdArgs(data interface{}, cmd *ClientCmd) error

ConvertCmdArgs 解析消息cmd的args

func NewFeatureWsConnector

func NewFeatureWsConnector(url string, retry int) *wsConnector

NewFeatureWsConnector ws辅助连接器

func SetLogger

func SetLogger(log v1log.ILog)

func SetMessageSize

func SetMessageSize(size int64)

Types

type ActRet added in v1.0.5

type ActRet struct {
	Ok      bool
	Message string
	Data    interface{}
	Error   error
}

ActRet 操作结果

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) Conn

func (c *Client) Conn() *websocket.Conn

func (*Client) ID

func (c *Client) ID() string

func (*Client) SendMessage

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

type ClientAnswer added in v1.0.5

type ClientAnswer struct {
	Code    int64       `json:"code"`
	Group   string      `json:"group"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

ClientAnswer client返回数据

type ClientCmd added in v1.0.5

type ClientCmd struct {
	Action string      `json:"action"`
	Args   interface{} `json:"args"`
	Client *Client     `json:"-"`

	OrigMessage *WebSocketMessage `json:"-"`
	// contains filtered or unexported fields
}

ClientCmd client操作命令

func (*ClientCmd) FirstAction added in v1.0.5

func (cmd *ClientCmd) FirstAction() string

FirstAction 解析action中的第1个action

func (*ClientCmd) ParseAction added in v1.0.5

func (cmd *ClientCmd) ParseAction(index int) string

ParseAction 解析action中的第n个参数(n从0开始),若不存在则返回空字符串

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

type ConnectorEvent string

ConnectorEvent 连接器事件

const (
	ConnectNotify ConnectorEvent = "connect notify"
)

type DoSomeThing added in v1.0.5

type DoSomeThing func(*Client) ActRet

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() error

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) Start

func (h *Hub) Start() error

func (*Hub) Stop

func (h *Hub) Stop() error

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 IWebsocketWorker added in v1.0.5

type IWebsocketWorker interface {
	Name() string
	Receive(cmd *ClientCmd) //接收消息处理

	Online(client *Client) //client 上线
	Close(client *Client)  //client close

	//NeedAccredit 是否需要授权检查
	NeedAccredit(client *Client) bool
}

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