Channel
You can implement channel joins and exits. You can also use callback when a new user joins or exits the channel.
When a channel group has been set in the console based on the user's country, language, countrycode, or guild, the user automatically enters the channel according to their properties when they connect to the chat server. To set the automatic channel group, check the console guide documentation.
You cannot use language, country, countrycode, guild, and whisper as channel group names. These are reserved words used in the Auto Create Channel function.
Description
There are three kinds of channel groups: open channels, private channels, and guild channels. Guild channels can be used together with BACKND Base's guild feature.
- Open channel: A public channel anyone can join. Users join with a channel group and a channel name.
- Private channel: A channel that requires a password to join. (Anyone can join if no password is set.) Users join with a channel group and a channel number.
- Guild channel: A channel linked to BACKND Base's guild feature. Only guild members can join, and they join automatically.
Channels are distinguished by their channel group first, so channel names may be duplicated. Think of a private channel as a room.
For example, there can be multiple private channels named Anyone up for the boss raid? in the party channel group. Duplicate channel names are allowed so channels can be used as one-off chat rooms.
The following details callback methods, call methods, and information classes related to the channel.
Callback method
// Called when channel join is completed
public void OnJoinChannel(ChannelInfo channelInfo)
{
// channelInfo: Contains channel information, user list, and previous messages
}
// Called when explicitly leaving a channel, when reconnection begins after a disconnection, or when ChatClient.Dispose() is called
public void OnLeaveChannel(ChannelInfo channelInfo)
{
// OnJoinChannel may be called again for the same channel after reconnection succeeds.
}
// Called when another user joins the channel
public void OnJoinChannelPlayer(
string channelGroup,
string channelName,
UInt64 channelNumber,
PlayerInfo player)
{
// player: Information about the user who joined
}
// Called when another user leaves the channel
public void OnLeaveChannelPlayer(
string channelGroup,
string channelName,
UInt64 channelNumber,
PlayerInfo player)
{
// player: Information about the user who left
}
Channel creation and join methods are processed only after the chat server connection has completed. Calls made immediately after creating ChatClient are ignored, so invoke them later, such as from the default channel's OnJoinChannel callback.
Call method
// Only private channels can be created in an SDK.
// You will need to use the console for other channel types.
// This is the method that creates private channels. It inserts and delivers the channel group, channel number, channel name, maximum users, and password.
// If you do not enter a password, the channel will be created as a public channel.
// If the channel number is sent as 0, the server automatically grants the channel number.
ChatClient.SendCreatePrivateChannel(string channelGroup, UInt64 channelNumber = 0, string channelName = "default", uint maxCount = 50, string password = "")
// This is the channel join method of the open channel type. It passes the channel group and the channel's name as arguments.
ChatClient.SendJoinOpenChannel(string channelGroup, string channelName)
// This is the channel join method of the private channel type. It passes the channel group and the channel's number as arguments.
ChatClient.SendJoinPrivateChannel(string channelGroup, ulong channelNumber, string password = "")
// This method leaves a channel. Pass the channel group, channel name, and a non-zero channel number.
ChatClient.SendLeaveChannel(string channelGroup, string channelName, UInt64 channelNumber)
// These methods are related to the guild channel and are usable only if you are using BACKND Base's guild function.
// This is the guild channel join method.
ChatClient.SendJoinGuildChannel();
// This is the guild channel exit method.
ChatClient.SendLeaveGuildChannel();
For example, you must call the following code after the guild creation (CreateGuildV3).
ChatClient.SendJoinGuildChannel();
You must call the following code after withdrawing from the guild (WithdrawGuildV3).
ChatClient.SendLeaveGuildChannel();
Additionally, if you set the guild registration acceptance to real-time notification, you must call the guild chat entry method when the OnApprovedGuildJoin callback occurs. Calling the method will automatically allow users to enter the guild chat channel of the guild they joined.
Guild chat will become unavailable until the user reconnects if the logic is not handled separately.
Enter the following code when creating a guild. You need to use a channel group name other than guild as it is a reserved word used in the auto channel creation function. You are automatically connected to the channel when the guild is created.
ChatClient.SendCreatePrivateChannel("guilds", 1, "guildname", 100, "");
After reconnecting, you must call the method below to connect to the guild channel.
ChatClient.SendJoinPrivateChannel("guilds", 1);
For guild members, call the method above upon immediate guild join or acceptance, even if the callback has been implemented.
If withdrawal and kick callback is implemented on a guild member, the following method must be called for that member to exit the guild channel:
ChatClient.SendLeaveChannel("guilds", "guildname", 1);
Information class
ChannelInfo
Channel information that is provided when entering a channel.
public class ChannelInfo
{
// Channel information
public string ChannelGroup; // Channel group name
public string ChannelName; // Channel name
public UInt64 ChannelNumber; // Channel number
public uint MaxCount; // Maximum members
// Channel data
public Dictionary<string, PlayerInfo> Players; // List of connected users
public List<MessageInfo> Messages; // Previous chat messages
}
PlayerInfo
Information of users in the channel.
public class PlayerInfo
{
public string GamerName; // User name
public string Avatar; // Avatar
public string Language; // Language
public Dictionary<string, string> Metadata; // Additional information
}
Check previous messages
public void OnJoinChannel(ChannelInfo channelInfo)
{
// Check previous messages
foreach (var msg in channelInfo.Messages)
{
Debug.Log($"[Previous messages] {msg.GamerName}: {msg.Message}");
}
// Check users who are currently logged in
foreach (var player in channelInfo.Players)
{
Debug.Log($"Connected: {player.Value.GamerName}");
}
}