Documentation ¶
Index ¶
- Variables
- func CloseAll()
- func OnAfterSessionBind(f func(ctx context.Context, s *Session) error)
- func OnSessionBind(f func(ctx context.Context, s *Session) error)
- func OnSessionClose(f func(s *Session))
- func SetCustomEncodeDecode(encoder DataEncoder, decoder DataDecoder)
- type DataDecoder
- type DataEncoder
- type HandshakeClientData
- type HandshakeData
- type NetworkEntity
- type Session
- func (s *Session) BackgroundServerID() string
- func (s *Session) Bind(ctx context.Context, uid string) error
- func (s *Session) Clear()
- func (s *Session) Close()
- func (s *Session) DebugString() string
- func (s *Session) GetHandshakeData() *HandshakeData
- func (s *Session) ID() int64
- func (s *Session) Kick(ctx context.Context) error
- func (s *Session) OnClose(c func())
- func (s *Session) Owner() (ownerID string, ownerType string)
- func (s *Session) OwnerEntityID() string
- func (s *Session) OwnerEntityType() string
- func (s *Session) Push(route string, v interface{}) error
- func (s *Session) RemoteAddr() net.Addr
- func (s *Session) ResponseMID(ctx context.Context, mid uint, v interface{}, err ...bool) error
- func (s *Session) SetHandshakeData(data *HandshakeData)
- func (s *Session) SwitchOwner(id string, typ string, backgroundServerID string)
- func (s *Session) UID() string
Constants ¶
This section is empty.
Variables ¶
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 ¶
OnAfterSessionBind adds a method to be called when session is bound and after all sessionBind callbacks
func OnSessionBind ¶
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 ¶
DataDecoder 自定义session data encoder
type DataEncoder ¶
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 ¶
GetSessionByID return a session bound to a frontend server id
func GetSessionByUID ¶
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 (*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 (*Session) GetHandshakeData ¶
func (s *Session) GetHandshakeData() *HandshakeData
GetHandshakeData gets the handshake data received by the client.
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) OwnerEntityID ¶ added in v0.1.0
func (*Session) OwnerEntityType ¶ added in v0.1.0
func (*Session) RemoteAddr ¶
RemoteAddr returns the remote network address.
func (*Session) ResponseMID ¶
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.