...

Package user

import "github.com/Diarkis/diarkis/user"
Overview
Index

Overview ▾

Index ▾

Constants
func CopyUser(user *User) map[string]interface{}
func DiscardUser(reliableClientAddr string, sid string)
func New(data map[string]interface{}, initTTL int64) (map[string]interface{}, error)
func OnDiscard(callback func(string, *User))
func OnNew(callback func(*User))
func SetTCPState(sid string, state *tcp.State)
func SetUDPStateAndRUDPConn(sid string, state *udp.State)
func SetUserByID(sid string, user *User, ttl int64) bool
func SetWSState(sid string, state *ws.State)
func SetupAsConnector(ttl int64)
func SetupAsTCPServer()
func SetupAsUDPServer()
func SetupAsWebSocketServer()
func UpdateUserByID(sid string) bool
type User
    func GetUserByID(sid string) *User
    func (user *User) Disconnect()
    func (user *User) Get(key string) interface{}
    func (user *User) GetClientAddr() string
    func (user *User) GetClientKey() string
    func (user *User) OnNewConnection(callback func())
    func (user *User) Push(message []byte, reliable bool)
    func (user *User) Respond(message []byte, status uint8, reliable bool)
    func (user *User) Send(ver uint8, cmd uint16, message []byte, status uint8, reliable bool)
    func (user *User) ServerPush(ver uint8, cmd uint16, message []byte, reliable bool)
    func (user *User) ServerRespond(message []byte, ver uint8, cmd uint16, status uint8, reliable bool)
    func (user *User) Set(key string, value interface{})
    func (user *User) ThrottledPush(throttleTime int64, msg []byte, reliable bool)
    func (user *User) ThrottledServerPush(throttleTime int64, ver uint8, cmd uint16, msg []byte, reliable bool)
    func (user *User) Update(key string, op func(data interface{}))
    func (user *User) WSMessage(ver float64, cmd float64, msg map[string]interface{})

Constants

MeshValueName name of mesh value

const MeshValueName = "uc"

VaultName vault name for users

const VaultName = "Users"

func CopyUser

func CopyUser(user *User) map[string]interface{}

CopyUser Copies a user struct for transferring user client data to another node

func DiscardUser

func DiscardUser(reliableClientAddr string, sid string)

DiscardUser Deletes a user data

func New

func New(data map[string]interface{}, initTTL int64) (map[string]interface{}, error)

New [INTERNAL USE ONLY] handle mesh network command sent from HTTP if initTTL is greater than 0, it will be used as the initial TTL (in seconds) if initTTL is 0 or less, config TTL * 3 will be used as the initial TTL (in seconds)

func OnDiscard

func OnDiscard(callback func(string, *User))

OnDiscard Registers a callback on user data deletion

func OnNew

func OnNew(callback func(*User))

OnNew Registers a callback on user.New()

func SetTCPState

func SetTCPState(sid string, state *tcp.State)

SetTCPState Sets TCP user state to the user data and updates TTL

func SetUDPStateAndRUDPConn

func SetUDPStateAndRUDPConn(sid string, state *udp.State)

SetUDPStateAndRUDPConn Sets UDP user state to the user data and updates TTL

func SetUserByID

func SetUserByID(sid string, user *User, ttl int64) bool

SetUserByID Sets a user data by sid

func SetWSState

func SetWSState(sid string, state *ws.State)

SetWSState Sets WebSocket state to the user data and updates TTL

func SetupAsConnector

func SetupAsConnector(ttl int64)

SetupAsConnector Sets up as Connector server

func SetupAsTCPServer

func SetupAsTCPServer()

SetupAsTCPServer Sets up as TCP server

func SetupAsUDPServer

func SetupAsUDPServer()

SetupAsUDPServer Sets up as UDP server

func SetupAsWebSocketServer

func SetupAsWebSocketServer()

SetupAsWebSocketServer Sets up as TCP server

func UpdateUserByID

func UpdateUserByID(sid string) bool

UpdateUserByID Updates TTL of user data in the vault

type User

User User data structure

type User struct {
    // unique ID to udentify the user by
    ID  string
    SID string

    SIDBytes []byte
    // key value storage - use user.Get() and user.Set() to access this map or should ONLY be used with Update()
    Data    map[string]interface{}
    Payload []byte
    // used only by WebSocket as Payload
    WSData   map[string]interface{}
    WSState  *ws.State
    TCPState *tcp.State
    UDPState *udp.State
    RUDPConn *udp.Connection
    // UDPState is always a new one..
    UDPClientKey string
    // encryption keys
    EncryptionKey    []byte
    EncryptionIV     []byte
    EncryptionMacKey []byte
    // reconnect flag
    Reconnect bool
    // contains filtered or unexported fields
}

func GetUserByID

func GetUserByID(sid string) *User

GetUserByID Returns a user by its SID - intended to be used by UDP server

func (*User) Disconnect

func (user *User) Disconnect()

Disconnect disconnects the client from the server

func (*User) Get

func (user *User) Get(key string) interface{}

Get returns a value associated with the key (SID) given

User data must be handled by the user and nobody else, so no race condition...
[IMPORTANT] value does NOT support struct. If you need to store a a structured data, please use map instead
[IMPORTANT] If you store complex data types such as maps etc... all value type will be interface{}
[IMPORTANT] If the value is numeric, the data type may become float64:
numInterface := userData.Get("num") // this maybe float64
var num int
if numInterface == nil {
  num = 0 // Get() can return a nil
}
if _, ok := numInterface.(float64); ok {
  num = int(numInterface.(float64)) // cast it to be int
} else {
  num = numInterface.(int)
}

func (*User) GetClientAddr

func (user *User) GetClientAddr() string

GetClientAddr returns the user client address

func (*User) GetClientKey

func (user *User) GetClientKey() string

GetClientKey returns clientKey

func (*User) OnNewConnection

func (user *User) OnNewConnection(callback func())

OnNewConnection [INTERNAL USE ONLY]

func (*User) Push

func (user *User) Push(message []byte, reliable bool)

Push [Deprecated] use ServerPush instead

func (*User) Respond

func (user *User) Respond(message []byte, status uint8, reliable bool)

Respond [Deprecated] use ServerRespond instead

func (*User) Send

func (user *User) Send(ver uint8, cmd uint16, message []byte, status uint8, reliable bool)

Send Sends a packet from the server to the target user client with a response status

func (*User) ServerPush

func (user *User) ServerPush(ver uint8, cmd uint16, message []byte, reliable bool)

ServerPush Sends a push packet from the server to the target user client

func (*User) ServerRespond

func (user *User) ServerRespond(message []byte, ver uint8, cmd uint16, status uint8, reliable bool)

ServerRespond sends a packet as a response w/ ver and cmd of your choice

func (*User) Set

func (user *User) Set(key string, value interface{})

Set stores a value along with the key (SID): the value will remain stored until user object is discarded

User data must be handled by the user and nobody else, so no race condition....
[IMPORTANT] value does NOT support struct. If you need to store a a structured data, please use map instead
[IMPORTANT] If you store complex data types such as maps etc... all value type will be interface{}

func (*User) ThrottledPush

func (user *User) ThrottledPush(throttleTime int64, msg []byte, reliable bool)

ThrottledPush [Deprecated] use ThrottledServerPush instead

func (*User) ThrottledServerPush

func (user *User) ThrottledServerPush(throttleTime int64, ver uint8, cmd uint16, msg []byte, reliable bool)

ThrottledServerPush sends a server push packet if the packet sent previously has past throttleTime previously set.

If not, the packet will be "Dropped".
throttleTime is in milliseconds.

func (*User) Update

func (user *User) Update(key string, op func(data interface{}))

Update allows a custom operation function to "update" user data by its key

[IMPORTANT] value does NOT support struct. If you need to store a a structured data, please use map instead
[IMPORTANT] If you store complex data types such as maps etc... all value type will be interface{}

func (*User) WSMessage

func (user *User) WSMessage(ver float64, cmd float64, msg map[string]interface{})

WSMessage sends a WebSocket message to user client. Only available for WebSocket server.