Bad Send status for server response.
const Bad = 4
Err Send status for server response.
const Err = 5
Ok Send status for server response.
const Ok = 1
TCPType a type of TCP server used by mesh module
const TCPType = tcp.Type
UDPType a type of UDP server used by mesh module
const UDPType = udp.Type
WSType a type of WebSocket server used by mesh module
const WSType = ws.Type
func Command(ver uint8, cmd uint16, handler func(*user.User, func(error)))
Command [WebSocket ONLY] use HandleCommand instead for other servers.
ver uint8 - Command version of the handler. cmd uint16 - Command ID of the handler. handler func(userData *user.User, next func(error))
Returns an error if it fails to assign a handler.
NOTE: If you assign multiple handles with the same ver and cmd, all handlers will be executed in the order of assignment.
func GetEndPoint() string
GetEndPoint returns the end point address the server is bound with
func GetServerType() string
GetServerType returns server type as a string
func HandleCommand(ver uint8, cmd uint16, handler func(uint8, uint16, []byte, *user.User, func(error))) error
HandleCommand registers a command handler function for TCP and UDP/RUDP server.
ver - Command version of the handler. cmd - Command ID of the handler. handler - Handler function to be executed when the server receives the command.
Returns an error if it fails to assign a handler.
[NOTE] If you assign multiple handles with the same ver and cmd, all handlers will be executed in the order of assignment.
func HandleWSCommand( ver float64, cmd float64, handler func(float64, float64, map[string]interface{}, *user.User, func(error)), ) error
HandleWSCommand registers WebSocket command function.
ver - Command version of the handler. cmd - Command ID of the handler. handler - Handler function to be executed when the server receives the command.
Returns an error if it fails to assign a handler.
[NOTE] If you assign multiple handles with the same ver and cmd, all handlers will be executed in the order of assignment.
func HookAllCommands(handler func(*user.User, func(error)))
HookAllCommands Registers a packet handler as a hook to all commands
handler func(userData *user.User, next func(error)) - Function to be invoked on command hook.
NOTE: The second argument next func(error) must be called at the end of handler to move the operation to next.
func IsCommandDuplicate(ver uint8, commandID uint16) bool
IsCommandDuplicate returns true if ver and commandID are used elsewhere when this function is invoked
ver uint8 - Command version to be checked for duplication. cmd uint16 - Command ID to be checked for duplication.
func IsP2PEnabled() bool
IsP2PEnabled returns true if UDP server enabled P2P by its configuration
func IsTCP() bool
IsTCP Returns true if the server is setup as TCP server
func IsUDP() bool
IsUDP Returns true if the server is setup as UDP server
func IsWebSocket() bool
IsWebSocket Returns true if the server is setup as WebSocket server
func MarkServerAsTakenIf(callback func() bool)
MarkServerAsTakenIf flags the server as "TAKEN" if the callback returns true. The callback is invoked every 2 second and if the callback returns true, the server will be marked as "ONLINE".
[IMPORTANT] If the server is marked as "OFFLINE", this function will be ignored.
[NOTE] This function is executed every 2 seconds so there will be race condition and it is not precise.
TAKEN - When the server is marked as "TAKEN", the server will NOT accept new user connections.
Example: The example code blow uses CCU of the node to control TAKEN <==> ONLINE
server.MarkServerAsTakenIf(func() bool { // user package can tell you the CCU of the node ccu := user.GetCCU() if ccu >= maxAllowedCCU { // this will mark the server as TAKEN return true } // this will mark the server as ONLINE return false })
func Offline()
Offline flags the server to be offline (will be shutdown) and will not accept new users
func OnDisconnect(callback func(string, *user.User))
OnDisconnect Registers a callback on connection termination for TCP/UDP
func OnKeepAlive(handler func(*user.User, func(error)))
OnKeepAlive assigns a callback function on TCP heartbeat or UDP echo
handler - Function to be invoked on keep alive message from the clients.
[NOTE] next func(error) must be called at the end of handler's operation.
func Online()
Online flags the server to be online and will accept new users
func SendPM(nodeAddr string, sid string, ver uint8, cmd uint16, message []byte)
SendPM sends a direct message to another user - The sent private message is a reliable delivery
nodeAddr string - This is the node (server) internal address of the target user. sid string - This is the sid (session ID) of the target user. ver uint8 - This is the command version to be used with the message sent. cmd uint16 - This is the command version to be used with the message sent. message []byte - This is the message data byte array.
func SetPublicEndPoint(addr string)
SetPublicEndPoint sets public end point to be sent to the client to be used to connect
addr string - This is the public endpoint for the server to be registered and used by the clients.
This is usually used internally.
func SetupAsConnector(path string)
SetupAsConnector Initialize the server as a Connector server for an external real-time server. This is meant to be used with an external real-time server such as Unreal Engine Dedicated server etc. Diarkis itself will not communicate wit the clients, but focuses on autoscaling and horizontal scaling on K8s.
path string - This is the absolute path of the configuration file to read.
func SetupAsTCPServer(path string)
SetupAsTCPServer Initialize as a TCP server. Load configurations from the file path given. Pass an empty string, if there is no need for configurations.
path string - This is the absolute path of the configuration file to read.
func SetupAsUDPServer(path string)
SetupAsUDPServer Initialize as a UDP server. Load configurations from the file path given. Pass an empty string, if there is no need for configurations.
path string - This is the absolute path of the configuration file to read.
func SetupAsWebSocketServer(path string)
SetupAsWebSocketServer Initialize as a WebSocket server. Load configurations from the file path given. The config name is "WS". Pass an empty string, if there is no need for configurations.
path string - This is the absolute path of the configuration file to read.
func SetupServerForAWS() error
SetupServerForAWS sets public endpoint in AWS environement Must be called in diarkis.OnReady() as a callback
func SetupServerForAlibaba() error
SetupServerForAlibaba sets public endpoint in Alibaba Cloud environment Must be called in diarkis.OnReady() as a callback
func SetupServerForAzure() error
SetupServerForAzure sets public endpoint in Microsoft Azure environement Must be called in diarkis.OnReady() as a callback
func SetupServerForGCP() error
SetupServerForGCP sets public endpoint in GCP environement Must be called in diarkis.OnReady() as a callback
func SetupServerForTencent() error
SetupServerForTencent sets public endpoint in Tencent Cloud environment Must be called in diarkis.OnReady() as a callback
func Taken()
Taken flags the server to be taken and will not accept new users.