...

Package user

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

Overview ▾

Index ▾

func CopyUser(user *User) map[string]interface{}
func DiscardUser(reliableClientAddr string, sid string)
func GetCCU() int
func New(data map[string]interface{}, initTTL int64) (map[string]interface{}, error)
func OnDiscard(callback func(string, *User))
func OnNew(callback func(*User))
func OnNewConnection(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 CreateBlankUser(sid, uid string) *User
    func GetUserByID(sid string) *User
    func (user *User) Disconnect()
    func (user *User) Get(key string) interface{}
    func (u *User) GetAsBool(k string) bool
    func (u *User) GetAsBytes(k string) []byte
    func (u *User) GetAsFloat64(k string) float64
    func (u *User) GetAsInt16(k string) int16
    func (u *User) GetAsInt32(k string) int32
    func (u *User) GetAsInt64(k string) int64
    func (u *User) GetAsInt8(k string) int8
    func (u *User) GetAsString(k string) string
    func (u *User) GetAsUint16(k string) uint16
    func (u *User) GetAsUint32(k string) uint32
    func (u *User) GetAsUint64(k string) uint64
    func (u *User) GetAsUint8(k string) uint8
    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 (u *User) SetAsBool(k string, v bool) bool
    func (u *User) SetAsBytes(k string, v []byte) bool
    func (u *User) SetAsFloat64(k string, v float64) bool
    func (u *User) SetAsInt16(k string, v int16) bool
    func (u *User) SetAsInt32(k string, v int32) bool
    func (u *User) SetAsInt64(k string, v int64) bool
    func (u *User) SetAsInt8(k string, v int8) bool
    func (u *User) SetAsString(k string, v string) bool
    func (u *User) SetAsUint16(k string, v uint16) bool
    func (u *User) SetAsUint32(k string, v uint32) bool
    func (u *User) SetAsUint64(k string, v uint64) bool
    func (u *User) SetAsUint8(k string, v uint8) bool
    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{})

func CopyUser

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

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

userData *User - User to be copied as a map[string]interface{}.

func DiscardUser

func DiscardUser(reliableClientAddr string, sid string)

DiscardUser Deletes a user data. Either reliableClientAddr or SID to be used.

[NOTE] Uses mutex lock internally.

NOTE: Newer version of the clients use SID instead of the client address.

reliableClientAddr string - Client address of the user to be discarded.

sid string - User SID of the user to be discarded.

func GetCCU

func GetCCU() int

GetCCU returns the number of concurrent users of the node

[NOTE] Uses mutex lock internally..

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

[NOTE] Uses mutex lock internally.

if initTTL is greater than 0, it will be used as the initial TTL (in seconds) if initTTL is 0 or less, config TTL 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

[IMPORTANT] This function is not goroutine safe and it must not be used in runtime. assign callbacks on the start of your process only.

callback func(SID string, userData *User) - Callback to be invoked when the target user is deleted.

func OnNew

func OnNew(callback func(*User))

OnNew Registers a callback on user.New()

[IMPORTANT] This function is not goroutine safe and it must not be used in runtime. assign callbacks on the start of your process only.

callback func(newUserData *User) - Callback to be invoked when a new user is created.

func OnNewConnection

func OnNewConnection(callback func(*User))

OnNewConnection assigns a callback on new user connection

[IMPORTANT] This function is not goroutine safe and it must not be used in runtime. assign callbacks on the start of your process only.

callback func(newUserData *User) - Callback to be invoked when a new user is created.

func SetTCPState

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

SetTCPState [INTERNAL USE ONLY] Sets TCP user state to the user data and updates TTL

[NOTE] Uses mutex lock internally.

sid string - User SID of the target user to updated.

state *tcp.State - TCP state to be assigned to the user.

func SetUDPStateAndRUDPConn

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

SetUDPStateAndRUDPConn [INTERNAL USE ONLY] Sets UDP user state to the user data and updates TTL

[NOTE] Uses mutex lock internally.

sid string - User SID of the target user to updated.

state *udp.State - UDP state to be assigned to the user.

func SetUserByID

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

SetUserByID Sets a user data by sid

[NOTE] Uses mutex lock internally.

sid string - User SID to store in the server memory.

userData *User - User to be stored.

ttl int64 - TTL of the stored user in seconds. This will be extended by using Extend etc.

func SetWSState

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

SetWSState [INTERNAL USE ONLY] Sets WebSocket state to the user data and updates TTL

[NOTE] Uses mutex lock internally.

sid string - User SID of the target user to updated.

state *ws.State - WebSocket state to be assigned to the user.

func SetupAsConnector

func SetupAsConnector(ttl int64)

SetupAsConnector Sets up as Connector server

ttl int64 - TTL of the health check in seconds.

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

[NOTE] Uses mutex lock internally.

sid string - User SID of the target user to updated.

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
    // User client local address list byte array from UDP State
    UDPClientLocalAddrBytes []byte
    // 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 CreateBlankUser

func CreateBlankUser(sid, uid string) *User

CreateBlankUser creates an incomplete user to be used as a dummy

Usage Example: Create a dummy user to create an empty room and/or group without actual client.

sid string - Blank user SID.

uid string - Blank user ID.

func GetUserByID

func GetUserByID(sid string) *User

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

[NOTE] Uses mutex lock internally.

sid string - User SID of the target user.

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 [Deprecated] returns a value associated with the key (SID) given

[NOTE] Uses mutex lock internally.

 User data must be handled by the user and nobody else, so no race condition...
 [IMPORTANT] When you get a byte array, the value may become base64 string (This happens when user moves from one node to another)
 [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)
 }

	key string - User property key of the value to retrieve.

func (*User) GetAsBool

func (u *User) GetAsBool(k string) bool

GetAsBool returns user data by its key as a bool.

[IMPORTANT] Returns false if the value does not exist or invalid.

func (*User) GetAsBytes

func (u *User) GetAsBytes(k string) []byte

GetAsBytes returns user data by its key as a string.

func (*User) GetAsFloat64

func (u *User) GetAsFloat64(k string) float64

GetAsFloat64 returns user data by its key as a float64.

[IMPORTANT] Returns 0 if the value is invalid or not found.

func (*User) GetAsInt16

func (u *User) GetAsInt16(k string) int16

GetAsInt16 returns user data by its key as a int16.

[IMPORTANT] Returns 0 if the value is invalid or not found.

func (*User) GetAsInt32

func (u *User) GetAsInt32(k string) int32

GetAsInt32 returns user data by its key as a int32.

[IMPORTANT] Returns 0 if the value is invalid or not found.

func (*User) GetAsInt64

func (u *User) GetAsInt64(k string) int64

GetAsInt64 returns user data by its key as a int64.

[IMPORTANT] Returns 0 if the value is invalid or not found.

func (*User) GetAsInt8

func (u *User) GetAsInt8(k string) int8

GetAsInt8 returns user data by its key as a int8.

[IMPORTANT] Returns 0 if the value is invalid or not found.

func (*User) GetAsString

func (u *User) GetAsString(k string) string

GetAsString returns user data by its key as a string.

[IMPORATNT] Returns an empty string if the value does not exist or invalid.

func (*User) GetAsUint16

func (u *User) GetAsUint16(k string) uint16

GetAsUint16 returns user data by its key as a uint16.

[IMPORTANT] Returns 0 if the value is invalid or not found.

func (*User) GetAsUint32

func (u *User) GetAsUint32(k string) uint32

GetAsUint32 returns user data by its key as a uint32.

[IMPORTANT] Returns 0 if the value is invalid or not found.

func (*User) GetAsUint64

func (u *User) GetAsUint64(k string) uint64

GetAsUint64 returns user data by its key as a uint32.

[IMPORTANT] Returns 0 if the value is invalid or not found.

func (*User) GetAsUint8

func (u *User) GetAsUint8(k string) uint8

GetAsUint8 returns user data by its key as a uint8.

[IMPORTANT] Returns 0 if the value is invalid or not found.

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]

callback func() - Callback to be invoked when a new user connection is establied with the client.

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 [Deprecated] 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

ver uint8 - Server push message command version.

cmd uint16 - Server push message command ID.

message []byte - Message byte array to be sent as a push.

reliable bool - If true, UDP will become RUDP.

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

message []byte - Message byte array to be sent as a response.

ver uint8 - Server response command version.

cmd uint16 - Server response command ID.

status uint8 - Server response status. Status consts are avialble in server package.
server.Ok (success), server.Bad (error caused by the user), server.Err (error caused by the server).

reliable bool - If true, UDP will become RUDP.

func (*User) Set

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

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

[NOTE] Uses mutex lock internally.

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

	key string - Key of the user property to be set.

	value interface{} - Value of the user property to be set.

func (*User) SetAsBool

func (u *User) SetAsBool(k string, v bool) bool

SetAsBool sets user data by a key given as a bool.

func (*User) SetAsBytes

func (u *User) SetAsBytes(k string, v []byte) bool

SetAsBytes sets user data by a key given as a byte array.

func (*User) SetAsFloat64

func (u *User) SetAsFloat64(k string, v float64) bool

SetAsFloat64 sets user data by a key given as a float64.

func (*User) SetAsInt16

func (u *User) SetAsInt16(k string, v int16) bool

SetAsInt16 sets user data by a key given as a int16.

func (*User) SetAsInt32

func (u *User) SetAsInt32(k string, v int32) bool

SetAsInt32 sets user data by a key given as a int32.

func (*User) SetAsInt64

func (u *User) SetAsInt64(k string, v int64) bool

SetAsInt64 sets user data by a key given as a int64.

func (*User) SetAsInt8

func (u *User) SetAsInt8(k string, v int8) bool

SetAsInt8 sets user data by a key given as a int8.

func (*User) SetAsString

func (u *User) SetAsString(k string, v string) bool

SetAsString sets user data by a key given as a string.

func (*User) SetAsUint16

func (u *User) SetAsUint16(k string, v uint16) bool

SetAsUint16 sets user data by a key given as a uint16.

func (*User) SetAsUint32

func (u *User) SetAsUint32(k string, v uint32) bool

SetAsUint32 sets user data by a key given as a uint32.

func (*User) SetAsUint64

func (u *User) SetAsUint64(k string, v uint64) bool

SetAsUint64 sets user data by a key given as a uint64.

func (*User) SetAsUint8

func (u *User) SetAsUint8(k string, v uint8) bool

SetAsUint8 sets user data by a key given as a uint8.

func (*User) ThrottledPush

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

ThrottledPush [Deprecated] use ThrottledServerPush instead

[NOTE] Uses mutex lock internally.

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.

[NOTE] Uses mutex lock internally.

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

[NOTE] Uses mutex lock internally.

 [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{}

	key string - Key of the user property to be updated.

	op func(targetPropertyValue interface{}) - Operation function to handle the property update.

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.

ver float64 - Server message command version.

cmd float64 - Server message command ID.

msg map[string]interface{} - Server message data to be sent.