UserIDs and Friends

UserIDs

在Photon中,玩家的身份是用一個唯一的UserID來識別的。

您可以用同一個用戶ID從多個客戶端訂閱一個Photon頻道。 您會在每個客戶端收到相同的消息。 同時,所有用同一個用戶ID連接的客戶端都會訂閱相同的頻道,在任何時候都有相同的好友。

Back To Top

獨特的用戶ID

一般來說,UserIDs不打算被顯示。 與用戶名、顯示名或綽號不同。 用戶ID不一定是人類可讀的,也不一定是非常人性化的。 因此,您可以,例如:使用一個GUID作為UserID。

每個玩家保持一個唯一的UserID的好處是。

  • 您可以在不同的遊戲環節和多個設備之間保存您的數據。 您可以重新加入房間,在您停止的地方繼續遊戲。
  • 您可以被您遇到的所有玩家所認識,並且容易被大家所識別。 您可以與您的朋友一起玩,向他們發送邀請和挑戰,進行在線聚會,組建團隊和公會,等等。 您可以添加用戶檔案(如經驗、統計、成就、等級等),使遊戲更具挑戰性(也可以使用錦標賽和排行榜)。
  • 您可以利用另一個服務將Photon UserID綁定到一個外部的唯一標識。 例如,Photon UserID可以被設置為Facebook ID、Google ID、Steam ID、PlayFab ID等。
  • 您可以通過保留一個用戶ID的黑名單並使用以下方法來禁止惡意用戶連接到您的應用程序。 自定義認証

Back To Top

設置用戶ID

Once authenticated, a Photon client will keep the same UserID until disconnected. The UserID for a client can be set in three ways:

  1. 客戶端在連接前通過設置AuthenticationValues.UserId來發送其UserID。 當您不使用自定義認証並希望設置一個用戶ID。
  2. 外部認証提供者在認証成功後返回UserID:見自定義認証.。它將覆蓋客戶端發送的任何值。
  3. Photon伺服器將為那些沒有使用1或2獲得UserIDs的用戶分配GUIDs作為ID。因此,即使是匿名用戶也會有UserIDs。

Back To Top

Users List In Public Channels

The following two features are available in C# client SDKs (Photon Chat included in PUN, Photon Unity SDK and Photon .NET SDK) and may not be part of other client SDKs. If this feature is missing from your client SDK, let us know via email.

If you want the client to have more control over the list of the users inside a public channel you can make use of MaxSubscribers and PublishSubscribers. By design, both options should be used to subscribe to a single channel at a time and not multiple ones.

Back To Top

MaxSubscribers

By default, public channels can have an unlimited number of subscribers. For some use cases, that number needs to be limited.

Photon Chat public channels can set a limit to the number of subscribers when created. We call this limit "MaxSubscribers". Knowing that a public channel is created when the first user subscribes to it, this means that, MaxSubscribers will be set by the first user who subscribes to the channel.

chatClient.Subscribe("theFiftyClan", creationOptions: new ChannelCreationOptions { MaxSubscribers = 50 });

If the client sets MaxSubscribers to 0, the public channel will have the default behaviour of unlimited subscribers.

To know the MaxSubscribers per channel use the channel class's property/field with the same name, e.g. in C# SDKs: ChatChannel.MaxSubscribers.

Back To Top

PublishSubscribers

By default, the Photon Chat client does not keep track of the users inside a public channel. It can only cache the list of the senders of the received messages by channel.

For some use cases, the chat client needs to have the list of the currently subscribed users at any given time. This feature, we call it "PublishSubscribers", can be enabled per channel during channel creation. Knowing that a public channel is created when the first user subscribes to it, this means that, "PublishSubscribers" can be activated by the first user who subscribes to the channel.

chatClient.Subscribe("specialChannel", creationOptions: new ChannelCreationOptions { PublishSubscribers = true });

The client will know if the subscription went successfully or not via an event. You can be notified about this using the same callback used for normal public channels:

IChatClientListener.OnSubscribed(string[] channels, bool[] results)

When PublishSubscribers is enabled inside a public channel, a client who subscribes to it will receive the list of previously subscribed users. Also, whenever a user unsubscribes from the same channel or a new user subscribes to it, the client will be notified using an event. These two mechanisms, the initial list of subscribers and future events of subscribers list changes, constitute the PublishSubscribers feature. They allow the client to always have an up-to-date list of subscribers per public channel.

To know if a public channel has PublishSubscribers enabled, use the channel class's property/field with the same name, e.g. in C# SDKs: ChatChannel.PublishSubscribers. To get the list of subscribers of a public channel, use the channel class's property/field with the same name, e.g. in C# SDKs: ChatChannel.Subscribers.

To be notified with subscribers list changes, subscribe to the callbacks:

IChatClientListener.OnUserSubscribed(string channel, string user)

and

IChatClientListener.OnUserUnsubscribed(string channel, string user)

MaxSubscribers can also be used in parallel with PublishSubscribers.

chatClient.Subscribe("evenMoreSpecialChannel", creationOptions: new ChannelCreationOptions { PublishSubscribers = true, MaxSubscribers = 16 })

However, when combined, MaxSubscribers will be capped to 1000. This means that when PublishSubscribers is enabled, the client can set MaxSubscribers to a value between 0 and 1000. If the client sets MaxSubscribers to 0, the public channel will have MaxSubscribers set to 1000.

Back To Top

朋友

  • Friends' UserIDs are case sensitive. Example: "mybestfriend" and "MyBestFriend" are two different UserIDs for two different friends.
  • Only friends connected to the same AppID, the same Photon Cloud region and play the same Photon AppVersion can find each other no matter what device or platform they're using.
  • FindFriends works only when connected to the Master Server, it does not work when the client is joined to a room.

Photon Chat會將您的好友名單保存在內存中,只要您還在連接。 但是這個列表不會在不同時段持續存在。 您可能需要一個外部服務來執行這一點。

Back To Top

添加和刪除好友

您可以在Photon Chat上添加好友並訂閱他/她的狀態更新。 如果您這樣做,每當該朋友改變狀態或離線時,您將收到一個事件。

添加好友後,每個好友都會收到一個初始狀態,並會觸發相應的回調。

您最多可以同時添加或刪除512個朋友。

要向Photon Chat添加朋友:

chatClient.AddFriends(friendsUserIds);

當您不再需要聽這些事件時,您可以刪除該好友並取消訂閱他/她的狀態更新。

要從Photon Chat中刪除朋友:

chatClient.RemoveFriends(friendsUserIds);

要接收朋友的狀態更新,您應該執行:

void IChatClientListener.OnStatusUpdate(string user, int status, bool gotMessage, object message);

Back To Top

更新狀態

您可以在Photon Chat上更新您的狀態,並廣播給您所有的朋友。

以下是如何執行:

chatClient.SetOnlineStatus(statusCode);

您可以在ChatUserStatus類中找到預定義的在線狀態代碼列表。 如果您將您的在線狀態設置為ChatUserStatus.Invisible,那麼您所有的朋友將看到您是ChatUserStatus.Offline。 您可以自由添加該列表中沒有的、您的應用程序所使用的自定義狀態。

根據設計,Photon Chat不會在您連接時隱含廣播ChatUserStatus.Online或其他狀態。 這就是為什麼如果您想擁有這種行為,您需要在連接後立即明確地將自己設置為在線。

在設計上,如果您沒有明確設置您的狀態為ChatUserStatus.InvisibleChatUserStatus.Offline,您的朋友會知道您與Photon Chat斷開連接。 他們會收到一個狀態更新,狀態設置為ChatUserStatus.Offline

您也可以選擇添加一條帶有更新狀態代碼的消息。 如果有兩個特殊狀態代碼ChatUserStatus.OfflineChatUserStatus.Invisible,則不會發送消息。

在這種情況下,使用重載方法,該方法需要兩個參數。

chatClient.SetOnlineStatus(statusCode, statusMessage);

您可以使用狀態更新功能與您的朋友交換任何類型的數據(Photon可以序列化的)。 它不只限於字符串。


To Document Top