ws

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const CompressThreshold = 1024 // 1KB

CompressThreshold 压缩阈值:超过1KB的消息才压缩

View Source
const HeaderLen = 4

HeaderLen 固定头部长度:4字节

View Source
const MaxMessageSize = 8 * 1024 // 单条消息最大16KB

Variables

View Source
var ByteOrder = binary.BigEndian

新增:全局字节序变量,方便外部调用

Functions

This section is empty.

Types

type BufferPool

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

func (*BufferPool) Get

func (p *BufferPool) Get() interface{}

func (*BufferPool) Put

func (p *BufferPool) Put(v interface{})

type CloseType

type CloseType int
const (
	None    CloseType = 0 //普通关闭
	Replace CloseType = 1 //顶号登录
)

type Option

type Option struct {
	Port              string
	Path              string
	ReadBufferSize    int
	WriteBufferSize   int
	WriteBufferPool   websocket.BufferPool
	CheckOrigin       func(r *http.Request) bool
	EnableCompression bool
}

func DefaultOption

func DefaultOption() *Option

type WSConn

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

func (*WSConn) Close

func (c *WSConn) Close(ct CloseType, err error)

func (*WSConn) Decompress

func (c *WSConn) Decompress(data []byte) ([]byte, error)

Decompress 解压数据(仅当isCompressed=true时调用)

func (*WSConn) GetId

func (c *WSConn) GetId() ID.Id

func (*WSConn) Pack

func (c *WSConn) Pack(msgId uint32, msg []byte) []byte

func (*WSConn) SendIDMSG

func (c *WSConn) SendIDMSG(msgId uint32, msg []byte)

func (*WSConn) SendMSG

func (c *WSConn) SendMSG(msg []byte)

func (*WSConn) SendNow

func (c *WSConn) SendNow(msgId uint32, msg []byte)

func (*WSConn) Start

func (c *WSConn) Start()

func (*WSConn) Unpack

func (c *WSConn) Unpack(header []byte) (dataLen uint32, isCompressed bool)

Unpack 解包:从头部解析压缩标记和数据长度 header:4字节头部

type WSServer

type WSServer struct {
	Opt *Option //配置
	// contains filtered or unexported fields
}

func DefaultServer

func DefaultServer(port, path string) *WSServer

func NewServer

func NewServer(port, path string, readBufferSize, writeBufferSize int, frequency int32) *WSServer

func (*WSServer) GetConnect

func (s *WSServer) GetConnect(uid ID.Id) *WSConn

func (*WSServer) Listen

func (s *WSServer) Listen()

func (*WSServer) Remove

func (s *WSServer) Remove(c *WSConn, ct CloseType, err error)

func (*WSServer) RemoveDelay

func (s *WSServer) RemoveDelay(c *WSConn, unixMilli int64)

func (*WSServer) SetCheckOriginFunc

func (s *WSServer) SetCheckOriginFunc(f func(r *http.Request) bool)

func (*WSServer) SetCloseHandler

func (s *WSServer) SetCloseHandler(f func(session *WSConn, ct CloseType))

func (*WSServer) SetDispatcher

func (s *WSServer) SetDispatcher(f func(session *WSConn, msgId uint32, data []byte))

func (*WSServer) SetTLS

func (s *WSServer) SetTLS(certificate, key string)

func (*WSServer) SetVerifyHandler

func (s *WSServer) SetVerifyHandler(f func(*http.Request) (ID.Id, error))

Jump to

Keyboard shortcuts

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