WebSocket server is one of the three real-time communication servers of Diarkis specifically for web technology where you are able to implement your custom “commands'' for your applications. Commands are formatted packets sent from the client and handled on the server. This is how Diarkis server clusters interact with the client.
Name of a network interface to retrieve binding address from for WebSocket server.
port
“7100”
Port for WebSocket server to bind. The WebSocket server automatically looks for available ports starting from the given port.
uri
“/ws”
URI for the WebSocket communication from the client.
readInterval
400
Read interval in milliseconds for the incoming packets.
ttl
30
Connection TTL in seconds.
NOTE: For Cloud environment, address should be the private IP address and environment variable DIARKIS_CLOUD_ENV should be used together.
Using Environment Variables In Configurations
Diarkis has auto-replace features with environment variables for configurations.
You add special syntax values in your configurations and those values will be replaced by environment variables.
Example:
Environment variable: DIARKIS_TEST=127.0.0.1
Configuration: “address”: “{$DIARKIS_TEST}”
The value of “address” will be “127.0.0.1”.
How To Write Custom Command For WebSocket Server
Commands are formatted packets sent from the client to Diarkis server cluster and handled by the server. This is how Diarkis communicates with its clients. Command implementation interface is uniform across all network protocols.
cmds/custom/main. go
packagecustomcmdsimport ("github.com/Diarkis/diarkis/log""github.com/Diarkis/diarkis/server""github.com/Diarkis/diarkis/user")const customVer =2const helloCmdID =10const pushCmdID =11var logger = log.New("CUSTOM")// this is called in cmds/main.gofuncExpose() { server.HandleWSCommand(customVer, helloCmdID, helloCmd) server.HandleWSCommand(customVer, helloCmdID, afterHelloCmd) server.HandleWSCommand(customVer, pushCmdID, pushCmd)}funchelloCmd(ver, cmd float64, data map[ string] interface{}, userData *user.User, next func(error)) {// data is the parsed JSON data sent from the client logger.Debug("Data %#v SID:%s - UID:%s", data, userData.SID, userData.ID)// we send a response back to the client with the same data map sent from the client userData.ServerRespond(data, uint8(ver), uint16(cmd), server.Ok, true)// move on to the next command handler if there is anynext(nil)}funcafterHelloCmd(ver, cmd float64, data map[ string] interface{}, userData *user.User, next func(error)) { logger.Debug("This is executed after Hello command has been handled")next(nil)}funcpushCmd(ver, cmd float64, data map[ string] interface{}, userData *user.User, next func(error)) {// data is the parsed JSON data sent from the client logger.Debug("Data %#v SID:%s - UID:%s", data, userData.SID, userData.ID)// we send a push packet to the client that sent the data to this command userData.ServerPush( uint8(ver), uint16(cmd), data, true)// move on to the next command handler if there is anynext(nil)}
Differences Between ServerRespond and ServerPush
Diarkis client has two types of packet reception events. Response and Push.
Response packets have “status” similar to HTTP responses. This is typically used as a response to a command execution. Push does not have “status” unlike Response.
Push packets are usually sent from Diarkis server cluster without clients asking for them.
An example of push packets would be broadcast messages from a room.
Response:
// payloadByteArray is the message byte array to be sent to the client// cmdVer is the version of the response 0 and 1 is reserved by Diarkis internal.// cmdID is the command ID of the response.// dataMap is a map[string]interface{} type and will be encoded as a JSON to be sent to the client// response status: OK = 1, BAD = 4, and ERROR = 5// reliable is used for UDP server only and if this is set to true, the response becomes RUDPuserData.ServerRespond(dataMap, cmdVer, cmdID, status, reliable)
Push:
// payloadByteArray is the message byte array to be sent to the client// cmdVer is the version of the response 0 and 1 is reserved by Diarkis internal.// cmdID is the command ID of the response.// dataMap is a map[string]interface{} type and will be encoded as a JSON to be sent to the client// reliable is used for UDP server only and if this is set to true, the response becomes RUDPuserData.ServerRespond(cmdVer, cmdID, dataMap, reliable)