Documentation
¶
Index ¶
- Variables
- type Adler
- func (a *Adler) Broadcast(msg []byte) error
- func (a *Adler) BroadcastAny(v any) error
- func (a *Adler) BroadcastBinary(msg []byte) error
- func (a *Adler) BroadcastBinaryFilter(msg []byte, fn func(*Session) bool) error
- func (a *Adler) BroadcastFilter(msg []byte, fn func(*Session) bool) error
- func (a *Adler) BroadcastJSON(v Map) error
- func (a *Adler) BroadcastJSONFilter(v Map, fn func(*Session) bool) error
- func (a *Adler) BroadcastOthers(msg []byte, s *Session) error
- func (a *Adler) Close() error
- func (a *Adler) DeleteRoom(roomName string) error
- func (a *Adler) HandleClose(fn func(*Session, int, string))
- func (a *Adler) HandleConnect(fn func(*Session))
- func (a *Adler) HandleDisconnect(fn func(*Session))
- func (a *Adler) HandleError(fn func(*Session, error))
- func (a *Adler) HandleMessage(fn func(*Session, []byte))
- func (a *Adler) HandleMessageBinary(fn func(*Session, []byte))
- func (a *Adler) HandlePong(fn func(*Session))
- func (a *Adler) HandleRequest(w http.ResponseWriter, r *http.Request) error
- func (a *Adler) HandleSentMessage(fn func(*Session, []byte))
- func (a *Adler) HandleSentMessageBinary(fn func(*Session, []byte))
- func (a *Adler) IsClosed() bool
- func (a *Adler) Len() int
- func (a *Adler) NewRoom(name string) *Room
- func (a *Adler) OnRoomJoin(fn func(*Session, *Room))
- func (a *Adler) OnRoomLeave(fn func(*Session, *Room))
- func (a *Adler) Room(roomName string) (*Room, error)
- func (a *Adler) SendTo(msg []byte, s *Session) error
- type Config
- type JSONSerializer
- type Map
- type Option
- func WithDeleteRoomOnEmpty(enabled bool) Option
- func WithDispatchAsync(dispatch bool) Option
- func WithMessageBufferSize(size int) Option
- func WithPingPeriod(d time.Duration) Option
- func WithPongWait(d time.Duration) Option
- func WithProtocol(p Protocol) Option
- func WithWriteWait(d time.Duration) Option
- type ProtobufSerializer
- type Protocol
- type Room
- func (r *Room) Broadcast(msg []byte)
- func (r *Room) BroadcastAny(v any) error
- func (r *Room) BroadcastBinary(msg []byte)
- func (r *Room) BroadcastBinaryFilter(msg []byte, filter func(*Session) bool)
- func (r *Room) BroadcastBinaryOther(msg []byte, s *Session)
- func (r *Room) BroadcastFilter(msg []byte, filter func(*Session) bool)
- func (r *Room) BroadcastJSON(v Map) error
- func (r *Room) BroadcastJSONFilter(v Map, filter func(*Session) bool) error
- func (r *Room) BroadcastOther(msg []byte, s *Session)
- func (r *Room) CloseRoom()
- func (r *Room) HandleJoin(fn func(*Session))
- func (r *Room) HandleLeave(fn func(*Session))
- func (r *Room) Join(s *Session) error
- func (r *Room) Leave(s *Session)
- func (r *Room) Len() int
- func (r *Room) Name() string
- func (r *Room) OpenRoom()
- func (r *Room) Sessions() []*Session
- type Serializer
- type Session
- func (s *Session) Clear()
- func (s *Session) Close(msg ...[]byte) error
- func (s *Session) Decr(key string) (int64, error)
- func (s *Session) Get(key string) (any, error)
- func (s *Session) GetBool(key string) (bool, bool)
- func (s *Session) GetFloat(key string) (float64, bool)
- func (s *Session) GetInt(key string) (int, bool)
- func (s *Session) GetInt64(key string) (int64, bool)
- func (s *Session) GetString(key string) (string, bool)
- func (s *Session) Has(key string) bool
- func (s *Session) Incr(key string) (int64, error)
- func (s *Session) Keys() []string
- func (s *Session) LocalAddr() net.Addr
- func (s *Session) Protocol() string
- func (s *Session) RemoteAddr() net.Addr
- func (s *Session) Request() *http.Request
- func (s *Session) Room() *Room
- func (s *Session) Set(key string, value any)
- func (s *Session) SetNX(key string, value any) bool
- func (s *Session) UnsafeConn() net.Conn
- func (s *Session) Unset(key string)
- func (s *Session) Values() []any
- func (s *Session) Write(v any) error
- func (s *Session) WriteBinary(msg []byte) error
- func (s *Session) WriteBinaryWithDeadline(msg []byte, deadline time.Duration) error
- func (s *Session) WriteJSON(v Map) error
- func (s *Session) WriteJSONWithDeadline(message any, deadline time.Duration) error
- func (s *Session) WriteText(message []byte) error
- func (s *Session) WriteTextWithDeadline(message []byte, deadline time.Duration) error
Constants ¶
This section is empty.
Variables ¶
var ( ErrKeyNotFound = errors.New("key does not exist") ErrCoreClosed = errors.New("core is closed") ErrNilSession = errors.New("nil session") ErrAlreadyRegistered = errors.New("session is already registered") ErrWriteClosed = errors.New("write session is closed") ErrBufferFull = errors.New("buffer is full") ErrSessionClosed = errors.New("session is closed") ErrTypeAssertionFailed = errors.New("type assertion failed") ErrRoomClosed = errors.New("room is closed") ErrCoreExit = errors.New("cannot exit from core") ErrRoomNotFound = errors.New("room not found") ErrRoomNotEmpty = errors.New("room is not empty") ErrProtobufValue = errors.New("protobuf serializer: value must implement proto.message") ErrProtobufTarget = errors.New("protobuf serializer: target must implement proto.message") )
Functions ¶
This section is empty.
Types ¶
type Adler ¶
type Adler struct {
// Config holds runtime behavior knobs used by session read/write loops.
Config *Config
// contains filtered or unexported fields
}
Adler is the main websocket server orchestrator. It owns active sessions, rooms and all user-provided callbacks.
func New ¶
New initializes a ready-to-use Adler instance. Options override default configuration values in construction order.
func (*Adler) BroadcastAny ¶ added in v1.3.0
BroadcastAny marshals v using the configured serializer and broadcasts it to all connected sessions. The serializer determines whether the message is sent as JSON (text) or Protobuf (binary).
func (*Adler) BroadcastBinary ¶
BroadcastBinary sends a binary websocket message to all connected sessions.
func (*Adler) BroadcastBinaryFilter ¶
BroadcastBinaryFilter sends a binary websocket message to sessions matching fn.
func (*Adler) BroadcastFilter ¶
BroadcastFilter sends a text websocket message to sessions matching fn.
func (*Adler) BroadcastJSON ¶
BroadcastJSON marshals v and broadcasts it as a text websocket message.
func (*Adler) BroadcastJSONFilter ¶
BroadcastJSONFilter marshals v and broadcasts it as text to sessions matching fn.
func (*Adler) BroadcastOthers ¶
BroadcastOthers sends msg to all connected sessions except s.
func (*Adler) DeleteRoom ¶ added in v1.1.0
DeleteRoom removes a room by name when it has no active sessions.
func (*Adler) HandleConnect ¶
HandleConnect registers a callback triggered when a websocket session starts.
func (*Adler) HandleDisconnect ¶
HandleDisconnect registers a callback triggered when a websocket session ends.
func (*Adler) HandleError ¶
HandleError registers the session-level error handler.
func (*Adler) HandleMessage ¶
HandleMessage registers the text-message handler.
func (*Adler) HandleMessageBinary ¶
HandleMessageBinary registers the binary-message handler.
func (*Adler) HandlePong ¶
HandlePong registers a callback for pong events.
func (*Adler) HandleRequest ¶
HandleRequest upgrades the HTTP request to websocket and serves the session. The call blocks until the websocket session exits.
func (*Adler) HandleSentMessage ¶
HandleSentMessage registers a callback for sent text messages.
func (*Adler) HandleSentMessageBinary ¶
HandleSentMessageBinary registers a callback for sent binary messages.
func (*Adler) OnRoomJoin ¶
OnRoomJoin registers a callback triggered after a session joins a room.
func (*Adler) OnRoomLeave ¶
OnRoomLeave registers a callback triggered after a session leaves a room.
type Config ¶
type Config struct {
// WriteWait is the maximum duration allowed for a single outbound write.
// A zero value disables the per-write deadline.
// Typical values are between 5 and 30 seconds for internet clients.
WriteWait time.Duration
// PongWait is the maximum duration to wait for peer activity before the
// connection is considered stale by higher-level logic.
// A zero value disables the idle timeout.
// If enabled, common values are 30-120 seconds.
PongWait time.Duration
// PingPeriod defines how often server-side ping frames are sent.
// Keep it lower than PongWait when PongWait is enabled.
// A common ratio is PingPeriod = ~80-90% of PongWait.
PingPeriod time.Duration
// MessageBufferSize is the per-session outbound queue capacity.
// Start with 64-256 for chat-like traffic and increase only if you observe
// ErrBufferFull under normal expected bursts.
// Higher values increase per-session memory usage.
// When this value is 0, runtime falls back to a minimal queue.
MessageBufferSize int
// DispatchAsync controls inbound message dispatch mode.
// true dispatches each message on its own goroutine.
DispatchAsync bool
// DeleteRoomOnEmpty controls whether rooms are removed automatically
// when the last session leaves.
DeleteRoomOnEmpty bool
// Prefered protocol by server.
// This is the used protocol in matchmaker and in other structures of Adler.
Protocol Protocol
}
Config contains runtime tuning knobs for Adler sessions.
Use constructor options (With...) to override individual fields while keeping stable defaults for all other values.
type JSONSerializer ¶ added in v1.3.0
type JSONSerializer struct{}
JSONSerializer marshals messages as JSON text messages and unmarshals from JSON.
type Option ¶
type Option func(*Config)
Option mutates Config during construction.
func WithDeleteRoomOnEmpty ¶ added in v1.1.0
WithDeleteRoomOnEmpty controls automatic room removal when room size reaches 0.
func WithDispatchAsync ¶
WithDispatchAsync overrides Config.DispatchAsync.
func WithMessageBufferSize ¶
WithMessageBufferSize overrides Config.MessageBufferSize.
func WithPingPeriod ¶
WithPingPeriod overrides Config.PingPeriod. d is interpreted as seconds: WithPingPeriod(60) means 60s.
func WithPongWait ¶
WithPongWait overrides Config.PongWait. d is interpreted as seconds: WithPongWait(60) means 60s. Set 0 to disable idle disconnects.
func WithProtocol ¶ added in v1.3.0
WithProtocol sets the serialization protocol for the Adler instance. Use JSON for text-based messaging or Protobuf for binary protobuf messages.
func WithWriteWait ¶
WithWriteWait overrides Config.WriteWait. d is interpreted as seconds: WithWriteWait(10) means 10s. Do not pass time.Second-multiplied values here.
type ProtobufSerializer ¶ added in v1.3.0
type ProtobufSerializer struct{}
ProtobufSerializer marshals messages as binary protobuf messages and unmarshals from protobuf.
type Protocol ¶ added in v1.3.0
type Protocol int
Protocol defines the serialization format for messages between server and clients.
type Room ¶
type Room struct {
// contains filtered or unexported fields
}
Room groups sessions under the same logical channel.
func (*Room) BroadcastAny ¶ added in v1.3.0
BroadcastAny marshals v using the configured serializer and broadcasts it to all room sessions. The serializer determines whether the message is sent as JSON (text) or Protobuf (binary).
func (*Room) BroadcastBinary ¶
BroadcastBinary sends a binary websocket message to all room sessions.
func (*Room) BroadcastBinaryFilter ¶ added in v1.4.1
BroadcastBinaryFilter sends a binary websocket message to room sessions matching filter.
func (*Room) BroadcastBinaryOther ¶ added in v1.4.1
BroadcastBinaryOther sends a binary websocket message to all room sessions except for s.
func (*Room) BroadcastFilter ¶
BroadcastFilter sends a text websocket message to room sessions matching filter.
func (*Room) BroadcastJSON ¶
BroadcastJSON marshals v and sends it as a text websocket message.
func (*Room) BroadcastJSONFilter ¶
BroadcastJSONFilter marshals v and sends it as text to sessions matching filter.
func (*Room) BroadcastOther ¶ added in v1.4.1
BroadcastOther sends a text websocket message to all room sessions excluding for s.
func (*Room) CloseRoom ¶
func (r *Room) CloseRoom()
CloseRoom marks the room as closed for new Join calls.
func (*Room) HandleJoin ¶
HandleJoin registers a callback triggered after a session joins the room.
func (*Room) HandleLeave ¶
HandleLeave registers a callback triggered after a session leaves the room.
type Serializer ¶ added in v1.3.0
type Serializer interface {
// Marshal converts a value to bytes and returns the appropriate WebSocket opcode.
Marshal(v any) ([]byte, ws.OpCode, error)
// Unmarshal decodes bytes into a value.
Unmarshal(data []byte, v any) error
}
Serializer defines the interface for marshaling and unmarshaling messages in different formats (JSON, Protobuf, etc.) for WebSocket transmission.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session represents a single WebSocket client connection.
func (*Session) Clear ¶
func (s *Session) Clear()
Clear removes all key-value pairs from the session store.
func (*Session) LocalAddr ¶
LocalAddr returns the local network address of the websocket connection.
func (*Session) RemoteAddr ¶
RemoteAddr returns the remote network address of the websocket connection.
func (*Session) SetNX ¶
SetNX sets key only when it does not already exist. It returns true when the value was written.
func (*Session) UnsafeConn ¶
UnsafeConn exposes the underlying websocket network connection. Callers must coordinate concurrent reads/writes to avoid races.
func (*Session) Write ¶ added in v1.3.0
Write marshals v using the configured serializer and queues it as a message. The serializer determines whether the message is sent as JSON (text) or Protobuf (binary).
func (*Session) WriteBinary ¶
WriteBinary queues a binary message to be sent to the client.
func (*Session) WriteBinaryWithDeadline ¶
WriteBinaryWithDeadline queues a binary message with a custom write deadline.
func (*Session) WriteJSONWithDeadline ¶
WriteJSONWithDeadline queues a JSON payload with a custom write deadline.