packagemainimport ("fmt""github.com/Diarkis/diarkis""your/project/cmds""github.com/Diarkis/diarkis/log""github.com/Diarkis/diarkis/mesh""github.com/Diarkis/diarkis/server""os")funcmain() { rootpath, err := os.Getwd()if err !=nil {panic(err) } log.Setup(fmt.Sprintf("%s/configs/shared/log.json", rootpath))// Sets up the internal communication network mesh.Setup(fmt.Sprintf("%s/configs/shared/mesh.json", rootpath))// Sets up the server as TCP server server.SetupAsTCPServer(fmt.Sprintf("%s/configs/tcp/main.json", rootpath)) cmds.Setup(rootpath)// Exposes all commands including built-in commands and custom commands cmds.ExposeServer() diarkis.Start()}
packagecustomcmdsimport ("github.com/Diarkis/diarkis/log""github.com/Diarkis/diarkis/server""github.com/Diarkis/diarkis/user")const customVer =2// 0 and 1 are reserved the Diarkis internalconst helloCmdID =10const pushCmdID =11var logger = log.New("CUSTOM")// this is called in cmds/main.gofuncExpose() { server.HandleCommand(customVer, helloCmdID, helloCmd) server.HandleCommand(customVer, helloCmdID, afterHelloCmd) server.HandleCommand(customVer, pushCmdID, pushCmd)}funchelloCmd(ver uint8, cmd uint16, payload []byte, userData *user.User, next func(error)) {// payload is the byte array sent from the client logger.Debug("Payload %#v SID:%s - UID:%s", payload, userData.SID, userData.ID)// if this is executed as UDP, reliable = true means sending the packet as an RUDP packet reliable :=true// we send a response back to the client with the byte array sent from the client userData.ServerRespond(payload, ver, cmd, server.Ok, reliable)// move on to the next command handler if there is anynext(nil)}funcafterHelloCmd(ver uint8, cmd uint16, payload []byte, userData *user.User, next func(error)) { logger.Debug("This is executed after Hello command has been handled")next(nil)}funcpushCmd(ver uint8, cmd uint16, payload []byte, userData *user.User, next func(error)) {// payload is the byte array sent from the client logger.Debug("Payload %#v SID:%s - UID:%s", payload, userData.SID, userData.ID)// if this is executed as UDP, reliable = true means sending the packet as an RUDP packet reliable :=true// we send a push packet to the client that sent the data to this command userData.ServerPush(ver, cmd, payload, reliable)// move on to the next command handler if there is anynext(nil)}
// 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.// 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(payloadByteArray, 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.// reliable is used for UDP server only and if this is set to true, the response becomes RUDPuserData.ServerRespond(cmdVer, cmdID, payloadByteArray, reliable)