ChatBox - Channels

Channels in chatboxs can be quiet tricky. However, I take away some of the pain for you by providing easy to use methods for clients to request the server to have them create channels, destroy channels, join channels, and invite others to created channels.

Joining A Channel

1. SetIDofChannelToJoin(string id)
2. RegisterWithChannel()

or 

1. RegisterWithChannel(int id)

If you want to join a channel first you need to call:

SetIDofChannelToJoin with a id of the channel you wish to join. This is meant to be tied into a InputField of a UI element. That can call this function either onEndEdit or onValueChanged of the InputField.

With the id of the channel you wish to join now the client needs to call RegisterWithChannel. There is also RegisterWithChannel(int id) if you are not using the SetIDofChannelToJoin function call. Calling this will join the client with that channel, if the channel exists. If the channel does not exist it will no respond to the client.

Example - Joining A Channel

protected virtual IEnumerator JoinChannel(int channelId)
{
    yield return new WaitUntil(() => ChatBox.instance != null);
    ChatBox.instance.SetIDofChannelToJoin(channelId);
    ChatBox.instance.RegisterWithChannel();
}

StartCoroutine(JoinChannel(1234));

Invite To Channel

1. UpdateInviteList({clientId}, true)
2. RequestInviteToChannel()

Now that you're a member of a private channel you can invite other clients to join the channel if they wish. To invite another client to a channel you need to first call UpdateInviteList with the clientId of the client you want to invite, the clientId can be obtained from the ClientConnection that is owned by that client.

With the inviteList populated via the UpdateInviteList function. You can now call RequestInviteToChannel to send an invite to everyone that is currently in the inviteList.

Example - Invite To Channel

protected virtual IEnumerator AddToInviteList(int clientId)
{
    yield return new WaitUntil(() => ChatBox.instance != null);
    ChatBox.instance.UpdateInviteList(clientId, true);
}
protected virtual IEnumerator RemoveFromInviteList(int clientId)
{
    yield return new WaitUntil(() => ChatBox.instance != null);
    ChatBox.instance.UpdateInviteList(clientId, false);
}


public virtual void AddToListButtonPress(int clientId)
{
    StartCoroutine(AddToInviteList(clientId));
}
public virtual void RemoveFromListButtonPress(int clientId)
{
    StartCoroutine(RemoveFromInviteList(clientId));
}
public virtual void SendOutInvitesButtonPress()
{
    try {
        ChatBox.instance.RequestInviteToChannel();
    }
    catch {}
}

Leave A Channel

1. SetActiveChannel({channelId})
2. RequestLeaveActiveChannel()

If you want to leave a channel call the RequestLeaveActiveChannel function. This will always attempt to leave whatever your current activeChannel is.

Example - Leave Channel

protected virtual IEnumerator LeaveChannel(int channelId)
{
    yield return new WaitUntil(() => ChatBox.instance != null);
    ChatBox.instance.SetActiveChannel(channelId);
    ChatBox.instance.RequestLeaveActiveChannel();
}
StartCoroutine(LeaveChannel(1234));

View Channel Invites

If you want to see what your current invites are just look at the invitedChannels dictionary.

Example - View Channel Invites

protected virtual IEnumerator DebugInvitedChannels()
{
    yield return new WaitUntil(() => ChatBox.instance != null);
    foreach(KeyValuePair<int, string> item in ChatBox.instance.invitedChannels)
    {
        Debug.Log("New Channel Name: {item.Value} with ID: {item.Key}");
    }
}

StartCoroutine(DebugInvitedChannels());

Create Channel

1. RequestCreateChannel({channelName})

If you want to create a private channel that you can invite other people to, simply call RequestCreateChannel with the name of the channel you want to create. If the server doesn't already have a channel by that name it will create it and respond back to the client with a success message. That success message will trigger the creation of the visual ui channel button and automatically join them to that channel. If that channel already exists, the server will not respond.

Example - Create Channel

protected virtual IEnumerator CreateChatBoxChannel(string channelName)
{
    yield return new WaitUntil(() => ChatBox.instance != null);
    ChatBox.instance.RequestCreateChannel(channelName);
}

StartCoroutine(CreateChatBoxChannel("my brand new channel"));