websocket

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2025 License: MIT Imports: 21 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

SendMessage 服务端向客户端发送消息

func Serve

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

Serve 处理 WebSocket 连接请求

func StringToBytes

func StringToBytes(s string) []byte

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

Types

type Authenticator

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

type BinaryMessageHandler

type BinaryMessageHandler interface {
	Handler(ctx context.Context, clientId, userId string, data []byte) error
	// contains filtered or unexported methods
}

BinaryMessageHandler 二进制消息处理器

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

func (h *Hub) IsClientConnected(clientId string) bool

IsClientConnected 添加客户端连接状态检查方法

func (*Hub) Run

func (h *Hub) Run()

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

func (*Hub) SetBinaryMessageHandler

func (h *Hub) SetBinaryMessageHandler(handler BinaryMessageHandler)

func (*Hub) SetTextMessageHandler

func (h *Hub) SetTextMessageHandler(handler TextMessageHandler)

type JWTAuthenticator

type JWTAuthenticator struct{}

func (*JWTAuthenticator) Authenticate

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

type TextMessageHandler

type TextMessageHandler interface {
	Handler(ctx context.Context, clientId, userId string, data []byte) error
	// contains filtered or unexported methods
}

TextMessageHandler 文本消息处理器

type UnimplementedBinaryMessageHandler

type UnimplementedBinaryMessageHandler struct{}

func (*UnimplementedBinaryMessageHandler) Handler

func (h *UnimplementedBinaryMessageHandler) Handler(ctx context.Context, clientId, userId string, data []byte) error

type UnimplementedTextMessageHandler

type UnimplementedTextMessageHandler struct{}

func (*UnimplementedTextMessageHandler) Handler

func (h *UnimplementedTextMessageHandler) Handler(ctx context.Context, clientId, userId string, data []byte) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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