Documentation
¶
Index ¶
- type Config
- type Conn
- func (c *Conn) Close(code uint16, reason []byte)
- func (c *Conn) Listen()
- func (c *Conn) LocalAddr() net.Addr
- func (c *Conn) RemoteAddr() net.Addr
- func (c *Conn) SetDeadline(t time.Time)
- func (c *Conn) SetReadDeadline(t time.Time)
- func (c *Conn) SetWriteDeadline(t time.Time)
- func (c *Conn) WriteMessage(opcode Opcode, payload []byte)
- func (c *Conn) WritePing(payload []byte)
- func (c *Conn) WritePong(payload []byte)
- type Event
- type Map
- type Message
- type Opcode
- type Request
- type SessionStorage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶ added in v1.2.0
type Config struct {
// whether to compress data
CompressEnabled bool
// compress level eg: flate.BestSpeed
CompressLevel int
// max message size
MaxContentLength int
// whether to check utf8 encoding, disabled for better performance
CheckTextEncoding bool
// client authentication
Authenticate func(r *Request) bool
}
type Conn ¶
type Conn struct {
// Concurrent Variable
// store session information
SessionStorage SessionStorage
// contains filtered or unexported fields
}
func Accept ¶ added in v1.2.0
func Accept(ctx context.Context, w http.ResponseWriter, r *http.Request, eventHandler Event, config Config) (*Conn, error)
Accept http protocol upgrade to websocket ctx done means server stopping
func (*Conn) Close ¶
Close write closed frame code: https://developer.mozilla.org/zh-CN/docs/Web/API/CloseEvent#status_codes 发送关闭帧, 并将连接状态置为关闭
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) WriteMessage ¶ added in v1.1.0
WriteMessage write text/binary message text message must be utf8 encoding 发送文本/二进制消息, 文本消息必须是utf8编码
type Event ¶ added in v1.1.2
type Event interface {
OnOpen(socket *Conn)
OnError(socket *Conn, err error)
OnClose(socket *Conn, code uint16, reason []byte)
OnPing(socket *Conn, payload []byte)
OnPong(socket *Conn, payload []byte)
OnMessage(socket *Conn, message *Message)
}
WebSocket Event OnError and OnClose will not both be called
type Map ¶ added in v1.2.3
type Map struct {
// contains filtered or unexported fields
}
func (*Map) Delete ¶ added in v1.2.3
func (c *Map) Delete(key interface{})
Delete deletes the value for a key.
type Request ¶
type Request struct {
*http.Request // http request
SessionStorage SessionStorage // store user session
}
type SessionStorage ¶ added in v1.2.3
type SessionStorage interface {
Load(key interface{}) (value interface{}, exist bool)
Delete(key interface{})
Store(key interface{}, value interface{})
Range(f func(key, value interface{}) bool)
}
SessionStorage because sync.Map is not easy to debug, so I implemented my own map. if you don't like it, you can also use sync.Map instead.