#define CONVERSATION_MAGIC_HEADER_SIZE 8
#define CONVERSATION_MAGIC_STATUS_STR "conv"
static bool _is_target_message(const uint8_t* message, const char* target) {
if (message == NULL || target == NULL) {
// Check if the first 4 bytes match the magic number for "conv"
if (*(const uint32_t*)message != *(const uint32_t*)target) {
static void on_conversation_status_message_received(byte_rtc_engine_t engine, const cJSON* root) {
cJSON* stage_obj = cJSON_GetObjectItem(root, "Stage");
if (stage_obj != NULL) {
cJSON* code_obj = cJSON_GetObjectItem(stage_obj, "Code");
printf("conversation status message, code: %d\\n", (int)cJSON_GetNumberValue(code_obj));
cJSON* description_obj = cJSON_GetObjectItem(stage_obj, "Description");
if (description_obj != NULL) {
printf("conversation status message, description: %s\\n", cJSON_GetStringValue(description_obj));
cJSON* task_id_obj = cJSON_GetObjectItem(root, "TaskId");
if (task_id_obj != NULL) {
printf("conversation status message, task_id: %s\\n", cJSON_GetStringValue(task_id_obj));
cJSON* user_id_obj = cJSON_GetObjectItem(root, "UserID");
if (user_id_obj != NULL) {
printf("conversation status message, user_id: %s\\n", cJSON_GetStringValue(user_id_obj));
cJSON* round_id_obj = cJSON_GetObjectItem(root, "RoundID");
if (round_id_obj != NULL) {
printf("conversation status message, round_id: %d\\n", (int)cJSON_GetNumberValue(round_id_obj));
cJSON* event_time_obj = cJSON_GetObjectItem(root, "EventTime");
if (event_time_obj != NULL) {
printf("conversation status message, event_time: %lld\\n", (long long)cJSON_GetNumberValue(event_time_obj));
cJSON* error_info_obj = cJSON_GetObjectItem(root, "ErrorInfo");
if (error_info_obj != NULL && cJSON_IsObject(error_info_obj)) {
cJSON* error_code_obj = cJSON_GetObjectItem(error_info_obj, "Code");
if (error_code_obj != NULL) {
printf("error info, code: %d\\n", (int)cJSON_GetNumberValue(error_code_obj));
cJSON* reason_obj = cJSON_GetObjectItem(error_info_obj, "Reason");
if (reason_obj != NULL) {
printf("error info, reason: %s\\n", cJSON_GetStringValue(reason_obj));
static void on_message_received(byte_rtc_engine_t engine, const char* room, const char* src, const uint8_t* message, int size, bool binary) {
if (size < CONVERSATION_MAGIC_HEADER_SIZE) {
if (!_is_target_message(message, CONVERSATION_MAGIC_STATUS_STR)) {
root = cJSON_Parse((const char*)(message + CONVERSATION_MAGIC_HEADER_SIZE));
on_conversation_status_message_received(engine, root);