Channel class


Channel class represents a single chat channel. It allows you to send messages and receive messages from other players.

Channel class is an abstract class.

Properties

Type Name Description
string Name Name of the channel
bool IsPublic True if the channel is public
bool IsJoined True if the player is in the channel
Players IReadOnlyCollection\<ChatPlayerInfo> Collection of players in the channel

Methods

CreateOrJoinChannel

Joins the channel if it exists, or creates a new one if it does not. If new channel is created, it will be public or private, depending on the value of IsPublic flag passed to this method.

OperationResult CreateOrJoinChannel(bool isPublic = true);

LeaveChannel

Leaves the channel if the player has joined.

Throws exception if the player is not in the channel.

OperationResult LeaveChannel();

PlayerJoined

Abstract method which is invoked when any player joins the channel.

void PlayerJoined(ChatPlayerInfo player);

PlayerJoined

Abstract method which is invoked when any player leaves the channel.

void PlayerLeft(ChatPlayerInfo player);

MessageReceived

Abstract method which is invoked when any player sends a message in the channel.

void MessageReceived(ChatPlayerInfo player, string message);

Send

Sends the message to all players in the channel.

Throws exception if the player is not in the channel.

void Send(string message);

Examples

To join any channel, we need to create a class which inherits the Channel class. In this example, we want to create a world chat, where any player can join.

The name of the channel in this example is "world".

public class WorldChannel : Channel
{
    public WorldChannel(IChatService chatService) : base(chatService, "world")
    {
    }

    public override void PlayerJoined(ChatPlayerInfo player)
    {
        Console.WriteLine($"Player {player.DisplayName} joined the world channel.");
    }

    public override void PlayerLeft(ChatPlayerInfo player)
    {
        Console.WriteLine($"Player {player.DisplayName} left the world channel.");
    }

    public override void MessageReceived(ChatPlayerInfo player, string message)
    {
        Console.WriteLine($"Player {player.DisplayName} sent a message: {message}");
    }
}

Next, create the channel and invoke CreateOrJoinChannel method.

public async Task JoinWorldChannel()
{
    var worldChannel = new WorldChannel(_chatService);
    await worldChannel.CreateOrJoinChannel();
}

If the channel is succesfully joined, new messages will trigger the MessageReceived callback.

To send a message, invoke Send method:

worldChannel.Send("Hello world!");

To leave a channel, invoke LeaveChannel method:

await worldChannel.LeaveChannel();

results matching ""

    No results matching ""