session

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2021 License: MIT Imports: 12 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// SessionCloseCallbacks contains global session close callbacks
	SessionCloseCallbacks = make([]func(s *Session), 0)

	// SessionCount keeps the current number of sessions
	SessionCount int64
)

Functions

func CloseAll

func CloseAll()

CloseAll calls Close on all sessions

func OnAfterSessionBind

func OnAfterSessionBind(f func(ctx context.Context, s *Session) error)

OnAfterSessionBind adds a method to be called when session is bound and after all sessionBind callbacks

func OnSessionBind

func OnSessionBind(f func(ctx context.Context, s *Session) error)

OnSessionBind adds a method to be called when a session is bound same function cannot be added twice!

func OnSessionClose

func OnSessionClose(f func(s *Session))

OnSessionClose adds a method that will be called when every session closes

func SetCustomEncodeDecode

func SetCustomEncodeDecode(encoder DataEncoder, decoder DataDecoder)

SetCustomEncodeDecode 设置自定义的session data encoder和decoder

默认为 json.Marshal 和 json.Unmarshal

Types

type DataDecoder

type DataDecoder func(bytes []byte, data interface{}) error

DataDecoder 自定义session data encoder

type DataEncoder

type DataEncoder func(data interface{}) ([]byte, error)

DataEncoder 自定义session data encoder

type HandshakeClientData

type HandshakeClientData struct {
	Platform    string `json:"platform"`
	LibVersion  string `json:"libVersion"`
	BuildNumber string `json:"clientBuildNumber"`
	Version     string `json:"clientVersion"`
}

HandshakeClientData represents information about the client sent on the handshake.

type HandshakeData

type HandshakeData struct {
	Sys  HandshakeClientData    `json:"sys"`
	User map[string]interface{} `json:"user,omitempty"`
}

HandshakeData represents information about the handshake sent by the client. `sys` corresponds to information independent from the app and `user` information that depends on the app and is customized by the user.

type NetworkEntity

type NetworkEntity interface {
	Push(route string, v interface{}) error
	ResponseMID(ctx context.Context, mid uint, v interface{}, isError ...bool) error
	Close() error
	Kick(ctx context.Context) error
	RemoteAddr() net.Addr
	SendRequest(ctx context.Context, entityID, entityType string, serverID, route string, v interface{}) (*protos.Response, error)
}

NetworkEntity represent low-level network instance

type Session

type Session struct {
	sync.RWMutex // protect data

	OnCloseCallbacks []func()             //onClose callbacks
	Subscriptions    []*nats.Subscription // subscription created on bind when using nats rpc server
	// contains filtered or unexported fields
}

Session represents a client session, which can store data during the connection. All data is released when the low-level connection is broken. Session instance related to the client will be passed to Handler method in the context parameter.

func GetSessionByID

func GetSessionByID(id int64) *Session

GetSessionByID return a session bound to a frontend server id

func GetSessionByUID

func GetSessionByUID(uid string) *Session

GetSessionByUID return a session bound to an user id

func New

func New(entity NetworkEntity, UID ...string) *Session

New returns a new session instance a NetworkEntity is a low-level network instance

func (*Session) BackgroundServerID added in v0.1.0

func (s *Session) BackgroundServerID() string

func (*Session) Bind

func (s *Session) Bind(ctx context.Context, uid string) error

Bind bind UID to current session

func (*Session) Clear

func (s *Session) Clear()

Clear releases all data related to current session

func (*Session) Close

func (s *Session) Close()

Close terminates current session, session related data will not be released, all related data should be cleared explicitly in Session closed callback

func (*Session) DebugString added in v0.0.31

func (s *Session) DebugString() string

func (*Session) GetHandshakeData

func (s *Session) GetHandshakeData() *HandshakeData

GetHandshakeData gets the handshake data received by the client.

func (*Session) ID

func (s *Session) ID() int64

ID returns the session id

func (*Session) Kick

func (s *Session) Kick(ctx context.Context) error

Kick kicks the user

func (*Session) OnClose

func (s *Session) OnClose(c func())

OnClose adds the function it receives to the callbacks that will be called when the session is closed

func (*Session) Owner added in v0.1.0

func (s *Session) Owner() (ownerID string, ownerType string)

func (*Session) OwnerEntityID added in v0.1.0

func (s *Session) OwnerEntityID() string

func (*Session) OwnerEntityType added in v0.1.0

func (s *Session) OwnerEntityType() string

func (*Session) Push

func (s *Session) Push(route string, v interface{}) error

Push message to client

func (*Session) RemoteAddr

func (s *Session) RemoteAddr() net.Addr

RemoteAddr returns the remote network address.

func (*Session) ResponseMID

func (s *Session) ResponseMID(ctx context.Context, mid uint, v interface{}, err ...bool) error

ResponseMID responses message to client, mid is request message ID

func (*Session) SetHandshakeData

func (s *Session) SetHandshakeData(data *HandshakeData)

SetHandshakeData sets the handshake data received by the client.

func (*Session) SwitchOwner added in v0.1.0

func (s *Session) SwitchOwner(id string, typ string, backgroundServerID string)

func (*Session) UID

func (s *Session) UID() string

UID returns uid that bind to current session

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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