wsCore

package
v0.3.15 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	WebsocketHandshakeSuccess      = `{"code":200,"msg":"ws连接成功"}`
	WebsocketHandshakeError        = `{"code":0,"msg":"发送消息格式不正确"}`
	WebsocketServerPingMsg         = "Server->Ping->Client"
	WebsocketHeartbeatFailMaxTimes = 4
	WebsocketWriteReadBufferSize   = 20480
	WebsocketMaxMessageSize        = 65535
	WebsocketPingPeriod            = 20
	WebsocketReadDeadline          = 100
	WebsocketWriteDeadline         = 35
)

Variables

This section is empty.

Functions

func ReturnBeanError

func ReturnBeanError(err error) []byte

func ReturnBeanFail

func ReturnBeanFail(msg string) []byte

func ReturnBeanMsg

func ReturnBeanMsg(msg string) []byte

func ReturnBeanSuccess

func ReturnBeanSuccess(data interface{}) []byte

Types

type Client

type Client struct {
	Hub                *Hub            // 负责处理客户端注册、注销、在线管理
	Conn               *websocket.Conn // 一个ws连接
	Send               chan []byte     // 一个ws连接存储自己的消息管道
	PingPeriod         time.Duration
	ReadDeadline       time.Duration
	WriteDeadline      time.Duration
	HeartbeatFailTimes int
	ClientLastPongTime time.Time // 客户端最近一次响应服务端 ping 消息的时间
	State              uint8     // ws状态,1=ok;0=出错、掉线等
	sync.RWMutex
	ClientMoreParams // 这里追加一个结构体,方便开发者在成功上线后,可以自定义追加更多字段信息
}

func (*Client) Close

func (c *Client) Close()

func (*Client) Heartbeat

func (c *Client) Heartbeat()

Heartbeat 按照websocket标准协议实现隐式心跳,Server端向Client远端发送ping格式数据包,浏览器收到ping标准格式,自动将消息原路返回给服务器

func (*Client) OnOpen

func (c *Client) OnOpen(hub *Hub, w http.ResponseWriter, r *http.Request) (*Client, bool)

OnOpen 处理握手+协议升级

func (*Client) ReadPump

func (c *Client) ReadPump(callbackOnMessage func(messageType int, receivedData []byte), callbackOnError func(err error), callbackOnClose func())

ReadPump 主要功能主要是实时接收消息

func (*Client) SendMessage

func (c *Client) SendMessage(messageType int, message []byte) error

SendMessage 发送消息,请统一调用本函数进行发送 消息发送时增加互斥锁,加强并发情况下程序稳定性 提醒:开发者发送消息时,不要调用 c.Conn.WriteMessage(messageType, []byte(message)) 直接发送消息

type ClientMoreParams

type ClientMoreParams struct {
	ClientId string `json:"clientId"` //全局唯一的client_id
	Uid      int64  `json:"uid"`      //用户id
	Role     string `json:"role"`     //角色 - 》 admin 可以全局广播,普通用户只能广播自己
}

type Hub

type Hub struct {
	//上线注册
	Register chan *Client
	//下线注销
	UnRegister chan *Client
	//所有在线客户端的内存地址
	Clients map[*Client]bool
	// Inbound messages from the clients.
	Broadcast chan []byte
}

func CreateHubFactory

func CreateHubFactory() *Hub

func (*Hub) Run

func (h *Hub) Run()

type MsgBean

type MsgBean struct {
	Type string `json:"type,default=sendAll"`
	To   string `json:"to,default=0"`
	Data string `json:"data,optional,omitempty"`
}

MsgBean 发送

func GetMsgBean

func GetMsgBean(data []byte) (*MsgBean, error)

type ReturnBean

type ReturnBean struct {
	Code uint32      `json:"code"`
	Msg  string      `json:"msg,optional,omitempty"`
	Data interface{} `json:"data,optional,omitempty"`
}

ReturnBean 返回

func NewReturnBean

func NewReturnBean(data interface{}, code uint32, msg string) *ReturnBean

NewReturnBean 创建ReturnBean

Jump to

Keyboard shortcuts

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