session

package
v2.6.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Kind

type Kind int

Kind is the kind of a session.

const (
	Conn Kind = iota + 1 // Conn: a connection session
	User                 // User: a user session
)

The kinds of a session.

func (Kind) String

func (k Kind) String() string

String returns the name of the session kind.

type Session

type Session struct {
	// contains filtered or unexported fields
}

Session manages the connection and user sessions of a node along with their channel subscriptions.

func NewSession

func NewSession() *Session

NewSession creates a session instance.

func (*Session) AddConn

func (s *Session) AddConn(conn network.Conn)

AddConn adds conn to the connection sessions.

func (*Session) Bind

func (s *Session) Bind(cid, uid int64) error

Bind binds the user ID uid to the connection identified by cid. When uid is already bound to another connection, that connection is closed and replaced.

func (*Session) Broadcast

func (s *Session) Broadcast(kind Kind, disconnect bool, buf buffer.Buffer) (int64, error)

Broadcast pushes buf to every session of the given kind asynchronously. When disconnect is true, each connection is closed after the push. It returns the number of sessions the message was pushed to, or errors.ErrInvalidSessionKind for an unknown kind.

func (*Session) Close

func (s *Session) Close(kind Kind, target int64, force ...bool) error

Close closes the session identified by target. The kind selects whether target is a connection ID or a user ID. Pass force as true to close the connection forcibly.

func (*Session) Has

func (s *Session) Has(kind Kind, target int64) (ok bool, err error)

Has reports whether the session identified by target exists. The kind selects whether target is a connection ID or a user ID. It reports errors.ErrInvalidSessionKind for an unknown kind.

func (*Session) LocalAddr

func (s *Session) LocalAddr(kind Kind, target int64) (net.Addr, error)

LocalAddr returns the local address of the session identified by target. The kind selects whether target is a connection ID or a user ID.

func (*Session) LocalIP

func (s *Session) LocalIP(kind Kind, target int64) (string, error)

LocalIP returns the local IP address of the session identified by target. The kind selects whether target is a connection ID or a user ID.

func (*Session) Multicast

func (s *Session) Multicast(kind Kind, targets []int64, disconnect bool, buf buffer.Buffer) (int64, error)

Multicast pushes buf to every session in targets asynchronously. The kind selects whether each target is a connection ID or a user ID. When disconnect is true, each connection is closed after the push. It returns the number of sessions the message was pushed to, or errors.ErrInvalidSessionKind for an unknown kind.

func (*Session) Publish added in v2.3.0

func (s *Session) Publish(channel string, disconnect bool, buf buffer.Buffer) (int64, error)

Publish pushes buf to every session subscribed to channel asynchronously. When disconnect is true, each connection is closed after the push. It returns the number of sessions the message was pushed to.

func (*Session) Push

func (s *Session) Push(kind Kind, target int64, disconnect bool, buf buffer.Buffer) error

Push pushes buf to the session identified by target asynchronously. The kind selects whether target is a connection ID or a user ID. When disconnect is true, the connection is closed after the push.

func (*Session) RemConn

func (s *Session) RemConn(conn network.Conn) bool

RemConn removes conn from the connection sessions. It reports whether conn was registered and has been removed.

The existence check and the removal happen under the same write lock, so the caller does not need to call Session.Has before RemConn and pay for a second lookup. When the same conn is removed concurrently, only the first call reports true, which keeps repeated removals from unbalancing the counters.

func (*Session) RemoteAddr

func (s *Session) RemoteAddr(kind Kind, target int64) (net.Addr, error)

RemoteAddr returns the remote address of the session identified by target. The kind selects whether target is a connection ID or a user ID.

func (*Session) RemoteIP

func (s *Session) RemoteIP(kind Kind, target int64) (string, error)

RemoteIP returns the remote IP address of the session identified by target. The kind selects whether target is a connection ID or a user ID.

func (*Session) Stat

func (s *Session) Stat(kind Kind) (int64, error)

Stat returns the number of sessions of the given kind, or errors.ErrInvalidSessionKind for an unknown kind.

func (*Session) Subscribe added in v2.3.0

func (s *Session) Subscribe(kind Kind, targets []int64, channel string) (err error)

Subscribe subscribes the sessions in targets to channel. The kind selects whether each target is a connection ID or a user ID. It reports errors.ErrInvalidSessionKind for an unknown kind.

func (*Session) Unbind

func (s *Session) Unbind(uid int64) (int64, error)

Unbind unbinds the user ID uid and returns the ID of the connection it was bound to.

func (*Session) Unsubscribe added in v2.3.0

func (s *Session) Unsubscribe(kind Kind, targets []int64, channel string) (err error)

Unsubscribe unsubscribes the sessions in targets from channel. The kind selects whether each target is a connection ID or a user ID. It reports errors.ErrInvalidSessionKind for an unknown kind.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL