Message callback
Understand how to use message callback to handle messages.
Read time 1 minuteLast updated 20 hours ago
When you initialize the Vivox SDK, you need to set up your implementation to handle SDK messages. Using a callback method is the suggested best practice. This simplifies your game logic by only responding to messages when they occur, rather than using a polling loop that might be idle most of the time. The callback is a parameter for
vx_sdk_config_tVxcTypes.hThe following example is built on code from the SDKSampleApp with extra details on using OnSdkMessageAvailable on a ListenerThread:// When the SDK message callback is called, call vx_get_message() until there are no more messages.void (*pf_sdk_message_callback)(void *callback_handle);
static vx_sdk_config_t MakeConfig(const SDKSampleApp &app, unsigned int default_codecs_mask, vx_log_level log_level = log_error){ vx_sdk_config_t config; vx_get_default_config3(&config, sizeof(config)); config.pf_logging_callback = OnLog; config.pf_sdk_message_callback = OnSdkMessageAvailable;}void OnSdkMessageAvailable(void *callbackHandle){ SDKSampleApp *app = reinterpret_cast<SDKSampleApp *>(callbackHandle); app->set_event(m_messageAvailableEvent);}void SDKSampleApp::ListenerThread(){ for (; m_started;) // Pulls a message from the queue. If no message found, waits until notified, then tries again. { wait_event(m_messageAvailableEvent, -1); for (;;) { vx_message_base_t *msg = NULL; vx_get_message(&msg); // Type of message, and then type of the Event or Response is discovered, and the // appropriate call is made to process that message type. if (msg == NULL) { break; } Lock(); HandleMessage(msg); Unlock(); vx_destroy_message(msg); } } set_event(m_listenerThreadTerminatedEvent);}