Chat room
Update time: 2021/12/03 09:25:55
Overview
Chat room features:
- If a user joins a chat room, new connection must be created. If the user leaves or is removed from the chat room, connection will be terminated. If the user is disconnected from chat room, automatic re-connection starts. Developers must track connection status of the chat room to take required action.
- The number of online members in a chat room is not limited.
- A user is allowed to join multiple chat rooms and create multiple connections.
- A user may join a chat room at multiple clients.
- After disconnecting from a chat room, the user will no longer receive messages related to the chat room from the server.
- The chat room members include fixed members and guests.
Chat room SDK for PC provides a dynamic link library (nim_chat room.dll) that is different from IM SDK. Therefore, loading and using chat room features require making the following preparations:
Put the SDK related dll files (nim_chat room.dll) into the App's run directory. The SDK is developed based on vs2010. If the app does not have a corresponding runtime library file (Msvcp100.dll and msvcr100.dll), please put those files into the App's run directory.
After preparation is done, initialize SDK when the app starts; and perform SKD cleanup before leaving the app. Sample codes:
C++ initialization parameters: nim_chat room::ChatRoomPlatformConfig optional parameter, reference
void Init()
{
nim_chatroom::ChatRoom::Init("");
}
void Cleanup()
{
nim_chatroom::ChatRoom::Cleanup();
}
C#
void Init()
{
NIMChatRoom.ChatRoomApi.Init();
}
void Cleanup()
{
NIMChatRoom.ChatRoomApi.Cleanup();
}
C
typedef void(*nim_chatroom_init)(const char *json_extension);
typedef void(*nim_chatroom_cleanup)(const char *json_extension);
void Init()
{
// Get the interface for SDK initialization
HINSTANCE hInst = LoadLibraryW(L"nim_chatroom.dll");
nim_chatroom_init func = (nim_chatroom_init) GetProcAddress(hInst, "nim_chatroom_init");
//Initialize SDK
func("");
}
void Cleanup()
{
nim_chatroom_cleanup func = (nim_chatroom_cleanup) GetProcAddress(hInst, "nim_chatroom_cleanup");
func("");
FreeLibrary(hInst);
}
Proxy settings
The SDK supports SOCKS4, SOCKS4a and SOCKS5 proxies.
API prototype
-
C++
void SetProxy(NIMChatRoomProxyType type, const std::string& host, int port, const std::string& user, const std::string& password)
File:nim_chat room\api\nim_chat room_cpp.h
Namespace:nim_chat room
Class:ChatRoom
-
C#
void SetProxy(NIMChatRoomProxyType type,string host,int port,string user,string password)
Namespace:NIMChatRoom
Class:ChatRoomApi
-
C
void nim_chat room_set_proxy(enum NIMChatRoomProxyType type, const char *host, int port, const char *user, const char *password)
File:nim_chat room\api\nim_chat room.h
Parameters
- C
Parameter | Type | Description |
---|---|---|
type | NIMChatRoomProxyType | Proxy type |
host | string | Proxy address |
port | int | port |
user | string | account |
password | string | password |
Response parameters
No return value.
Join a chat room
Before joining chat room, it's required to obtain the permission by calling the IM module interface. Note: The interface here belongs to IM SDK module.
C++
void OnChatRoomRequestEnterCallback(int error_code, const std::string& result)
{
}
void foo()
{
//Get chat room login information
nim::PluginIn::ChatRoomRequestEnterAsync(room_id, &OnChatRoomRequestEnterCallback);
}
C#
void foo()
{
NIM.Plugin.ChatRoom.RequestLoginInfo(roomId, (response, authResult) =>
{
if (response == NIM.ResponseCode.kNIMResSuccess)
{
}
});
}
C
typedef void(*nim_plugin_chatroom_request_join_async)(const __int64 room_id, const char *json_extension, nim_plugin_chatroom_request_join_cb_func cb, const void *user_data);
void CallbackRequestChatRoomEnter(int error_code, const char *result, const char *json_extension, const void *user_data)
{
//result is permission information
}
void foo()
{
HINSTANCE hInst = LoadLibraryW(L"nim.dll");
nim_plugin_chatroom_request_join_async func = (nim_plugin_chatroom_request_join_async) GetProcAddress(hInst, "nim_plugin_chatroom_request_join_async");
func(room_id, null, &CallbackRequestChatRoomEnter, null);
}
After successfully obtaining the permission, you will get the permission information (result, the second parameter of the callback feature), after which you can call the interface to join the chat room.
C++
void foo()
{
ChatRoomEnterInfo info;
ChatRoom::Enter(room_id_, room_join_token, info);
}
C#
void foo()
{
NIMChatRoom.LoginData data = new NIMChatRoom.LoginData();
data.Nick = "【C# Client】";
NIMChatRoom.ChatRoomApi.Login(roomId, authResult, data);
}
C typedef bool(*nim_chat room_join)(const __int64 room_id, const char *request_join_data, const char *join_info, const char *json_extension);
void foo()
{
//Get the interface for joining a chat room
HINSTANCE hInst = LoadLibraryW(L"nim_chatroom.dll");
nim_chatroom_join func = (nim_chatroom_join) GetProcAddress(hInst, "nim_chatroom_join");
//Optional parameters for chat room.
Json::Value info_value;
info_value[kNIMChatRoomEnterKeyNick] = "my_profile name_of_room";//Set display name in a chat room.
info_value[kNIMChatRoomEnterKeyprofile picture] = "http://my_profile picture_url";//Set profile picture address in a chat room.
info_value[kNIMChatRoomEnterKeyExt] = GetJsonStringWithNoStyled(JSON_OBJECT);//Set extension field.
info_value[kNIMChatRoomEnterKeyNotifyExt] = GetJsonStringWithNoStyled(JSON_OBJECT);//Set extension field for notification.
//The permission information is input as a parameter here.
func(room_id, result, GetJsonStringWithNoStyled(info_value).c_str(), "");
}
Before joining the chat room, you need to register some global callbacks (See the API documentation for specific instructions). The following is an example of registering callback for result notification of joining chat room:
C++
void OnEnterCallback(__int64 room_id, const NIMChatRoomEnterStep step, int error_code, const ChatRoomInfo& info, const ChatRoomMemberInfo& my_info)
{
if (step != kNIMChatRoomEnterStepRoomAuthOver)
return;
if (error_code != nim::kNIMResSuccess && error_code != nim::kNIMResTimeoutError)
{
std::wstring kick_tip_str;
switch (error_code)
{
case nim::kNIMResNotExist:
kick_tip_str = L"The chat room cannot be found";
break;
case nim::kNIMResForbidden:
kick_tip_str = L"There is a problem in permission";
break;
case nim::kNIMResRoomLinkError:
case nim::kNIMResRoomError:
kick_tip_str = L"The chat room is not normal";
break;
case nim::kNIMResRoomBlackBeOut:
kick_tip_str = L"Users in the blocklist are not allowed to join the chat room";
break;
case nim::kNIMResFrequently:
kick_tip_str = L"You have operated frequently. Please try later.";
break;
default:
return;
}
}
else
{
}
}
void foo()
{
ChatRoom::RegEnterCb(&OnEnterCallback);
}
C#
void ChatRoomApi_LoginHandler(NIMChatRoom.NIMChatRoomLoginStep loginStep, NIM.ResponseCode errorCode, NIMChatRoom.ChatRoomInfo roomInfo, NIMChatRoom.MemberInfo memberInfo)
{
if (loginStep == NIMChatRoom.NIMChatRoomLoginStep.kNIMChatRoomLoginStepRoomAuthOver && errorCode == NIM.ResponseCode.kNIMResSuccess)
{
}
if (errorCode != NIM.ResponseCode.kNIMResSuccess)
{
MessageBox.Show(loginStep.ToString() + " " + errorCode.ToString(), "There is an error in joining the chat room");
}
}
void foo()
{
NIMChatRoom.ChatRoomApi.LoginHandler += ChatRoomApi_LoginHandler;
}
C
typedef void(*nim_chatroom_reg_join_cb)(const char *json_extension, nim_chatroom_join_cb_func cb, const void *user_data);
static void CallbackEnter(__int64 room_id, int step, int error_code, const char *result, const char *json_extension, const void *user_data)
{
}
void foo()
{
nim_chatroom_reg_join_cb func = (nim_chatroom_reg_join_cb)GetProcAddress(hInst, "nim_chatroom_reg_join_cb");
func(nullptr, &CallbackEnter, nullptr);
}
Kick a user out of a chat room
Call the interface nim_chat room_leave for removing a user from a chat room.
C++
void foo()
{
ChatRoom::Exit(room_id);
}
C#
void foo()
{
NIMChatRoom.ChatRoomApi.Exit(roomId);
}
C typedef void(*nim_chat room_leave)(const __int64 room_id, const char *json_extension);
void foo()
{
nim_chatroom_leave func = (nim_chatroom_leave) GetProcAddress(hInst, "nim_chatroom_leave");
func(room_id, nullptr);
}
To understand the reason for being kicked out of the chat room, the developer can register the global notification callback feature by using interface nim_chat room_reg_leave_cb.
C++
void OnEnterCallback(__int64 room_id, int error_code, NIMChatRoomExitReason leave_reason)
{
}
void foo()
{
ChatRoom::RegExitCb(&OnExitCallback);
}
C#
void ChatRoomApi_ExitHandler(long roomId, NIM.ResponseCode errorCode, NIMChatRoom.NIMChatRoomExitReason reason)
{
if (errorCode == NIM.ResponseCode.kNIMResSuccess)
{
}
}
void foo()
{
NIMChatRoom.ChatRoomApi.ExitHandler += ChatRoomApi_ExitHandler;
}
C
static void CallbackExit(__int64 room_id, int error_code, int leave_reason, const char *json_extension, const void *user_data)
{
...
}
typedef void(*nim_chatroom_reg_leave_cb)(const char *json_extension, nim_chatroom_leave_cb_func cb, const void *user_data);
void foo()
{
nim_chatroom_reg_leave_cb func = (nim_chatroom_reg_leave_cb) GetProcAddress(hInst, "nim_chatroom_reg_leave_cb");
func("", &CallbackExit, "");
}
Except for kickout by multiple clients, anchor and administrator, notification for leaving chat room will also appear when the chat room is closed or dismissed.
Notification for connection status in a chat room
Via the interface nim_chat room_reg_link_condition_cb to register the callback feature for current link status change, the developer use this feature to get current link status. C++
void OnRegLinkConditionCallback(__int64 room_id, const NIMChatRoomLinkCondition condition)
{
}
void foo()
{
ChatRoom::RegLinkConditionCb(&OnRegLinkConditionCallback);
}
C++
void OnRegLinkConditionCallback(__int64 room_id, const NIMChatRoomLinkCondition condition)
{
}
void foo()
{
ChatRoom::RegLinkConditionCb(&OnRegLinkConditionCallback);
}
C#
void ChatRoomApi_LinkStateChanged(long roomId, NIMChatRoomLinkCondition state)
{
}
void foo()
{
NIMChatRoom.ChatRoomApi.LinkStateChanged += ChatRoomApi_LinkStateChanged;
}
C
static void CallbackLinkCondition(__int64 room_id, int condition, const char *json_extension, const void *user_data)
{
}
typedef void(*nim_chatroom_reg_link_condition_cb)(const char *json_extension, nim_chatroom_link_condition_cb_func cb, const void *user_data);
void foo()
{
nim_chatroom_reg_link_condition_cb func = (nim_chatroom_reg_link_condition_cb) GetProcAddress(hInst, "nim_chatroom_reg_link_condition_cb");
func(nullptr, &CallbackLinkCondition, nullptr);
}
Get or update the profile of a chat room
Chat room information is available through callback feature of joining chat room or separate interface. SDK doesn't provide local cache for chat room information. It only provide the interface for query from the server, the use of which is subject to limitations on frequency of server interface usage.
C++
void OnGetChatRoomInfoCallback(__int64 room_id, int error_code, const ChatRoomInfo& info)
{
if (error_code != nim::kNIMResSuccess || room_id != room_id_)
{
return;
}
}
void foo()
{
ChatRoom::GetInfoAsync(room_id, &OnGetChatRoomInfoCallback);
}
C#
void foo()
{
NIMChatRoom.ChatRoomApi.GetRoomInfo(roomId, (room_Id, errorCode, info) =>
{
});
}
C
static void CallbackGetChatRoomInfo(__int64 room_id, int error_code, const char *result, const char *json_extension, const void *user_data)
{
}
typedef void(*nim_chatroom_get_info_async)(const __int64 room_id, const char *json_extension, nim_chatroom_get_info_cb_func cb, const void *user_data);
void foo()
{
nim_chatroom_get_info_async func = (nim_chatroom_get_info_async) GetProcAddress(hInst, "nim_chatroom_get_info_async");
func(room_id, nullptr, &CallbackGetChatRoomInfo, nullptr);
}
Update chat room information (requires administrator permission). Currently, only four fields can be updated, namely kNIMChatRoomInfoKeyName, kNIMChatRoomInfoKeyAnnouncement, kNIMChatRoomInfoKeyBroadcastUrl, kNIMChatRoomInfoKeyExt.
C++
void UpdateRoomCallback(int64_t room_id, int error_code)
{
}
void foo()
{
ChatRoomInfo info;
info.announcement_ = ; //Chat room announcement
info.name_ = ; //Chat room name
ChatRoom::UpdateRoomInfoAsync(room_id, info, true, "Broadcast notification", &UpdateRoomCallback);
}
C#
NIMChatRoom.ChatRoomApi.UpdateRoomInfo(long roomId, ChatRoomInfo info, UpdateRoomInfoDelegate cb, bool notify, string notify_ext);
Now, it is only available to update four attributes of ChatRoomInfo, i.e. RoomName, Announcement, BroadcastUrl, and Extension.
C
void CallbackUpdateRoomInfo(int64_t room_id, int error_code, const char *json_extension, const void *user_data)
{
}
typedef void(*nim_chatroom_update_room_info_async)(const int64_t room_id, const char *room_info_json_str, bool need_notify, const char *notify_ext, const char *json_extension, nim_chatroom_update_room_info_cb_func cb, const void *user_data);
void foo()
{
nim_chatroom_update_room_info_async func = (nim_chatroom_update_room_info_async) GetProcAddress(hInst, "nim_chatroom_update_room_info_async");
Json::Value values;
values[kNIMChatRoomInfoKeyName] = name_; //Chat room name.
values[kNIMChatRoomInfoKeyAnnouncement] = announcement_; //Chat room announcement.
values[kNIMChatRoomInfoKeyBroadcastUrl] = broadcast_url_; //Pull streaming address for video live streaming.
values[kNIMChatRoomInfoKeyExt] = ext_; //The third-party extension field.
Json::FastWriter fw;
func(room_id, fw.write(values).c_str(), true, "Broadcast notification", nullptr, &CallbackUpdateRoomInfo, nullptr);
}
Update my user profile
Only three fields can be updated, kNIMChatRoomMemberInfoKeyNick, kNIMChatRoomMemberInfoKeyprofile picture, kNIMChatRoomMemberInfoKeyExt. Starting from V3.8.0, the data of ordinary members can be persisted through parameters:
C++
void UpdateMyRoleCallback(int64_t room_id, int error_code)
{
}
void foo()
{
ChatRoomMemberInfo info;
info.nick_ = ; //The field of display name in a chat room.
info.profile picture_ = ; //profile picture in a chat room.
info.ext_ = ; //Extension field of developers.
Json::Value values;
values[nim_chatroom::kNIMChatRoomUpdateMyRoleExtNeedSave] = ; //bool It determines to persist.
Json::FastWriter fw;
std::string extension = fw.write(values);
ChatRoom::UpdateMyRoomRoleAsync(room_id, info, true, "Broadcast notification", &UpdateMyRoleCallback, extension);
}
C#
ChatRoomApi.UpdateMyRoleInfo.UpdateMyRoleInfo(long roomId, MemberInfo info, UpdateMyRoleDelegate cb, bool notify, string notify_ext, string json_extension)
Now, it is only available to update three attributes of MemberInfo, i.e. Nick, MemberIcon, Extension.
C
void CallbackUpdateMyRoomRole(int64_t room_id, int error_code, const char *json_extension, const void *user_data)
{
}
typedef void(*nim_chatroom_update_my_role_async)(const int64_t room_id, const char *role_info_json_str, bool need_notify, const char *notify_ext, const char *json_extension, nim_chatroom_update_my_role_cb_func cb, const void *user_data);
void foo()
{
nim_chatroom_update_my_role_async func = (nim_chatroom_update_my_role_async) GetProcAddress(hInst, "nim_chatroom_update_my_role_async");
Json::Value values;
values[kNIMChatRoomMemberInfoKeyNick] = id_; //The field of display name in a chat room.
values[kNIMChatRoomMemberInfoKeyprofile picture] = name_; //profile picture in a chat room.
values[kNIMChatRoomMemberInfoKeyExt] = announcement_; //Extension field of developers.
Json::FastWriter fw;
Json::Value ext_values;
ext_values[nim_chatroom::kNIMChatRoomUpdateMyRoleExtNeedSave] = ; //bool It determines to persist.
std::string extension = fw.write(values);
func(room_id, fw.write(values).c_str(), true, "Broadcast notification", extension.c_str(), &CallbackUpdateMyRoomRole, nullptr);
}
Send and receive chat room message
Chat room SDK supports multiple types of messages, including text, images, audio, video, geographic location, notifications, alerts, file messages, and custom messages.
-
Send message
Taking text message as an example:
C++
void foo() { std::string send_msg = ChatRoom::CreateRoomMessage(kNIMChatRoomMsgTypeText, QString::GetGUID(), text, ChatRoomMessageSetting()); ChatRoom::SendMsg(room_id_, send_msg); }
C#
void foo() { NIMChatRoom.Message msg = new NIMChatRoom.Message(); msg.MessageType = NIMChatRoom.NIMChatRoomMsgType.kNIMChatRoomMsgTypeText; msg.MessageAttachment = "This is a test message " + DateTime.Now.ToString() + " " + new Random().NextDouble().ToString(); NIMChatRoom.ChatRoomApi.SendMessage(roomId, msg); }
C
typedef void(*nim_chatroom_send_msg)(const __int64 room_id, const char *msg, const char *json_extension); void foo() { Json::Value values; values[kNIMChatRoomMsgKeyFromNick] = from_nick; //display name of message sender. values[kNIMChatRoomMsgKeyType] = msg_type; //Message type (NIMChatRoomMsgType). values[kNIMChatRoomMsgKeyAttach] = attach; //Message content, json structure (The non-formative content must be input when the message is sent.) values[kNIMChatRoomMsgKeyClientMsgid] = client_msg_id; //Client message ID. values[kNIMChatRoomMsgKeyResendFlag] = 0; //Message resending flag. 0 indicates the first sending; 1 indicates re-sending. values[kNIMChatRoomMsgKeyExt] = ext; //The third-party extension field, json structure (The non-formative content must be input when the message is sent.) nim_chatroom_send_msg func = (nim_chatroom_send_msg) GetProcAddress(hInst, "nim_chatroom_send_msg"); Json::FastWriter fw; func(room_id, fw.write(values).c_str(), ""); }
The message sending status can be obtained through the global callback registered before joining the chat room.
C++
void OnSendMsgCallback(__int64 room_id, int error_code, const ChatRoomMessage& result) { if (error_code != 200) { } } void foo() { ChatRoom::RegSendMsgAckCb(&OnSendMsgCallback); }
C#
void ChatRoomApi_SendMessageHandler(long roomId, NIM.ResponseCode code, NIMChatRoom.Message message) { if (code != NIM.ResponseCode.kNIMResSuccess) { MessageBox.Show("The chat room message is not sent successfully"); } } void foo() { NIMChatRoom.ChatRoomApi.SendMessageHandler += ChatRoomApi_SendMessageHandler; }
C
static void CallbackSendMsgAck(__int64 room_id, int error_code, const char *result, const char *json_extension, const void *user_data) { } typedef void(*nim_chatroom_reg_send_msg_ack_cb)(const char *json_extension, nim_chatroom_sendmsg_arc_cb_func cb, const void *user_data); void foo() { nim_chatroom_reg_send_msg_ack_cb func = (nim_chatroom_reg_send_msg_ack_cb) GetProcAddress(hInst, "nim_chatroom_reg_send_msg_ack_cb"); func(nullptr, &CallbackSendMsgAck, nullptr); }
-
Receiving messages
Register the global callback feature for receiving messages before joining a chat room.
C++
void OnReceiveMsgCallback(__int64 room_id, const ChatRoomMessage& result) { } void foo() { ChatRoom::RegReceiveMsgCb(&OnReceiveMsgCallback); }
C#
void ChatRoomApi_ReceiveMessageHandler(long roomId, NIMChatRoom.Message message) { string item = string.Format("{0}:\r\n{1}\r\n", message.SenderNickName, message.MessageAttachment); } void foo() { NIMChatRoom.ChatRoomApi.ReceiveMessageHandler += ChatRoomApi_ReceiveMessageHandler; }
C
static void CallbackReceiveMsg(__int64 room_id, const char *content, const char *json_extension, const void *user_data) { } typedef void(*nim_chatroom_reg_receive_msg_cb)(const char *json_extension, nim_chatroom_receive_msg_cb_func cb, const void *user_data); void foo() { nim_chatroom_reg_receive_msg_cb func = (nim_chatroom_reg_receive_msg_cb) GetProcAddress(hInst, "nim_chatroom_reg_receive_msg_cb"); func(nullptr, &CallbackReceiveMsg, nullptr); }
Query the message history
The SDK doesn't provide local cache for chat room member profile. It only provide the interface for query from the server, the use of which is subject to limitations on frequency of server interface usage.
C++
void GetMsgHistoryCallback(__int64 room_id, int error_code, const std::list<ChatRoomMessage>& msgs)
{
if (error_code != nim::kNIMResSuccess)
return;
}
void foo()
{
ChatRoomGetMsgHistoryParameters history_param;
history_param.limit_ = 10;
history_param.start_timetag_ = 0;
ChatRoom::GetMessageHistoryOnlineAsync(room_id, history_param, &GetMsgHistoryCallback);
}
C#
void foo()
{
NIMChatRoom.ChatRoomApi.QueryMessageHistoryOnline(roomId, 0, 50, (room_Id, errorCode, messages) =>
{
if (errorCode == NIM.ResponseCode.kNIMResSuccess)
});
}
C
typedef void(*nim_chatroom_get_msg_history_online_async)(const __int64 room_id, const char *parameters_json_str, const char *json_extension, nim_chatroom_get_msg_cb_func cb, const void *user_data);
static void CallbackGetMsgHistoryOnline(__int64 room_id, int error_code, const char *result, const char *json_extension, const void *user_data)
{
}
void foo()
{
nim_chatroom_get_msg_history_online_async func = (nim_chatroom_get_msg_history_online_async) GetProcAddress(hInst, "nim_chatroom_get_msg_history_online_async");
Json::Value values;
values[kNIMChatRoomGetMsgHistoryKeyStartTime] = start_timetag_; //Start time, unit: ms
values[kNIMChatRoomGetMsgHistoryKeyLimit] = limit_; //Limit of returned messages
Json::FastWriter fw;
func(room_id, fw.write(values).c_str(), nullptr, &CallbackGetMsgHistoryOnline, nullptr);
}
Get online members of chat room
SDK doesn't provide local cache for chat room member profile. It only provide the interface for query from the server, the use of which is subject to limitations on frequency of server interface usage.
SDK provides two interfaces for getting online member profile of chat rooms:
-
Get online member profile by paging
Member type definitionC++
void OnGetMembersCallback(__int64 room_id, int error_code, const std::list<ChatRoomMemberInfo>& infos) { if (error_code != nim::kNIMResSuccess) return; } void foo() { ChatRoomGetMembersParameters member_param; member_param.type_ = kNIMChatRoomGetMemberTypeSolid; ChatRoom::GetMembersOnlineAsync(room_id, member_param, &OnGetMembersCallback); }
C#
void foo() { NIMChatRoom.ChatRoomApi.QueryMembersOnline(roomId, NIMChatRoom.NIMChatRoomGetMemberType.kNIMChatRoomGetMemberTypeSolid, 0, 10, (room_Id, errorCode, Mmembers) => { }); }
C
typedef void(*nim_chatroom_get_members_online_async)(const __int64 room_id, const char *parameters_json_str, const char *json_extension, nim_chatroom_get_members_cb_func cb, const void *user_data); static void CallbackGetMembersOnline(__int64 room_id, int error_code, const char *result, const char *json_extension, const void *user_data) { } void foo() { nim_chatroom_get_members_online_async func = (nim_chatroom_get_members_online_async) GetProcAddress(hInst, "nim_chatroom_get_members_online_async"); Json::Value values; values[kNIMChatRoomGetMembersKeyType] = type_; //Member type values[kNIMChatRoomGetMembersKeyOffset] = timestamp_offset_; //Offset of member timestamp values[kNIMChatRoomGetMembersKeyLimit] = limit_; //Limit Json::FastWriter fw; func(room_id, fw.write(values).c_str(), nullptr, &CallbackGetMembersOnline, nullptr); }
-
Get online member profiles by ID.
C++
void OnGetMembersCallback(__int64 room_id, int error_code, const std::list<ChatRoomMemberInfo>& infos) { if (error_code != nim::kNIMResSuccess) return; } void foo() { std::list<std::string> ids; ids.push_back("user_id"); ChatRoom::GetMemberInfoByIDsAsync(room_id, ids, &OnGetMembersCallback); }
C#
void foo() { NIMChatRoom.ChatRoomApi.QueryMemberInfosByIdCollection(roomId, new string[] { "userID"}, (room_Id, errorCode, Mmembers) => { }); }
C
typedef void(*nim_chatroom_get_members_by_ids_online_async)(const __int64 room_id, const char *ids_json_array_string, const char *json_extension, nim_chatroom_get_members_cb_func cb, const void *user_data); static void CallbackGetMembersOnline(__int64 room_id, int error_code, const char *result, const char *json_extension, const void *user_data) { } void foo() { nim_chatroom_get_members_by_ids_online_async func = (nim_chatroom_get_members_by_ids_online_async) GetProcAddress(hInst, "nim_chatroom_get_members_by_ids_online_async"); func(room_id, ids, nullptr, &CallbackGetMembersOnline, nullptr); }
Kick online members out
C++
void KickMemberCallback(__int64 room_id, int error_code)
{
if (error_code != nim::kNIMResSuccess)
return;
}
void foo()
{
ChatRoom::KickMemberAsync(room_id, user_account, "", &KickMemberCallback);
}
C#
void foo()
{
NIMChatRoom.ChatRoomApi.RemoveMember(roomId, "userId", "Remove tip text", (room_Id, errorCode) =>
{
});
}
C
typedef void(*nim_chatroom_kick_member_async)(const __int64 room_id, const char *id, const char *notify_ext, const char *json_extension, nim_chatroom_kick_member_cb_func cb, const void *user_data);
static void CallbackKickMember(__int64 room_id, int error_code, const char *json_extension, const void *user_data)
{
}
void foo()
{
nim_chatroom_kick_member_async func = (nim_chatroom_kick_member_async) GetProcAddress(hInst, "nim_chatroom_kick_member_async");
func(room_id, id, nullptr, nullptr, &CallbackKickMember, nullptr);
}
Setting Chat room permissions
All settings (administrator, ordinary member, blocklist and mute) are configured through the same interface nim_chat room_set_member_attribute_async
Set as administrator
C++
void SetMemberAttributeCallback(__int64 room_id, int error_code, const ChatRoomMemberInfo& info)
{
if (error_code != nim::kNIMResSuccess)
return;
}
void foo()
{
ChatRoomSetMemberAttributeParameters param;
param.account_id_ = user_account;
param.attribute_ = kNIMChatRoomMemberAttributeMuteList;
param.opt_ = true;
ChatRoom::SetMemberAttributeOnlineAsync(room_id, param, &SetMemberAttributeCallback);
}
C#
void foo()
{
MemberProperty prop = new MemberProperty();
prop.MemberId = "user_id";
prop.Attribute = NIMChatRoomMemberAttribute.kNIMChatRoomMemberAttributeAdminister;
NIMChatRoom.ChatRoomApi.SetMemberPropertyOnline(roomId, prop, (room_Id, errorCode, info) =>
{
});
}
C
typedef void(*nim_chatroom_set_member_attribute_async)(const __int64 room_id, const char *parameters_json_str, const char *json_extension, nim_chatroom_set_member_attribute_cb_func cb, const void *user_data);
void foo()
{
nim_chatroom_set_member_attribute_async func = (nim_chatroom_set_member_attribute_async) GetProcAddress(hInst, "nim_chatroom_set_member_attribute_async");
Json::Value values;
values[kNIMChatRoomSetMemberAttributeKeyAccoutID] = account_id_; //Member ID
values[kNIMChatRoomSetMemberAttributeKeyNotifyExt] = notify_ext_; //Extension field of notification
//Set enumeration value of administrator
values[kNIMChatRoomSetMemberAttributeKeyAttribute] = kNIMChatRoomMemberAttributeAdminister;
//"True" is to set administrator, "false" is to Remove an administrator.
values[kNIMChatRoomSetMemberAttributeKeyOpt] = true;
Json::FastWriter fw;
func(room_id, fw.write(values).c_str(), nullptr, &CallbackSetMemberAtribute, nullptr);
}
Set as ordinary member
The ordinary member can be set through changing the above code field "Set enumeration value of administrator" to "Set enumeration value of ordinary member".
values[kNIMChatRoomSetMemberAttributeKeyAttribute] = kNIMChatRoomMemberAttributeNomalSold;
//"True" is to set an ordinary member, "false" is to cancel an ordinary member.
values[kNIMChatRoomSetMemberAttributeKeyOpt] = true;
Blocklist
The ordinary member can be set through changing the above code field "Set enumeration value of administrator" to "Set enumeration value of blocklist".
values[kNIMChatRoomSetMemberAttributeKeyAttribute] = kNIMChatRoomMemberAttributeBlocklist;
//"True" is to be added to a blocklist, "false" is to be removed from blocklist.
values[kNIMChatRoomSetMemberAttributeKeyOpt] = true;
Mute
The ordinary member can be set through changing the above code field "Set enumeration value of administrator" to "Set enumeration value of mute".
values[kNIMChatRoomSetMemberAttributeKeyAttribute] = kNIMChatRoomMemberAttributeMuteList;
//"True" is to set mute, "false" is to cancel mute.
values[kNIMChatRoomSetMemberAttributeKeyOpt] = true;
Temporary mute
You can set a time limit so that the member is automatically unmuted when the set time expires.
C++
void TempMuteCallback(__int64 room_id, int error_code, const ChatRoomMemberInfo& info)
{
if (error_code != nim::kNIMResSuccess)
return;
}
void foo()
{
ChatRoom::TempMuteMemberAsync(room_id, user_account, 60, true, "", &TempMuteCallback);
}
C#
void foo()
{
ChatRoomApi.TempMuteMember(roomId, accid, 120,
(roomId, errorCode, info) =>
{
}, true, accid + " Be muted for120s");
}
C
void CallbackTempMuteMember(int64_t room_id, int error_code, const char *result, const char *json_extension, const void *user_data)
{
// parse result
}
typedef void(*nim_chatroom_temp_mute_member_async)(const int64_t room_id, const char *accid, const int64_t duration, bool need_notify, const char *notify_ext, const char *json_extension, nim_chatroom_temp_mute_member_cb_func cb, const void *user_data);
void foo()
{
nim_chatroom_temp_mute_member_async func = (nim_chatroom_temp_mute_member_async) GetProcAddress(hInst, "nim_chatroom_temp_mute_member_async");
func(room_id, "user_id", 60, true, "Someone is muted", nullptr, &CallbackTempMuteMember, nullptr);
}
Chat room queuing service
-
Add or update the queue element of mic position (administrator permissions)
C++
void QueueOfferCallback(int64_t room_id, int error_code) { if (error_code != nim::kNIMResSuccess) return; } void foo() { ChatRoomQueueElement element; element.key_ = "key"; element.value_ = "value"; ChatRoom::QueueOfferAsync(room_id, element, &QueueOfferCallback); }
C#
void foo() { NIMChatRoom.ChatRoomApi.QueueOfferAsync(roomId, "key", "value", (room_id, error_code, json_extension, user_data) => { }); }
C
void CallbackQueueOffer(int64_t room_id, int error_code, const char *json_extension, const void *user_data) { } typedef void(*nim_chatroom_queue_offer_async)(const int64_t room_id, const char *element_key, const char *element_value, const char *json_extension, nim_chatroom_queue_offer_cb_func cb, const void *user_data); void foo() { nim_chatroom_queue_offer_async func = (nim_chatroom_queue_offer_async) GetProcAddress(hInst, "nim_chatroom_queue_offer_async"); func(room_id, "key", "value", nullptr, &CallbackQueueOffer, nullptr); }
-
Take out head element of mic position (administrator permission)
C++
void CallbackQueuePoll(int64_t room_id, int error_code, const ChatRoomQueueElement& element) { if (error_code != nim::kNIMResSuccess) return; } void foo() { ChatRoom::QueuePollAsync(room_id, "key", &CallbackQueuePoll); }
C#
void foo() { NIMChatRoom.ChatRoomApi.QueuePollAsync(roomId, "key", (room_id, error_code, result, json_extension, user_data) => { }); }
C
void CallbackQueuePoll(int64_t room_id, int error_code, const char *result, const char *json_extension, const void *user_data) { // parse result } typedef void(*nim_chatroom_queue_poll_async)(const int64_t room_id, const char *element_key, const char *json_extension, nim_chatroom_queue_poll_cb_func cb, const void *user_data); void foo() { nim_chatroom_queue_poll_async func = (nim_chatroom_queue_poll_async) GetProcAddress(hInst, "nim_chatroom_queue_poll_async"); func(room_id, "key", nullptr, &CallbackQueuePoll, nullptr); }
-
Sort and list all mic position elements
C++
void CallbackQueueList(int64_t room_id, int error_code, const ChatRoomQueue& queue) { if (error_code != nim::kNIMResSuccess) return; } void foo() { ChatRoom::QueueListAsync(room_id, &CallbackQueueList); }
C#
void foo() { NIMChatRoom.ChatRoomApi.QueueListAsync(roomId, (room_id, error_code, result, json_extension, user_data) => { if (error_code == ResponseCode.kNIMResSuccess) { } }); }
C
void CallbackQueueList(int64_t room_id, int error_code, const char *result, const char *json_extension, const void *user_data) { // parse result } typedef void(*nim_chatroom_queue_list_async)(const int64_t room_id, const char *json_extension, nim_chatroom_queue_list_cb_func cb, const void *user_data); void foo() { nim_chatroom_queue_list_async func = (nim_chatroom_queue_list_async) GetProcAddress(hInst, "nim_chatroom_queue_list_async"); func(room_id, nullptr, &CallbackQueueList, nullptr); }
-
Delete mic position queue (administrator permission)
C++
void CallbackQueueDrop(int64_t room_id, int error_code) { if (error_code != nim::kNIMResSuccess) return; } void foo() { ChatRoom::QueueDropAsync(room_id, &CallbackQueueDrop); }
C#
void foo() { NIMChatRoom.ChatRoomApi.QueueDropAsync(roomId, (room_id, error_code, json_extension, user_data) => { }); }
C
void CallbackQueueDrop(int64_t room_id, int error_code, const char *json_extension, const void *user_data) { } typedef void(*nim_chatroom_queue_drop_async)(const int64_t room_id, const char *json_extension, nim_chatroom_queue_drop_cb_func cb, const void *user_data); void foo() { nim_chatroom_queue_drop_async func = (nim_chatroom_queue_drop_async) GetProcAddress(hInst, "nim_chatroom_queue_drop_async"); func(room_id, nullptr, &CallbackQueueDrop, nullptr); }