Documentation
¶
Index ¶
- 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) 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 ¶
This section is empty.
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开销. Compared to WriteAsync, 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 广播 向单个客户端发送广播消息. 注意: 不要并行调用Broadcast方法 Send a broadcast message to a single client. Note: Do not call the Broadcast method in parallel.
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 {
// store session information
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
func (*Conn) ReadLoop ¶ added in v1.4.8
func (c *Conn) ReadLoop()
ReadLoop start a read message loop 启动一个读消息的死循环
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) WriteAsync ¶ added in v1.3.0
WriteAsync 异步非阻塞地写入消息 Write messages asynchronously and non-blockingly
func (*Conn) WriteClose ¶
WriteClose code: https://developer.mozilla.org/zh-CN/docs/Web/API/CloseEvent#status_codes 通过emitError发送关闭帧, 将连接状态置为关闭, 用于服务端主动断开连接 没有特殊原因的话, 建议code=0, reason=nil Send a close frame via emitError to set the connection state to closed, for server-initiated disconnection If there is no special reason, we suggest code=0, reason=nil
func (*Conn) WriteMessage ¶ added in v1.1.0
WriteMessage 发送消息
func (*Conn) WriteString ¶ added in v1.2.10
WriteString write text frame
type Event ¶ added in v1.1.2
type Event interface {
// 建立连接事件
// WebSocket connection was successfully established
OnOpen(socket *Conn)
// 关闭事件
// 接收到了网络连接另一端发送的关闭帧, 或者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)
// 心跳探测事件
// Received a ping frame
OnPing(socket *Conn, payload []byte)
// 心跳响应事件
// Received a pong frame
OnPong(socket *Conn, payload []byte)
// 消息事件
// 如果开启了ReadAsyncEnabled, 会并行地调用OnMessage; 没有做recover处理.
// If ReadAsyncEnabled is enabled, OnMessage is called in parallel. No recover is done.
OnMessage(socket *Conn, message *Message)
}
WebSocket Event
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 subprotocol, usually no need to set
Subprotocols []string
// 连接握手时添加的额外的响应头, 如果客户端不支持就不要传
// https://www.rfc-editor.org/rfc/rfc6455.html#section-1.3
// attention: client may not support custom response header, use nil instead
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
