Please enable Javascript to view the contents

PUN2 Realtime API 筆記

 ·  ☕ 2 分钟  ·  👩🏿‍🚀 伊琉沙 AKA 哇咔咔

實作環境

  • Unity2018.3.0f2
  • PUN 2.17
  • Photon lib 4.1.3.0

如果還沒加入PUN2可以參考上一篇,PUN2 快速上手

API

Photon.Realtime API

Connect()

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public virtual bool Connect()
{
    this.DisconnectedCause = DisconnectCause.None;

    #if UNITY_WEBGL
    SocketWebTcp.SerializationProtocol = Enum.GetName(typeof(SerializationProtocol), this.LoadBalancingPeer.SerializationProtocolType);
    #endif

    if (this.LoadBalancingPeer.Connect(this.MasterServerAddress, this.AppId, this.TokenForInit))
    {
        this.State = ClientState.ConnectingToMasterServer;
        return true;
    }

    #if UNITY_XBOXONE
    if (this.AuthValues == null || this.AuthValues.AuthType != CustomAuthenticationType.Xbox)
    {
        UnityEngine.Debug.LogError("UNITY_XBOXONE builds must set AuthValues.AuthType to \"CustomAuthenticationType.Xbox\". Set this before calling any Connect method. Connect failed!");
        return false;
    }
    #endif

    return false;
}

Connect可以直接連到主伺服器(MasterServer),調用此方法前必須先設定AppIdMasterServerAddress,如果使用Photon Cloud則應該使用ConnectToRegionMaster

1
PhotonNetwork.NetworkingClient.Connect();

ConnectToNameServer()

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public bool ConnectToNameServer()
{
    this.IsUsingNameServer = true;
    this.CloudRegion = null;

    #if UNITY_WEBGL
    SocketWebTcp.SerializationProtocol = Enum.GetName(typeof(SerializationProtocol), this.LoadBalancingPeer.SerializationProtocolType);
    #endif

    if (this.AuthMode == AuthModeOption.AuthOnceWss)
    {
        this.ExpectedProtocol = this.LoadBalancingPeer.TransportProtocol;
        this.LoadBalancingPeer.TransportProtocol = ConnectionProtocol.WebSocketSecure;
    }

    if (!this.LoadBalancingPeer.Connect(this.NameServerAddress, "NameServer", this.TokenForInit))
    {
        return false;
    }

    this.State = ClientState.ConnectingToNameServer;
    return true;
}

ConnectToNameServer可以直接連到名稱伺服器,調用此方法前必須先設定AppIdNameServerHost

實作
1
2
3
4
5
6
7
// 自定義連線方法
void ConnectToServer()
{
    PhotonNetwork.NetworkingClient.AppId = PhotonNetwork.PhotonServerSettings.AppSettings.AppIdRealtime;
    PhotonNetwork.NetworkingClient.NameServerHost = "ns.photonengine.cn"; // 如果不指定,預設會是 ns.exitgames.com
    PhotonNetwork.NetworkingClient.ConnectToNameServer();
}

執行後,PUN會開始連線(StatusCode.Connect)並建立加密(StatusCode.EncryptionEstablished)。

1
virtual void OnStatusChanged (StatusCode statusCode)

從內部狀態方法可發現,在加密完成後,會調用OpGetRegions方法取得名稱伺服器下的地區列表,這份列表儲存在RegionHandler類裡面。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
private bool OpGetRegions ()
{
    if (!this.CheckIfOpCanBeSent (OperationCode.GetRegions, this.Server, "GetRegions"))
    {
        return false;
    }

    bool sent = this.LoadBalancingPeer.OpGetRegions (this.AppId);
    return sent;
}

最後由OnRegionListReceived接收回傳的RegionHandler。

1
2
3
4
5
6
7
8
9
public void OnRegionListReceived (RegionHandler regionHandler)
{
    this.UpdateCallbackTargets ();

    foreach (IConnectionCallbacks target in this)
    {
        target.OnRegionListReceived (regionHandler);
    }
}

ConnectToRegionMaster()

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public bool ConnectToRegionMaster(string region)
{
    this.IsUsingNameServer = true;

    if (this.State == ClientState.ConnectedToNameServer)
    {
        this.CloudRegion = region;
        return this.CallAuthenticate();
    }

    this.LoadBalancingPeer.Disconnect();
    this.CloudRegion = region;

    #if UNITY_WEBGL
    SocketWebTcp.SerializationProtocol = Enum.GetName(typeof(SerializationProtocol), this.LoadBalancingPeer.SerializationProtocolType);
    #endif

    if (this.AuthMode == AuthModeOption.AuthOnceWss)
    {
        this.ExpectedProtocol = this.LoadBalancingPeer.TransportProtocol;
        this.LoadBalancingPeer.TransportProtocol = ConnectionProtocol.WebSocketSecure;
    }

    if (!this.LoadBalancingPeer.Connect(this.NameServerAddress, "NameServer", null))
    {
        return false;
    }

    this.State = ClientState.ConnectingToNameServer;
    return true;
}

ConnectToRegionMaster可透過region參數,連到指定地區的伺服器,調用此方法前必須先設定AppIdNameServerHost

實作
1
2
3
4
5
6
7
8
// 自定義連線方法
void ConnectToServer()
{
    string region = "asia"; // 指定地區
    PhotonNetwork.NetworkingClient.AppId = PhotonNetwork.PhotonServerSettings.AppSettings.AppIdRealtime;
    PhotonNetwork.NetworkingClient.NameServerHost = "ns.photonengine.cn"; // 如果不指定,預設會是 ns.exitgames.com
    PhotonNetwork.NetworkingClient.ConnectToRegionMaster(region);
}
分享

伊琉沙 AKA 哇咔咔
作者
伊琉沙 AKA 哇咔咔
遊戲開發者 / 密室機關師