Documentation ¶
Index ¶
- Variables
- func Recovery(logger Logger)
- 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 ConcurrentMap
- func (c *ConcurrentMap[K, V]) Delete(key K)
- func (c *ConcurrentMap[K, V]) GetSharding(key K) *Map[K, V]
- func (c *ConcurrentMap[K, V]) Len() int
- func (c *ConcurrentMap[K, V]) Load(key K) (value V, ok bool)
- func (c *ConcurrentMap[K, V]) Range(f func(key K, value V) bool)
- func (c *ConcurrentMap[K, V]) Store(key K, value V)
- type Config
- type Conn
- func (c *Conn) Async(f func())
- func (c *Conn) Context() context.Context
- 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) Session() SessionStorage
- 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, callback func(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
- func (c *Conn) Writev(opcode Opcode, payloads ...[]byte) error
- func (c *Conn) WritevAsync(opcode Opcode, payloads [][]byte, callback func(error))
- type Dialer
- type Event
- type Logger
- type Map
- type Message
- type Opcode
- type PermessageDeflate
- 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 ¶
Types ¶
type Broadcaster ¶
type Broadcaster struct {
// contains filtered or unexported fields
}
func NewBroadcaster ¶
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 ¶
func (c *Broadcaster) Broadcast(socket *Conn) error
Broadcast 广播 向客户端发送广播消息 Send a broadcast message to a client.
func (*Broadcaster) Close ¶
func (c *Broadcaster) Close() error
Close 释放资源 在完成所有Broadcast调用之后执行Close方法释放资源. Call the Close method after all the Broadcasts have been completed to release the resources.
type BuiltinEventHandler ¶
type BuiltinEventHandler struct{}
func (BuiltinEventHandler) OnClose ¶
func (b BuiltinEventHandler) OnClose(socket *Conn, err error)
func (BuiltinEventHandler) OnMessage ¶
func (b BuiltinEventHandler) OnMessage(socket *Conn, message *Message)
func (BuiltinEventHandler) OnOpen ¶
func (b BuiltinEventHandler) OnOpen(socket *Conn)
func (BuiltinEventHandler) OnPing ¶
func (b BuiltinEventHandler) OnPing(socket *Conn, payload []byte)
func (BuiltinEventHandler) OnPong ¶
func (b BuiltinEventHandler) OnPong(socket *Conn, payload []byte)
type ClientOption ¶
type ClientOption struct { // 写缓冲区的大小, v1.4.5版本此参数被废弃 // Deprecated: Size of the write buffer, v1.4.5 version of this parameter is deprecated WriteBufferSize int PermessageDeflate PermessageDeflate ParallelEnabled bool ParallelGolimit int ReadMaxPayloadSize int ReadBufferSize int WriteMaxPayloadSize int CheckUtf8Enabled bool Logger Logger Recovery func(logger Logger) // 连接地址, 例如 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 NewSession func() SessionStorage }
type CloseError ¶
func (*CloseError) Error ¶
func (c *CloseError) Error() string
type ConcurrentMap ¶
type ConcurrentMap[K comparable, V any] struct { // contains filtered or unexported fields }
func NewConcurrentMap ¶
func NewConcurrentMap[K comparable, V any](size ...uint64) *ConcurrentMap[K, V]
NewConcurrentMap create a new concurrency-safe map arg0 represents the number of shardings; arg1 represents the initialized capacity of a sharding.
func (*ConcurrentMap[K, V]) Delete ¶
func (c *ConcurrentMap[K, V]) Delete(key K)
Delete deletes the value for a key.
func (*ConcurrentMap[K, V]) GetSharding ¶
func (c *ConcurrentMap[K, V]) GetSharding(key K) *Map[K, V]
GetSharding returns a map sharding for a key the operations inside the sharding is lockless, and need to be locked manually.
func (*ConcurrentMap[K, V]) Len ¶
func (c *ConcurrentMap[K, V]) Len() int
Len returns the number of elements in the map
func (*ConcurrentMap[K, V]) Load ¶
func (c *ConcurrentMap[K, V]) Load(key K) (value V, ok bool)
Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.
func (*ConcurrentMap[K, V]) Range ¶
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 ¶
func (c *ConcurrentMap[K, V]) Store(key K, value V)
Store sets the value for a key.
type Config ¶
type Config struct { // 是否开启并行消息处理 // Whether to enable parallel message processing ParallelEnabled bool // (单个连接)用于并行消息处理的协程数量限制 // Limit on the number of concurrent goroutine used for parallel message processing (single connection) ParallelGolimit 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 // 是否检查文本utf8编码, 关闭性能会好点 // Whether to check the text utf8 encoding, turn off the performance will be better CheckUtf8Enabled bool // 消息回调(OnMessage)的恢复程序 // Message callback (OnMessage) recovery program Recovery func(logger Logger) // 日志工具 // Logging tools Logger Logger // contains filtered or unexported fields }
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
func NewClientFromConn ¶
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) Async ¶
func (c *Conn) Async(f func())
Async 异步 将任务加入发送队列(并发度为1), 执行异步操作 注意: 不要加入长时间阻塞的任务 Add the task to the send queue (concurrency 1), perform asynchronous operation. Note: Don't add tasks that are blocking for a long time.
func (*Conn) ReadLoop ¶
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 ¶
func (*Conn) SetNoDelay ¶
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 ¶
SetReadDeadline sets read deadline
func (*Conn) SetWriteDeadline ¶
SetWriteDeadline sets write deadline
func (*Conn) SubProtocol ¶
SubProtocol 获取协商的子协议 Get negotiated sub-protocols
func (*Conn) WriteAsync ¶
WriteAsync 异步写 异步非阻塞地将消息写入到任务队列, 收到回调后才允许回收payload内存 Asynchronously and non-blockingly write the message to the task queue, allowing the payload memory to be reclaimed only after a callback is received.
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 ¶
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 ¶
WriteString 写入文本消息, 使用UTF8编码. Write text messages, should be encoded in UTF8.
type Event ¶
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 消息事件 // 如果开启了ParallelEnabled, 会并行地调用OnMessage; 没有做recover处理. // If ParallelEnabled is enabled, OnMessage is called in parallel. No recover is done. OnMessage(socket *Conn, message *Message) }
type Map ¶
type Map[K comparable, V any] struct { sync.Mutex // contains filtered or unexported fields }
type Message ¶
type PermessageDeflate ¶
type PermessageDeflate struct { // 是否开启压缩 // Whether to turn on compression Enabled bool // 压缩级别 // Compress level Level int // 压缩阈值, 长度小于阈值的消息不会被压缩, 仅适用于无上下文接管模式. // Compression threshold, messages below the threshold will not be compressed, only for context-free takeover mode. Threshold int // 压缩器内存池大小 // 数值越大竞争的概率越小, 但是会耗费大量内存 // Compressor memory pool size // The higher the value the lower the probability of competition, but it will consume a lot of memory PoolSize int // 服务端上下文接管 // Server side context takeover ServerContextTakeover bool // 客户端上下文接管 // Client side context takeover ClientContextTakeover bool // 服务端滑动窗口指数 // 取值范围 8<=n<=15, 表示pow(2,n)个字节 // The server-side sliding window index // Range 8<=n<=15, means pow(2,n) bytes. ServerMaxWindowBits int // 客户端滑动窗口指数 // 取值范围 8<=x<=15, 表示pow(2,n)个字节 // The client-side sliding window index // Range 8<=n<=15, means pow(2,n) bytes. ClientMaxWindowBits int }
PermessageDeflate 压缩拓展配置 对于gws client, 建议开启上下文接管, 不修改滑动窗口指数, 提供最好的兼容性. 对于gws server, 如果开启上下文接管, 每个连接会占用更多内存, 合理配置滑动窗口指数. For gws client, it is recommended to enable contextual takeover and not modify the sliding window index to provide the best compatibility. For gws server, if you turn on context-side takeover, each connection takes up more memory, configure the sliding window index appropriately.
type Server ¶
type Server struct { // OnError OnError func(conn net.Conn, err error) // OnRequest OnRequest func(conn net.Conn, br *bufio.Reader, r *http.Request) // contains filtered or unexported fields }
func NewServer ¶
func NewServer(eventHandler Event, option *ServerOption) *Server
NewServer 创建websocket服务器 create a websocket server
func (*Server) GetUpgrader ¶
func (*Server) Run ¶
Run 运行. 可以被多次调用, 监听不同的地址. It can be called multiple times, listening to different addresses.
func (*Server) RunListener ¶
RunListener 运行网络监听器 Running the network listener
type ServerOption ¶
type ServerOption struct { // 写缓冲区的大小, v1.4.5版本此参数被废弃 // Deprecated: Size of the write buffer, v1.4.5 version of this parameter is deprecated WriteBufferSize int PermessageDeflate PermessageDeflate ParallelEnabled bool ParallelGolimit int ReadMaxPayloadSize int ReadBufferSize int WriteMaxPayloadSize int CheckUtf8Enabled bool Logger Logger Recovery func(logger Logger) // TLS设置 TlsConfig *tls.Config // 握手超时时间 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 NewSession func() SessionStorage // contains filtered or unexported fields }
type SessionStorage ¶
type Upgrader ¶
type Upgrader struct {
// contains filtered or unexported fields
}
func NewUpgrader ¶
func NewUpgrader(eventHandler Event, option *ServerOption) *Upgrader