Documentation
¶
Index ¶
- type BuiltinEventHandler
- func (b BuiltinEventHandler) OnClose(socket *Conn, code uint16, reason []byte)
- func (b BuiltinEventHandler) OnError(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 ConcurrentMap
- func (c *ConcurrentMap) Delete(key interface{})
- func (c *ConcurrentMap) Len() int
- func (c *ConcurrentMap) Load(key interface{}) (value interface{}, exist bool)
- func (c *ConcurrentMap) Range(f func(key interface{}, value interface{}) bool)
- func (c *ConcurrentMap) Store(key interface{}, value interface{})
- type Config
- type Conn
- func (c *Conn) Listen()
- func (c *Conn) LocalAddr() net.Addr
- func (c *Conn) NetConn() net.Conn
- func (c *Conn) RemoteAddr() net.Addr
- func (c *Conn) SetDeadline(t time.Time) 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 Event
- type Message
- type Opcode
- type ServerOption
- type SessionStorage
- type Upgrader
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BuiltinEventHandler ¶ added in v1.3.0
type BuiltinEventHandler struct{}
func (BuiltinEventHandler) OnClose ¶ added in v1.3.0
func (b BuiltinEventHandler) OnClose(socket *Conn, code uint16, reason []byte)
func (BuiltinEventHandler) OnError ¶ added in v1.3.0
func (b BuiltinEventHandler) OnError(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
ReadAsyncCap int
ReadMaxPayloadSize int
ReadBufferSize int
WriteAsyncCap int
WriteMaxPayloadSize int
CompressEnabled bool
CompressLevel int
CompressThreshold int
CheckUtf8Enabled bool
// 连接地址, 例如 wss://example.com/connect
// service address, eg: wss://example.com/connect
Addr string
// 额外的请求头
// extra request header
RequestHeader http.Header
// dial timeout
// 连接超时时间
DialTimeout time.Duration
// TLS设置
// tls config
TlsConfig *tls.Config
}
type ConcurrentMap ¶ added in v1.2.5
type ConcurrentMap struct {
// contains filtered or unexported fields
}
ConcurrentMap used to store websocket connections in the IM server 用来存储IM等服务的连接
func NewConcurrentMap ¶ added in v1.2.5
func NewConcurrentMap(segments uint64) *ConcurrentMap
func (*ConcurrentMap) Delete ¶ added in v1.2.5
func (c *ConcurrentMap) Delete(key interface{})
func (*ConcurrentMap) Len ¶ added in v1.2.5
func (c *ConcurrentMap) Len() int
func (*ConcurrentMap) Load ¶ added in v1.2.5
func (c *ConcurrentMap) Load(key interface{}) (value interface{}, exist bool)
func (*ConcurrentMap) Range ¶ added in v1.2.5
func (c *ConcurrentMap) Range(f func(key interface{}, value interface{}) bool)
Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.
func (*ConcurrentMap) Store ¶ added in v1.2.5
func (c *ConcurrentMap) Store(key interface{}, value interface{})
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
// 异步读的容量限制, 容量溢出将会返回错误
// Capacity limit for asynchronous reads, overflow will return an error
ReadAsyncCap int
// 最大读取的消息内容长度
// Maximum read message content length
ReadMaxPayloadSize int
// 读缓冲区的大小
// Size of the read buffer
ReadBufferSize int
// 异步写的容量限制, 容量溢出将会返回错误
// Capacity limit for asynchronous writes, overflow will return an error
WriteAsyncCap 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
// 是否检查文本utf8编码, 关闭性能会好点
// Whether to check the text utf8 encoding, turn off the performance will be better
CheckUtf8Enabled bool
}
type Conn ¶
type Conn struct {
// store session information
SessionStorage SessionStorage
// contains filtered or unexported fields
}
func (*Conn) Listen ¶ added in v1.1.2
func (c *Conn) Listen()
Listen listening to websocket messages through a dead loop 监听websocket消息
func (*Conn) RemoteAddr ¶ added in v1.0.1
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 proactively close the connection code: https://developer.mozilla.org/zh-CN/docs/Web/API/CloseEvent#status_codes 通过emitError发送关闭帧, 将连接状态置为关闭, 用于服务端主动断开连接 没有特殊原因的话, 建议code=0, reason=nil
func (*Conn) WriteMessage ¶ added in v1.1.0
WriteMessage 发送消息 如果是客户端, payload内容会被改变 writes message
func (*Conn) WriteString ¶ added in v1.2.10
WriteString write text frame force convert string to []byte
type Event ¶ added in v1.1.2
type Event interface {
// 建立连接事件
OnOpen(socket *Conn)
// 错误事件
// IO错误, 协议错误, 压缩解压错误...
OnError(socket *Conn, err error)
// 关闭事件
// 另一端发送了关闭帧
OnClose(socket *Conn, code uint16, reason []byte)
// 心跳探测事件
OnPing(socket *Conn, payload []byte)
// 心跳响应事件
OnPong(socket *Conn, payload []byte)
// 消息事件
// 如果开启了AsyncReadEnabled, 可以在一个连接里面并行处理多个请求
OnMessage(socket *Conn, message *Message)
}
WebSocket Event one of onclose and onerror will be called once during the connection's lifetime. 在连接的生命周期中,onclose和onerror中的一个有且只有一次被调用.
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
ReadAsyncCap int
ReadMaxPayloadSize int
ReadBufferSize int
WriteAsyncCap int
WriteMaxPayloadSize int
CompressEnabled bool
CompressLevel int
CompressThreshold int
CheckUtf8Enabled bool
// 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
// 检查请求来源
// Check the origin of the request
CheckOrigin func(r *http.Request, session SessionStorage) bool
}
type SessionStorage ¶ added in v1.2.3
type SessionStorage interface {
Load(key string) (value interface{}, exist bool)
Delete(key string)
Store(key string, value interface{})
Range(f func(key string, value interface{}) bool)
}
SessionStorage because sync.Map is not easy to debug, so I implemented my own map. if you don't like it, use sync.Map instead.
type Upgrader ¶
type Upgrader struct {
// contains filtered or unexported fields
}
func NewUpgrader ¶ added in v1.2.11
func NewUpgrader(eventHandler Event, option *ServerOption) *Upgrader


