事件订阅

更新时间: 2023/02/27 09:49:03

事件订阅,允许用户订阅其他人发布的事件,当被订阅人发布事件后,订阅者可以收到响应的通知。事件分为系统预定义事件和自定义事件,系统预定义事件类型范围为1-9999,用户自定义事件范围必须大于9999。 对于用户自定义事件,用户可以根据自己的需求场景来指定不同的事件类型和事件来定义不同的含义;对于系统预定义事件,系统保留1~9999的事件值,用户如果要发送自定义事件,事件值必须大于9999。 目前系统预定义事件只有在线状态事件,事件值为1,当用户登录、登出、异常退出后,服务器会下发响应的在线状态事件值来通知订阅者。用户在某端订阅或者取消订阅某些事件,会同时对其他端的生效,所以取消订阅事件时需谨慎,防止影响了其他端。

接收事件

提前注册好接收事件的回调函数,在登录后,回调函数会收到订阅的事件信息。需要注册两个回调函数:接收订阅事件回调函数、批量接收订阅的事件的回调函数。

示例代码如下:

C++

c++void OnPushEventCallback(nim::NIMResCode res_code, const nim::EventData& event_data)
{	
	if (res_code == nim::kNIMResSuccess)
	{
		...
	}
}

void :OnBatchPushEventCallback(nim::NIMResCode res_code, const std::list<nim::EventData>& event_list)
{
	if (res_code == nim::kNIMResSuccess)
	{
		...
	}
}

std::string foo()
{
	nim::SubscribeEvent::RegPushEventCb(&OnPushEventCallback);
	nim::SubscribeEvent::RegBatchPushEventCb(&OnBatchPushEventCallback);
));
}

C#

...

C

cvoid CallbackPushEvent(int res_code, const char *event_info_json, const char *json_extension, const void *user_data)
{
	if (user_data)
	{
		...
	}
}

void CallbackBatchPushEvent(int res_code, const char *event_list_json, const char *json_extension, const void *user_data)
{
	if (user_data)
	{
		...
	}
}

typedef void(*nim_subscribe_event_reg_push_event_cb)(const char *json_extension, nim_push_event_cb_func cb, const void *user_data);
typedef void(*nim_subscribe_event_reg_batch_push_event_cb)(const char *json_extension, nim_batch_push_event_cb_func cb, const void *user_data);

void foo()
{
	nim_subscribe_event_reg_push_event_cb func = (nim_subscribe_event_reg_push_event_cb) GetProcAddress(hInst, "nim_subscribe_event_reg_push_event_cb");
	nim_subscribe_event_reg_batch_push_event_cb batch_func = (nim_subscribe_event_reg_batch_push_event_cb) GetProcAddress(hInst, "nim_subscribe_event_reg_batch_push_event_cb");

	func(nullptr, &CallbackPushEvent, nullptr);
	batch_func(nullptr, &CallbackBatchPushEvent, nullptr);
}

发布事件

用户可以根据自己的需求来发布系统预定义事件或自定义事件。发布事件时,需要指定事件类型、事件值、事件消息id、事件有效期、事件广播类型、事件同步类型,可以根据需求选择是否附加自定义事件扩展属性。其中事件有效期范围为:60s到7天,单位秒,订阅者在有效期内登录才可以收到对应的事件;事件广播类型指定是否通知离线订阅者;事件同步类型指定是否把事件同步给自己的其他端;自定义事件扩展属性可以设置任意字符串。

示例代码如下:

C++

c++void EventCallback(nim::NIMResCode res_code, int event_type, const nim::EventData& event_data)
{
	...
}

std::string foo()
{
	nim::EventData event_data;
	event_data.event_type_ = nim::kNIMEventTypeOnlineState;
	event_data.event_value_ = nim::kNIMEventOnlineStateValueUpdateConfig;
	event_data.client_msg_id_ =本地定义的消息id;
	event_data.ttl_ = 7*24*60*60;
	event_data.broadcast_type_ = nim::kNIMEventBroadcastTypeAll;
	event_data.sync_self_ = nim::kNIMEventSyncTypeSelf;

	nim::SubscribeEvent::Publish(event_data, &EventCallback);
}

C#

...

C

cvoid CallbackPublishEvent(int res_code, int event_type, const char *event_info_json, const char *json_extension, const void *user_data)
{
	if (user_data)
	{
		...
	}
}

typedef void(*nim_publish_event)(const char *event_json, const char *json_extension, nim_publish_event_cb_func cb, const void *user_data);

void foo()
{
	nim_publish_event func = (nim_publish_event) GetProcAddress(hInst, "nim_publish_event");

	Json::FastWriter fs;
	Json::Value values;
	values[kNIMEventEventType] = nim::kNIMEventTypeOnlineState;
	values[kNIMEventEventValue] = nim::kNIMEventOnlineStateValueCustom + 1;
	values[kNIMEventMsgIdClient] = 本地定义的消息id;
	values[kNIMEventConfig] = config_;
	values[kNIMEventTTL] = 7*24*60*60;
	values[kNIMEventBroadcastType] = nim::kNIMEventBroadcastTypeAll;
	values[kNIMEventSyncSelf] = nim::kNIMEventSyncTypeSelf;
	std::string json_value = fs.write(values);

	func(json_value.c_str(), nullptr, &CallbackPublishEvent, nullptr);
}

订阅事件

只有先订阅了某个用户的某个事件后,才可以收到对应用户发布的事件。订阅事件,需要指定事件类型、订阅有效期、订阅后是否立即同步最新事件。其中订阅有效期范围为:60s到30天,单位秒,如果订阅有效期超时后,需要重新订阅才可以继续收到事件

示例代码如下:

C++

c++void EventCallback(nim::NIMResCode res_code, int event_type, const std::list<std::string>& faild_list)
{
	...
}

std::string foo()
{
	std::list<std::string> subscribe_list;
	subscribe_list.push_back("test1");
	subscribe_list.push_back("test2");

	nim::SubscribeEvent::Subscribe(nim::kNIMEventTypeOnlineState,
		1*24*60*60,
		nim::kNIMEventSubscribeSyncTypeSync,
		subscribe_ids,
		&EventCallback,
	);
}

C#

...

C

cvoid CallbackSubscribe(int res_code, int event_type, const char *faild_list_json, const char *json_extension, const void *user_data)
{
	...
}

typedef void(*nim_subscribe_event)(int event_type, int64_t ttl, int sync_event, const char *accid_list_json, const char *json_extension, nim_subscribe_event_cb_func cb, const void *user_data);

void foo()
{
	Json::FastWriter fs;
	Json::Value value;
	value.append("test1");
	value.append("test2");
	std::string json_value = fs.write(value);

	nim_subscribe_event func = (nim_subscribe_event) GetProcAddress(hInst, "nim_subscribe_event");
	func(nim::kNIMEventTypeOnlineState,
	1*24*60*60,
	nim::kNIMEventSubscribeSyncTypeSync,
	json_value.c_str(),
	nullptr,
	&CallbackSubscribe,
	nullptr);
}

取消订阅事件

示例代码如下:

C++

c++void EventCallback(nim::NIMResCode res_code, int event_type, const std::list<std::string>& faild_list)
{
	...
}

std::string foo()
{
	std::list<std::string> unsubscribe_list;
	unsubscribe_list.push_back("test1");
	unsubscribe_list.push_back("test2");

	nim::SubscribeEvent::UnSubscribe(nim::kNIMEventTypeOnlineState, unsubscribe_ids, &EventCallback);
}

C#

...

C

cvoid CallbackUnSubscribe(int res_code, int event_type, const char *faild_list_json, const char *json_extension, const void *user_data)
{
	...
}

typedef void(*nim_unsubscribe_event)(int event_type, const char *accid_list_json, const char *json_extension, nim_unsubscribe_event_cb_func cb, const void *user_data);

void foo()
{
	Json::FastWriter fs;
	Json::Value value;
	value.append("test1");
	value.append("test2");
	std::string json_value = fs.write(value);

	nim_unsubscribe_event func = (nim_unsubscribe_event) GetProcAddress(hInst, "nim_unsubscribe_event");
	func(nim::kNIMEventTypeOnlineState, json_value.c_str(),	nullptr, &CallbackUnSubscribe, nullptr);
}

批量取消订阅事件

示例代码如下:

C++

c++void EventCallback(nim::NIMResCode res_code, int event_type)
{
	...
}

std::string foo()
{
	nim::SubscribeEvent::BatchUnSubscribe(nim::kNIMEventTypeOnlineState, &EventCallback);
}

C#

...

C

cvoid CallbackBatchUnSubscribe(int res_code, int event_type, const char *json_extension, const void *user_data)
{
	if (user_data)
	{
		...
	}
}

typedef void(*nim_batch_unsubscribe_event)(int event_type, const char *json_extension, nim_batch_unsubscribe_event_cb_func cb, const void *user_data);

void foo()
{
	nim_batch_unsubscribe_event func = (nim_batch_unsubscribe_event) GetProcAddress(hInst, "nim_batch_unsubscribe_event");
	func(nim::kNIMEventTypeOnlineState, nullptr, &CallbackBatchUnSubscribe, nullptr);
}

查询订阅关系

示例代码如下:

C++

c++void EventCallback(nim::NIMResCode res_code, int event_type, const std::list<nim::EventSubscribeData>& subscribe_list)
{
	...
}

std::string foo()
{
	std::list<std::string> accid_list;
	accid_list.push_back("test1");
	accid_list.push_back("test2");

	nim::SubscribeEvent::QuerySubscribe(nim::kNIMEventTypeOnlineState, accid_list, &EventCallback);
}

C#

...

C

cvoid CallbackQuerySubscribe(int res_code, int event_type, const char *subscribe_list_json, const char *json_extension, const void *user_data)
{
	if (user_data)
	{
		...
	}
}

typedef void(*nim_query_subscribe_event)(int event_type, const char *accid_list_json, const char *json_extension, nim_query_subscribe_event_cb_func cb, const void *user_data);

void foo()
{
	Json::FastWriter fs;
	Json::Value value;
	value.append("test1");
	value.append("test2");
	std::string json_value = fs.write(value);

	nim_query_subscribe_event func = (nim_query_subscribe_event) GetProcAddress(hInst, "nim_query_subscribe_event");
	func(nim::kNIMEventTypeOnlineState, json_value.c_str(),	nullptr, &CallbackQuerySubscribe, nullptr);
}
此文档是否对你有帮助?
有帮助
去反馈
  • 接收事件
  • 发布事件
  • 订阅事件
  • 取消订阅事件
  • 批量取消订阅事件
  • 查询订阅关系