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.
cmds/servers/ws/main.go |
package main import ( "fmt" "github.com/Diarkis/diarkis" "your/project/cmds" "github.com/Diarkis/diarkis/log" "github.com/Diarkis/diarkis/mesh" "github.com/Diarkis/diarkis/server" "os" ) func main() { rootpath, err := os.Getwd() if err != nil { panic(err) } log.Setup(fmt.Sprintf("%s/configs/shared/log.json", rootpath)) mesh.Setup(fmt.Sprintf("%s/configs/shared/mesh.json", rootpath)) server.SetupAsWebSocketServer(fmt.Sprintf("%s/configs/ws/main.json", rootpath)) cmds.Setup(rootpath) cmds.ExposeServer() diarkis.Start() } |
WebSocket Configurations
address |
“127.0.0.1” |
Address for WebSocket server to bind. |
nic |
“eth0” |
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 |
package customcmds |
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 RUDP userData.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 RUDP userData.ServerRespond(cmdVer, cmdID, dataMap, reliable) |