ws

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BytesToString

func BytesToString(b []byte) string

BytesToString 转换 []byte 到 string,不会分配新的内存

func Metrics

func Metrics(w http.ResponseWriter)

Metrics 返回 Hub 的当前的关键指标

func SendMessage

func SendMessage(clientId string, msgType message.Type, msg string) error

func StringToBytes

func StringToBytes(s string) []byte

StringToBytes 转换 string 到 []byte,不会分配新的内存

func WebsocketServe

func WebsocketServe(writer http.ResponseWriter, request *http.Request)

WebsocketServe 处理 WebSocket 连接请求

Types

type Authenticator

type Authenticator interface {
	// Authenticate 验证请求是否合法,第一个返回值为用户 id,第二个返回值为错误
	Authenticate(r *http.Request) (map[string]interface{}, error)
}

type Client

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

Client 是一个中间人,负责 WebSocket 连接和 Hub 之间的通信

type Handler

type Handler func(msg message.WsMessage, err error)

Handler 错误处理函数

type Hub

type Hub struct {

	// 互斥锁,保护 userClients 以及 clients 的读写
	sync.RWMutex
	// contains filtered or unexported fields
}

Hub 维护了所有的客户端连接

var HubBus *Hub

func NewHub

func NewHub() *Hub

func (*Hub) Run

func (h *Hub) Run()

Run 注册、取消注册请求处理

func (*Hub) SetMessageHandler

func (h *Hub) SetMessageHandler(handler MessageHandler)

type JWTAuthenticator

type JWTAuthenticator struct{}

func (*JWTAuthenticator) Authenticate

func (J *JWTAuthenticator) Authenticate(r *http.Request) (map[string]interface{}, error)

type MessageHandler

type MessageHandler interface {
	Handler(clientId, userId, userType string, messageType int, message []byte) error
}

MessageHandler 消息处理器

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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