session

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2019 License: MIT Imports: 12 Imported by: 2

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 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

Types

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, 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
	IsFrontend       bool     // if session is a frontend session

	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, frontend bool, UID ...string) *Session

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

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) Float32

func (s *Session) Float32(key string) float32

Float32 returns the value associated with the key as a float32.

func (*Session) Float64

func (s *Session) Float64(key string) float64

Float64 returns the value associated with the key as a float64.

func (*Session) Get

func (s *Session) Get(key string) interface{}

Get returns a key value

func (*Session) GetData

func (s *Session) GetData() map[string]interface{}

GetData gets the data

func (*Session) GetDataEncoded

func (s *Session) GetDataEncoded() []byte

GetDataEncoded returns the session data as an encoded value

func (*Session) GetHandshakeData

func (s *Session) GetHandshakeData() *HandshakeData

GetHandshakeData gets the handshake data received by the client.

func (*Session) HasKey

func (s *Session) HasKey(key string) bool

HasKey decides whether a key has associated value

func (*Session) ID

func (s *Session) ID() int64

ID returns the session id

func (*Session) Int

func (s *Session) Int(key string) int

Int returns the value associated with the key as a int.

func (*Session) Int16

func (s *Session) Int16(key string) int16

Int16 returns the value associated with the key as a int16.

func (*Session) Int32

func (s *Session) Int32(key string) int32

Int32 returns the value associated with the key as a int32.

func (*Session) Int64

func (s *Session) Int64(key string) int64

Int64 returns the value associated with the key as a int64.

func (*Session) Int8

func (s *Session) Int8(key string) int8

Int8 returns the value associated with the key as a int8.

func (*Session) Kick

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

Kick kicks the user

func (*Session) OnClose

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

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

func (*Session) Push

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

Push message to client

func (*Session) PushToFront

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

PushToFront updates the session in the frontend

func (*Session) RemoteAddr

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

RemoteAddr returns the remote network address.

func (*Session) Remove

func (s *Session) Remove(key string) error

Remove delete data associated with the key from session storage

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) Set

func (s *Session) Set(key string, value interface{}) error

Set associates value with the key in session storage

func (*Session) SetData

func (s *Session) SetData(data map[string]interface{}) error

SetData sets the whole session data

func (*Session) SetDataEncoded

func (s *Session) SetDataEncoded(encodedData []byte) error

SetDataEncoded sets the whole session data from an encoded value

func (*Session) SetFrontendData

func (s *Session) SetFrontendData(frontendID string, frontendSessionID int64)

SetFrontendData sets frontend id and session id

func (*Session) SetHandshakeData

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

SetHandshakeData sets the handshake data received by the client.

func (*Session) String

func (s *Session) String(key string) string

String returns the value associated with the key as a string.

func (*Session) UID

func (s *Session) UID() string

UID returns uid that bind to current session

func (*Session) Uint

func (s *Session) Uint(key string) uint

Uint returns the value associated with the key as a uint.

func (*Session) Uint16

func (s *Session) Uint16(key string) uint16

Uint16 returns the value associated with the key as a uint16.

func (*Session) Uint32

func (s *Session) Uint32(key string) uint32

Uint32 returns the value associated with the key as a uint32.

func (*Session) Uint64

func (s *Session) Uint64(key string) uint64

Uint64 returns the value associated with the key as a uint64.

func (*Session) Uint8

func (s *Session) Uint8(key string) uint8

Uint8 returns the value associated with the key as a uint8.

func (*Session) Value

func (s *Session) Value(key string) interface{}

Value returns the value associated with the key as a interface{}.

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