Documentation
¶
Index ¶
- Variables
- type Broadcaster
- type BuiltinEventHandler
- func (b BuiltinEventHandler) OnClose(socket *Conn, err error)
- func (b BuiltinEventHandler) OnMessage(socket *Conn, message *Message)
- func (b BuiltinEventHandler) OnOpen(socket *Conn)
- func (b BuiltinEventHandler) OnPing(socket *Conn, payload []byte)
- func (b BuiltinEventHandler) OnPong(socket *Conn, payload []byte)
- type ClientOption
- type CloseError
- type Comparable
- type ConcurrentMap
- type Config
- type Conn
- func (c *Conn) LocalAddr() net.Addr
- func (c *Conn) NetConn() net.Conn
- func (c *Conn) ReadLoop()
- func (c *Conn) RemoteAddr() net.Addr
- func (c *Conn) SetDeadline(t time.Time) error
- func (c *Conn) SetNoDelay(noDelay bool) error
- func (c *Conn) SetReadDeadline(t time.Time) error
- func (c *Conn) SetWriteDeadline(t time.Time) error
- func (c *Conn) SubProtocol() string
- func (c *Conn) WriteAsync(opcode Opcode, payload []byte) error
- func (c *Conn) WriteClose(code uint16, reason []byte)
- func (c *Conn) WriteMessage(opcode Opcode, payload []byte) error
- func (c *Conn) WritePing(payload []byte) error
- func (c *Conn) WritePong(payload []byte) error
- func (c *Conn) WriteString(s string) error
- type Dialer
- type Event
- type Message
- type Opcode
- type Server
- type ServerOption
- type SessionStorage
- type Upgrader
Constants ¶
This section is empty.
Variables ¶
var ( // Failure to pass forensic authentication ErrUnauthorized = errors.New("unauthorized") // ErrHandshake 握手错误, 请求头未通过校验 // Handshake error, request header does not pass checksum. ErrHandshake = errors.New("handshake error") // ErrCompressionNegotiation 压缩拓展协商失败, 请尝试关闭压缩 // Compression extension negotiation failed, please try to disable compression. ErrCompressionNegotiation = errors.New("invalid compression negotiation") // ErrSubprotocolNegotiation 子协议协商失败 // Sub-protocol negotiation failed ErrSubprotocolNegotiation = errors.New("sub-protocol negotiation failed") // ErrTextEncoding 文本消息编码错误(必须是utf8编码) // Text message encoding error (must be utf8) ErrTextEncoding = errors.New("invalid text encoding") // ErrConnClosed 连接已关闭 // Connection closed ErrConnClosed = net.ErrClosed // ErrUnsupportedProtocol 不支持的网络协议 // Unsupported network protocols ErrUnsupportedProtocol = errors.New("unsupported protocol") )
Functions ¶
This section is empty.
Types ¶
type Broadcaster ¶ added in v1.6.2
type Broadcaster struct {
// contains filtered or unexported fields
}
func NewBroadcaster ¶ added in v1.6.2
func NewBroadcaster(opcode Opcode, payload []byte) *Broadcaster
NewBroadcaster 创建广播器 相比循环调用WriteAsync, Broadcaster只会压缩一次消息, 可以节省大量CPU开销. Instead of calling WriteAsync in a loop, Broadcaster compresses the message only once, saving a lot of CPU overhead.
func (*Broadcaster) Broadcast ¶ added in v1.6.2
func (c *Broadcaster) Broadcast(socket *Conn) error
Broadcast 广播 向客户端发送广播消息 Send a broadcast message to a client.
func (*Broadcaster) Release ¶ added in v1.6.2
func (c *Broadcaster) Release()
Release 释放资源 在完成所有Broadcast调用之后执行Release方法释放资源. Call the Release method after all the Broadcasts have been completed to release the resources.
type BuiltinEventHandler ¶ added in v1.3.0
type BuiltinEventHandler struct{}
func (BuiltinEventHandler) OnClose ¶ added in v1.3.0
func (b BuiltinEventHandler) OnClose(socket *Conn, err error)
func (BuiltinEventHandler) OnMessage ¶ added in v1.3.0
func (b BuiltinEventHandler) OnMessage(socket *Conn, message *Message)
func (BuiltinEventHandler) OnOpen ¶ added in v1.3.0
func (b BuiltinEventHandler) OnOpen(socket *Conn)
func (BuiltinEventHandler) OnPing ¶ added in v1.3.0
func (b BuiltinEventHandler) OnPing(socket *Conn, payload []byte)
func (BuiltinEventHandler) OnPong ¶ added in v1.3.0
func (b BuiltinEventHandler) OnPong(socket *Conn, payload []byte)
type ClientOption ¶ added in v1.4.2
type ClientOption struct {
// 写缓冲区的大小, v1.4.5版本此参数被废弃
// Deprecated: Size of the write buffer, v1.4.5 version of this parameter is deprecated
WriteBufferSize int
ReadAsyncEnabled bool
ReadAsyncGoLimit int
ReadMaxPayloadSize int
ReadBufferSize int
WriteMaxPayloadSize int
CompressEnabled bool
CompressLevel int
CompressThreshold int
CheckUtf8Enabled bool
// 连接地址, 例如 wss://example.com/connect
// server address, eg: wss://example.com/connect
Addr string
// 额外的请求头
// extra request header
RequestHeader http.Header
// 握手超时时间
HandshakeTimeout time.Duration
// TLS设置
TlsConfig *tls.Config
// 拨号器
// 默认是返回net.Dialer实例, 也可以用于设置代理.
// The default is to return the net.Dialer instance
// Can also be used to set a proxy, for example
// NewDialer: func() (proxy.Dialer, error) {
// return proxy.SOCKS5("tcp", "127.0.0.1:1080", nil, nil)
// },
NewDialer func() (Dialer, error)
// 创建session存储空间
// 用于自定义SessionStorage实现
// For custom SessionStorage implementations
NewSessionStorage func() SessionStorage
}
type CloseError ¶ added in v1.6.0
func (*CloseError) Error ¶ added in v1.6.0
func (c *CloseError) Error() string
type Comparable ¶ added in v1.5.1
type ConcurrentMap ¶ added in v1.2.5
type ConcurrentMap[K Comparable, V any] struct { // contains filtered or unexported fields }
func NewConcurrentMap ¶ added in v1.2.5
func NewConcurrentMap[K Comparable, V any](segments uint64) *ConcurrentMap[K, V]
func (*ConcurrentMap[K, V]) Delete ¶ added in v1.2.5
func (c *ConcurrentMap[K, V]) Delete(key K)
func (*ConcurrentMap[K, V]) Len ¶ added in v1.2.5
func (c *ConcurrentMap[K, V]) Len() int
func (*ConcurrentMap[K, V]) Load ¶ added in v1.2.5
func (c *ConcurrentMap[K, V]) Load(key K) (value V, exist bool)
func (*ConcurrentMap[K, V]) Range ¶ added in v1.2.5
func (c *ConcurrentMap[K, V]) Range(f func(key K, value V) bool)
Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.
func (*ConcurrentMap[K, V]) Store ¶ added in v1.2.5
func (c *ConcurrentMap[K, V]) Store(key K, value V)
type Config ¶ added in v1.2.0
type Config struct {
// 是否开启异步读, 开启的话会并行调用OnMessage
// Whether to enable asynchronous reading, if enabled OnMessage will be called in parallel
ReadAsyncEnabled bool
// 异步读的最大并行协程数量
// Maximum number of parallel concurrent processes for asynchronous reads
ReadAsyncGoLimit int
// 最大读取的消息内容长度
// Maximum read message content length
ReadMaxPayloadSize int
// 读缓冲区的大小
// Size of the read buffer
ReadBufferSize int
// 最大写入的消息内容长度
// Maximum length of written message content
WriteMaxPayloadSize int
// 写缓冲区的大小, v1.4.5版本此参数被废弃
// Deprecated: Size of the write buffer, v1.4.5 version of this parameter is deprecated
WriteBufferSize int
// 是否开启数据压缩
// Whether to turn on data compression
CompressEnabled bool
// 压缩级别
// Compress level
CompressLevel int
// 压缩阈值, 低于阈值的消息不会被压缩
// Compression threshold, messages below the threshold will not be compressed
CompressThreshold int
// CompressorNum 压缩器数量
// 数值越大竞争的概率越小, 但是会耗费大量内存, 注意取舍
// Number of compressors
// The higher the value the lower the probability of competition, but it will consume a lot of memory, so be careful about the trade-off
CompressorNum int
// 是否检查文本utf8编码, 关闭性能会好点
// Whether to check the text utf8 encoding, turn off the performance will be better
CheckUtf8Enabled bool
// contains filtered or unexported fields
}
type Conn ¶
type Conn struct {
SessionStorage SessionStorage // 会话
// contains filtered or unexported fields
}
func NewClientFromConn ¶ added in v1.5.1
func NewClientFromConn(handler Event, option *ClientOption, conn net.Conn) (*Conn, *http.Response, error)
NewClientFromConn 通过外部连接创建客户端, 支持 TCP/KCP/Unix Domain Socket Create New client via external connection, supports TCP/KCP/Unix Domain Socket.
func (*Conn) ReadLoop ¶ added in v1.4.8
func (c *Conn) ReadLoop()
ReadLoop 循环读取消息. 如果复用了HTTP Server, 建议开启goroutine, 阻塞会导致请求上下文无法被GC. Read messages in a loop. If HTTP Server is reused, it is recommended to enable goroutine, as blocking will prevent the context from being GC.
func (*Conn) RemoteAddr ¶ added in v1.0.1
func (*Conn) SetNoDelay ¶ added in v1.6.2
SetNoDelay controls whether the operating system should delay packet transmission in hopes of sending fewer packets (Nagle's algorithm). The default is true (no delay), meaning that data is sent as soon as possible after a Write.
func (*Conn) SetReadDeadline ¶ added in v1.1.2
SetReadDeadline sets read deadline
func (*Conn) SetWriteDeadline ¶ added in v1.1.2
SetWriteDeadline sets write deadline
func (*Conn) SubProtocol ¶ added in v1.6.5
SubProtocol 获取协商的子协议 Get negotiated sub-protocols
func (*Conn) WriteAsync ¶ added in v1.3.0
WriteAsync 异步非阻塞地写入消息 Write messages asynchronously and non-blocking
func (*Conn) WriteClose ¶
WriteClose 发送关闭帧, 主动断开连接 没有特殊需求的话, 推荐code=1000, reason=nil Send shutdown frame, active disconnection If you don't have any special needs, we recommend code=1000, reason=nil https://developer.mozilla.org/zh-CN/docs/Web/API/CloseEvent#status_codes
func (*Conn) WriteMessage ¶ added in v1.1.0
WriteMessage 写入文本/二进制消息, 文本消息应该使用UTF8编码 Write text/binary messages, text messages should be encoded in UTF8.
func (*Conn) WritePing ¶
WritePing 写入Ping消息, 携带的信息不要超过125字节 Control frame length cannot exceed 125 bytes
func (*Conn) WritePong ¶
WritePong 写入Pong消息, 携带的信息不要超过125字节 Control frame length cannot exceed 125 bytes
func (*Conn) WriteString ¶ added in v1.2.10
WriteString 写入文本消息, 使用UTF8编码. Write text messages, should be encoded in UTF8.
type Event ¶ added in v1.1.2
type Event interface {
// OnOpen 建立连接事件
// WebSocket connection was successfully established
OnOpen(socket *Conn)
// OnClose 关闭事件
// 接收到了网络连接另一端发送的关闭帧, 或者IO过程中出现错误主动断开连接
// 如果是前者, err可以断言为*CloseError
// Received a close frame from the other end of the network connection, or disconnected voluntarily due to an error in the IO process
// In the former case, err can be asserted as *CloseError
OnClose(socket *Conn, err error)
// OnPing 心跳探测事件
// Received a ping frame
OnPing(socket *Conn, payload []byte)
// OnPong 心跳响应事件
// Received a pong frame
OnPong(socket *Conn, payload []byte)
// OnMessage 消息事件
// 如果开启了ReadAsyncEnabled, 会并行地调用OnMessage; 没有做recover处理.
// If ReadAsyncEnabled is enabled, OnMessage is called in parallel. No recover is done.
OnMessage(socket *Conn, message *Message)
}
type Message ¶
type Server ¶ added in v1.4.7
type Server struct {
// OnError 接收握手过程中产生的错误回调
// Receive error callbacks generated during the handshake
OnError func(conn net.Conn, err error)
// OnRequest
OnRequest func(socket *Conn, request *http.Request)
// contains filtered or unexported fields
}
func NewServer ¶ added in v1.4.7
func NewServer(eventHandler Event, option *ServerOption) *Server
NewServer 创建websocket服务器 create a websocket server
func (*Server) RunListener ¶ added in v1.4.9
type ServerOption ¶ added in v1.4.0
type ServerOption struct {
// 写缓冲区的大小, v1.4.5版本此参数被废弃
// Deprecated: Size of the write buffer, v1.4.5 version of this parameter is deprecated
WriteBufferSize int
ReadAsyncEnabled bool
ReadAsyncGoLimit int
ReadMaxPayloadSize int
ReadBufferSize int
WriteMaxPayloadSize int
CompressEnabled bool
CompressLevel int
CompressThreshold int
CompressorNum int
CheckUtf8Enabled bool
// 握手超时时间
HandshakeTimeout time.Duration
// WebSocket子协议, 握手失败会断开连接
// WebSocket sub-protocol, handshake failure disconnects the connection
SubProtocols []string
// 额外的响应头(可能不受客户端支持)
// Additional response headers (may not be supported by the client)
// https://www.rfc-editor.org/rfc/rfc6455.html#section-1.3
ResponseHeader http.Header
// 鉴权
// Authentication of requests for connection establishment
Authorize func(r *http.Request, session SessionStorage) bool
// 创建session存储空间
// 用于自定义SessionStorage实现
// For custom SessionStorage implementations
NewSessionStorage func() SessionStorage
// contains filtered or unexported fields
}
type SessionStorage ¶ added in v1.2.3
type Upgrader ¶
type Upgrader struct {
// contains filtered or unexported fields
}
func NewUpgrader ¶ added in v1.2.11
func NewUpgrader(eventHandler Event, option *ServerOption) *Upgrader
