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)}
// 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)