Photon Chatイントロ

はじめに

オンラインでPhoton Chatを使っているユーザーが継続的コミュニケーションをとれる手段を提供することができます。Photon Chatアプリは接続できる識別可能なアプリとAppIdが必要です。

無料でサインアップ した後、Photon ChatダッシュボードからAppIdを取得しましょう。

接続

アップデートやメッセージを受け取るには、コールバックとして使われる複数のメソッドを実装する必要があります。

C#

// In the C# SDKs, the callbacks are defined in the `IChatClientListener` interface.
// In the demos, we instantiate and use the ChatClient class to implement the IChatClientListener interface.
chatClient = new ChatClient( this );
// Set your favourite region. "EU", "US", and "ASIA" are currently supported.
chatClient.ChatRegion = "EU";
chatClient.Connect(chatAppId, chatAppVersion, new AuthenticationValues(userID));

C++

ChatListener chatListener; // replace 'ChatListener' with the name of your apps subclass of the abstract base class ExitGames::Chat::Listener or simply pass 'this' to the ExitGames::Chat::Client constructor if you prefer having one of your classes operating on ExitGames::Chat::Client AND inheriting from ExitGames::Chat::Listener
static const ExitGames::Common::JString appID = L"<no-app-id>"; // set your app id here
static const ExitGames::Common::JString appVersion = L"1.0";
ExitGames::Chat::Client chatClient(chatListener, appID, appVersion);
chatClient.setRegion(L"EU"); // Set your favorite region. "EU", "US", and "ASIA" are currently supported.
chatClient.connect(ExitGames::Chat::AuthenticationValues().setUserID(userID));

AppIDのほかConnect()はバージョン、任意の文字列、およびユーザー名(最良は固有の名前)に渡されます。

サービスを呼び出す

接続を維持し、受信メッセージを継続的に取得するには、以下を呼び出します:

C#

chatClient.Service();

C++

chatClient.service();

パブリックチャネルに登録する

登録は、パブリックチャネルについてのみ必要です。 プライベートチャネルでは、登録は不要です。

Photon Chatではチャネルを使ってユーザー、もしくはトピックをグループ化することができます。チャネルに登録した人は、送信されるメッセージを全て受信します。 初回の登録によって、新しいチャネルが作成されます。

C#

chatClient.Subscribe( new string[] { "channelA", "channelB" } );

C++

ExitGames::Common::JString channels[] = {L"channelA", L"channelB"};
chatClient.opSubscribe(ExitGames::Common::JVector<ExitGames::Common::JString>(channels, sizeof(channels)/sizeof(ExitGames::Common::JString)));

チャネルを登録するため、Subscribeに文字列の配列を渡します。

登録または解除を行うと、ChatClient.PublicChannels が更新されます。 オプションで、ChatChannelインスタンスは登録者リストを取得し、ChatChannel.Subscribersが更新されます。

送信する

パブリックメッセージ

送信をする前に、メッセージを送信したいチャネルに登録しましょう。 'PublishMessage()'を使ってチャンネル登録者全員にメッセージを送信しましょう。

C#

chatClient.PublishMessage( "channelA", "So Long, and Thanks for All the Fish!" );

C++

chatClient.opPublishMessage(L"channelA", L"So Long, and Thanks for All the Fish!");

Photon Chatでは、プレーンな文字列以外にたとえば招待などの複雑なメッセージを定義できます。

プライベートメッセージ

パブリックチャネルと異なり、プライベートチャネルでは事前の登録は不要です。

以下のように、任意のユーザーにプライベートメッセージを送信します:

C#

chatClient.SendPrivateMessage( "Arthur", "2 x 3 x 7" );

C++

chatClient.opSendPrivateMessage(L"Arthur", L"2 x 3 x 7");

受信する

パブリックメッセージ

登録済のすべてのパブリックチャネルからの受信パブリックメッセージを処理するには、適切なチャットリスナーコールバックが必要です:

C#

public class MyChatListner : IChatClientListener
{
    public void OnGetMessages( string channelName, string[] senders, object[] messages )
    {
       string msgs = "";
       for ( int i = 0; i < senders.Length; i++ )
       {
           msgs = string.Format("{0}{1}={2}, ", msgs, senders[i], messages[i]);
       }
       Console.WriteLine( "OnGetMessages: {0} ({1}) > {2}", channelName, senders.Length, msgs );
       // All public messages are automatically cached in `Dictionary<string, ChatChannel> PublicChannels`.
       // So you don't have to keep track of them.
       // The channel name is the key for `PublicChannels`.
       // In very long or active conversations, you might want to trim each channels history.
    }
// ...

C++

void ChatListener::onGetMessages(const ExitGames::Common::JString& channelName, const ExitGames::Common::JVector<ExitGames::Common::JString>& senders, const ExitGames::Common::JVector<ExitGames::Common::Object>& messages)
{
    for(unsigned int i=0; i<senders.getSize(); ++i)
        std::wcout << L"[" + channelName + L": " + senders[i] + L"] >>> " + messages[i].toString() << std::endl;
}

プライベートメッセージ

パブリックチャネルと異なり、プライベートチャネルでは事前の登録は不要です。

プライベートメッセージを受信してパースするには、以下のチャットリスナーコールバックを実装します:

C#

public class MyChatListner : IChatClientListener
{
    public void OnPrivateMessage( string sender, object message, string channelName )
    {
       Console.WriteLine( "OnPrivateMessage: {0} ({1}) > {2}", channelName, sender, message );
       // All private messages are automatically cached in `ChatClient.PrivateChannels`, so you don't have to keep track of them.
       // A channel name is applied as key for `PrivateChannels`.
       // Get a (remote) user's channel name with `ChatClient.GetPrivateChannelNameByUser(name)`.
       // e.g. To get and show all messages of a private channel:
       // ChatChannel ch = this.chatClient.PrivateChannels[ channelName ];
       // foreach ( object msg in ch.Messages )
       // {
       //     Console.WriteLine( msg );
       // }
    }
// ...

C++

void ChatListener::onPrivateMessage(const ExitGames::Common::JString& sender, const ExitGames::Common::Object& message, const ExitGames::Common::JString& channelName)
{
    std::wcout << L"[Private '" + channelName + L"': " + sender + L"] >>> " + message.toString() << std::endl;
}

オンラインステータス

自分のステータス

自分のプレイヤー向けのオンラインステータスと、オプションのステータスメッセージを設定します:

C#

chatClient.SetOnlineStatus( UserStatus.Online, "Mostly Harmless" );

C++

chatClient.opSetOnlineStatus(ExitGames::Chat::UserStatus::ONLINE, L"Mostly Harmless");

整数を使って自分のステータスを定義します。

フレンドのステータス

ユーザー名の配列をPhotonに送信するだけで、以降のユーザーステータスのアップデートを受信できます。

C#

friends = new List<string>() { "Ford", "Zaphod", "Marvin", "Eddie" };
chatClient.AddFriends(friends.ToArray());

C++

ExitGames::Common::JString friends[] = {L"Ford", L"Zaphod", L"Marvin", L"Eddie"};
chatClient.opAddFriends(ExitGames::Common::JVector<ExitGames::Common::JString>(friends, sizeof(friends)/sizeof(ExitGames::Common::JString)));

実装したコールバックへの、フレンドごとの最新ステータスのアップデートを受信します。

C#

void OnStatusUpdate( string user, int status, bool gotMessage, object message )
{
    Console.WriteLine( "Status change for: {0} to: {1}", user, status );
}

C++

void ChatListener::onStatusUpdate(const ExitGames::Common::JString& user, int status, bool gotMessage, const ExitGames::Common::Object& message)
{
    std::wcout << L"onStatusUpdate: " + user + L" has changed his/her status to: " + status + L" / " + (gotMessage?message.toString():L"[message skipped]") << std::endl;
}
Back to top