Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Attr ¶ added in v2.3.0
type Attr interface {
// Set sets the value of the attribute with the given key.
Set(key, value any)
// Get returns the value of the attribute with the given key and whether it exists.
Get(key any) (any, bool)
// Del deletes the attribute with the given key and reports whether the deletion succeeded.
Del(key any) bool
// Clear removes all attributes.
Clear()
// Visit calls fn for every attribute and stops when fn returns false.
Visit(fn func(key, value any) bool)
}
Attr is the attribute interface of a connection.
type Client ¶
type Client interface {
// Dial dials a connection.
//
// When addr is empty the address from the client options is used.
Dial(addr ...string) (Conn, error)
// Protocol returns the protocol name.
Protocol() string
// OnConnect registers the handler invoked when a connection is opened.
OnConnect(handler ConnectHandler)
// OnHeartbeat registers the handler invoked when a heartbeat is received.
OnHeartbeat(handler HeartbeatHandler)
// OnReceive registers the handler invoked when a message is received.
OnReceive(handler ReceiveHandler)
// OnHandshake registers the handler invoked when a handshake frame is received.
OnHandshake(handler HandshakeHandler)
// OnDisconnect registers the handler invoked when a connection is closed.
OnDisconnect(handler DisconnectHandler)
}
Client is the client interface.
type CloseHandler ¶
type CloseHandler func()
CloseHandler is the handler invoked when the server stops.
type Conn ¶
type Conn interface {
// ID returns the connection ID.
ID() int64
// UID returns the user ID.
UID() int64
// Attr returns the attribute interface of the connection.
Attr() Attr
// Bind binds the connection to the given user ID.
Bind(uid int64) error
// Unbind unbinds the user ID from the connection.
Unbind() error
// Push sends a message with low priority.
//
// The caller controls when buf is released when the send fails.
Push(buf buffer.Buffer) error
// State returns the connection state.
State() ConnState
// Close closes the connection.
//
// When force is true the connection is closed forcibly.
Close(force ...bool) error
// LocalIP returns the local IP address.
LocalIP() (string, error)
// LocalAddr returns the local address.
LocalAddr() (net.Addr, error)
// RemoteIP returns the remote IP address.
RemoteIP() (string, error)
// RemoteAddr returns the remote address.
RemoteAddr() (net.Addr, error)
}
Conn is the connection interface.
type ConnectHandler ¶
type ConnectHandler func(conn Conn)
ConnectHandler is the handler invoked when a connection is opened.
type DisconnectHandler ¶
type DisconnectHandler func(conn Conn)
DisconnectHandler is the handler invoked when a connection is closed.
type HandshakeHandler ¶ added in v2.6.3
HandshakeHandler is the handler invoked when a handshake frame is received.
Handshake frames are marked by a reserved bit of the packet header instead of a route, and they are dispatched before the receive handler. The user layer controls when buf is released to avoid a memory leak.
type HeartbeatHandler ¶ added in v2.6.0
HeartbeatHandler is the handler invoked when a heartbeat is received.
heartbeatTime is the time in nanoseconds at which the client received the heartbeat sent by the server.
type ReceiveHandler ¶
ReceiveHandler is the handler invoked when a message is received.
The user layer controls when buf is released to avoid a memory leak.
type Server ¶
type Server interface {
// Addr returns the listen address.
Addr() string
// Start starts the server.
Start() error
// Stop stops the server.
Stop() error
// Protocol returns the protocol name.
Protocol() string
// OnStart registers the handler invoked when the server starts.
OnStart(handler StartHandler)
// OnStop registers the handler invoked when the server stops.
OnStop(handler CloseHandler)
// OnConnect registers the handler invoked when a connection is opened.
OnConnect(handler ConnectHandler)
// OnHeartbeat registers the handler invoked when a heartbeat is received.
OnHeartbeat(handler HeartbeatHandler)
// OnReceive registers the handler invoked when a message is received.
OnReceive(handler ReceiveHandler)
// OnHandshake registers the handler invoked when a handshake frame is received.
OnHandshake(handler HandshakeHandler)
// OnDisconnect registers the handler invoked when a connection is closed.
OnDisconnect(handler DisconnectHandler)
}
Server is the server interface.
type StartHandler ¶
type StartHandler func()
StartHandler is the handler invoked when the server starts.