ConnectionState









How do I use Connection State
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: DemoBoxesGui.cs Copy
8     void OnGUI()
9     {
10         if (HideUI)
11         {
12             return;
13         }
14
15         GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
16
17         if (!PhotonNetwork.connected)
18         {
19             if (GUILayout.Button("Connect"))
20             {
21                 PhotonNetwork.ConnectUsingSettings(null);
22             }
23         }
24         else
25         {
26             if (GUILayout.Button("Disconnect"))
27             {
28                 PhotonNetwork.Disconnect();
29             }
30         }
31
32     }
File name: DemoOwnershipGui.cs Copy
23     public void OnGUI()
24     {
25         GUI.skin = this.Skin;
26         GUILayout.BeginArea(new Rect(Screen.width - 200, 0, 200, Screen.height));
27         {
28             string label = TransferOwnershipOnRequest ? "passing objects" : "rejecting to pass";
29             if (GUILayout.Button(label))
30             {
31                 this.TransferOwnershipOnRequest = !this.TransferOwnershipOnRequest;
32             }
33         }
34         GUILayout.EndArea();
35
36
37
38         if (PhotonNetwork.inRoom)
39         {
40             int playerNr = PhotonNetwork.player.ID;
41             string playerIsMaster = PhotonNetwork.player.isMasterClient ? "(master) " : "";
42             string playerColor = PlayerVariables.GetColorName(PhotonNetwork.player.ID);
43             GUILayout.Label(string.Format("player {0}, {1} {2}(you)", playerNr, playerColor, playerIsMaster));
44
45             foreach (PhotonPlayer otherPlayer in PhotonNetwork.otherPlayers)
46             {
47                 playerNr = otherPlayer.ID;
48                 playerIsMaster = otherPlayer.isMasterClient ? "(master)" : "";
49                 playerColor = PlayerVariables.GetColorName(otherPlayer.ID);
50                 GUILayout.Label(string.Format("player {0}, {1} {2}", playerNr, playerColor, playerIsMaster));
51             }
52
53             if (PhotonNetwork.inRoom && PhotonNetwork.otherPlayers.Length == 0)
54             {
55                 GUILayout.Label("Join more clients to switch object-control.");
56             }
57         }
58         else
59         {
60             GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
61         }
62     }
File name: InstantiateCube.cs Copy
12     void OnClick()
13     {
14         if (PhotonNetwork.connectionStateDetailed != PeerState.Joined)
15         {
16             // only use PhotonNetwork.Instantiate while in a room.
17             return;
18         }
19
20         switch (InstantiateType)
21         {
22             case 0:
23                 PhotonNetwork.Instantiate(Prefab.name, this.transform.position + 3*Vector3.up, Quaternion.identity, 0);
24                 break;
25             case 1:
26                 PhotonNetwork.InstantiateSceneObject(Prefab.name, InputToEvent.inputHitPos + new Vector3(0, 5f, 0), Quaternion.identity, 0, null);
27                 break;
28         }
29     }
File name: GUICustomAuth.cs Copy
81     void OnGUI()
82     {
83         if (PhotonNetwork.connected)
84         {
85             GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
86             return;
87         }
88
89
90         GUILayout.BeginArea(GuiRect);
91         switch (guiState)
92         {
93             case GuiState.AuthFailed:
94                 GUILayout.Label("Authentication Failed");
95
96                 GUILayout.Space(10);
97
98                 GUILayout.Label("Error message:\n'" + this.authDebugMessage + "'");
99
100                 GUILayout.Space(10);
101
102                 GUILayout.Label("For this demo set the Authentication URL in the Dashboard to:\nhttp://photon.webscript.io/auth-demo-equals");
103                 GUILayout.Label("That authentication-service has no user-database. It confirms any user if 'name equals password'.");
104                 GUILayout.Label("The error message comes from that service and can be customized.");
105
106                 GUILayout.Space(10);
107
108                 GUILayout.BeginHorizontal();
109                 if (GUILayout.Button("Back"))
110                 {
111                     SetStateAuthInput();
112                 }
113                 if (GUILayout.Button("Help"))
114                 {
115                     SetStateAuthHelp();
116                 }
117                 GUILayout.EndHorizontal();
118                 break;
119
120             case GuiState.AuthHelp:
121
122                 GUILayout.Label("By default, any player can connect to Photon.\n'Custom Authentication' can be enabled to reject players without valid user-account.");
123
124                 GUILayout.Label("The actual authentication must be done by a web-service which you host and customize. Example sourcecode for these services is available on the docs page.");
125
126                 GUILayout.Label("For this demo set the Authentication URL in the Dashboard to:\nhttp://photon.webscript.io/auth-demo-equals");
127                 GUILayout.Label("That authentication-service has no user-database. It confirms any user if 'name equals password'.");
128
129                 GUILayout.Space(10);
130                 if (GUILayout.Button("Configure Authentication (Dashboard)"))
131                 {
132                     Application.OpenURL("https://cloud.exitgames.com/dashboard");
133                 }
134                 if (GUILayout.Button("Authentication Docs"))
135                 {
136                     Application.OpenURL("https://doc.exitgames.com/en/pun/current/tutorials/pun-and-facebook-custom-authentication");
137                 }
138
139
140                 GUILayout.Space(10);
141                 if (GUILayout.Button("Back to input"))
142                 {
143                     SetStateAuthInput();
144                 }
145                 break;
146
147             case GuiState.AuthInput:
148
149                 GUILayout.Label("Authenticate yourself");
150
151                 GUILayout.BeginHorizontal();
152                 this.authName = GUILayout.TextField(this.authName, GUILayout.Width(Screen.width/4 - 5));
153                 GUILayout.FlexibleSpace();
154                 this.authToken = GUILayout.TextField(this.authToken, GUILayout.Width(Screen.width/4 - 5));
155                 GUILayout.EndHorizontal();
156
157
158                 if (GUILayout.Button("Authenticate"))
159                 {
160                     PhotonNetwork.AuthValues = new AuthenticationValues();
161                     PhotonNetwork.AuthValues.SetAuthParameters(this.authName, this.authToken);
162                     PhotonNetwork.ConnectUsingSettings("1.0");
163                 }
164
165                 GUILayout.Space(10);
166
167                 if (GUILayout.Button("Help", GUILayout.Width(100)))
168                 {
169                     SetStateAuthHelp();
170                 }
171
172                 break;
173         }
174
175         GUILayout.EndArea();
176     }
File name: GUIFriendsInRoom.cs Copy
15     public void OnGUI()
16     {
17         if (PhotonNetwork.connectionStateDetailed != PeerState.Joined)
18         {
19             return;
20         }
21
22         GUILayout.BeginArea(GuiRect);
23
24         GUILayout.Label("In-Game");
25         GUILayout.Label("For simplicity, this demo just shows the players in this room. The list will expand when more join.");
26         GUILayout.Label("Your (random) name: " + PhotonNetwork.playerName);
27         GUILayout.Label(PhotonNetwork.playerList.Length + " players in this room.");
28         GUILayout.Label("The others are:");
29         foreach (PhotonPlayer player in PhotonNetwork.otherPlayers)
30         {
31             GUILayout.Label(player.ToString());
32         }
33
34         if (GUILayout.Button("Leave"))
35         {
36             PhotonNetwork.LeaveRoom();
37         }
38         GUILayout.EndArea();
39     }
File name: IELdemo.cs Copy
59     public void OnGUI()
60     {
61         GUILayout.Space(10);
62         if (PhotonNetwork.isMasterClient)
63         {
64             GUILayout.Label("Move the cubes with the left and right keys. Run another client to check movement (smoothing) behaviour.");
65             GUILayout.Label("Ping: " + PhotonNetwork.GetPing());
66         }
67         else if (PhotonNetwork.isNonMasterClientInRoom)
68         {
69             GUILayout.Label("Check how smooth the movement is");
70             GUILayout.Label("Ping: " + PhotonNetwork.GetPing());
71         }
72         else
73         {
74             GUILayout.Label("Not connected..." + PhotonNetwork.connectionStateDetailed);
75         }
76     }
File name: WorkerMenu.cs Copy
43     public void Awake()
44     {
45         // this makes sure we can use PhotonNetwork.LoadLevel() on the master client and all clients in the same room sync their level automatically
46         PhotonNetwork.automaticallySyncScene = true;
47
48         // the following line checks if this client was just created (and not yet online). if so, we connect
49         if (PhotonNetwork.connectionStateDetailed == PeerState.PeerCreated)
50         {
51             // Connect to the photon master-server. We use the settings saved in PhotonServerSettings (a .asset file in this project)
52             PhotonNetwork.ConnectUsingSettings("0.9");
53         }
54
55         // generate a name for this player, if none is assigned yet
56         if (String.IsNullOrEmpty(PhotonNetwork.playerName))
57         {
58             PhotonNetwork.playerName = "Guest" + Random.Range(1, 9999);
59         }
60
61         // if you wanted more debug out, turn this on:
62         // PhotonNetwork.logLevel = NetworkLogLevel.Full;
63     }
File name: WorkerMenu.cs Copy
65     public void OnGUI()
66     {
67         if (this.Skin != null)
68         {
69             GUI.skin = this.Skin;
70         }
71
72         if (!PhotonNetwork.connected)
73         {
74             if (PhotonNetwork.connecting)
75             {
76                 GUILayout.Label("Connecting to: " + PhotonNetwork.ServerAddress);
77             }
78             else
79             {
80                 GUILayout.Label("Not connected. Check console output. Detailed connection state: " + PhotonNetwork.connectionStateDetailed + " Server: " + PhotonNetwork.ServerAddress);
81             }
82
83             if (this.connectFailed)
84             {
85                 GUILayout.Label("Connection failed. Check setup and use Setup Wizard to fix configuration.");
86                 GUILayout.Label(String.Format("Server: {0}", new object[] {PhotonNetwork.ServerAddress}));
87                 GUILayout.Label("AppId: " + PhotonNetwork.PhotonServerSettings.AppID);
88
89                 if (GUILayout.Button("Try Again", GUILayout.Width(100)))
90                 {
91                     this.connectFailed = false;
92                     PhotonNetwork.ConnectUsingSettings("0.9");
93                 }
94             }
95
96             return;
97         }
98
99         Rect content = new Rect((Screen.width - WidthAndHeight.x)/2, (Screen.height - WidthAndHeight.y)/2, WidthAndHeight.x, WidthAndHeight.y);
100         GUI.Box(content,"Join or Create Room");
101         GUILayout.BeginArea(content);
102
103         GUILayout.Space(40);
104
105         // Player name
106         GUILayout.BeginHorizontal();
107         GUILayout.Label("Player name:", GUILayout.Width(150));
108         PhotonNetwork.playerName = GUILayout.TextField(PhotonNetwork.playerName);
109         GUILayout.Space(158);
110         if (GUI.changed)
111         {
112             // Save name
113             PlayerPrefs.SetString("playerName", PhotonNetwork.playerName);
114         }
115         GUILayout.EndHorizontal();
116
117         GUILayout.Space(15);
118
119         // Join room by title
120         GUILayout.BeginHorizontal();
121         GUILayout.Label("Roomname:", GUILayout.Width(150));
122         this.roomName = GUILayout.TextField(this.roomName);
123
124         if (GUILayout.Button("Create Room", GUILayout.Width(150)))
125         {
126             PhotonNetwork.CreateRoom(this.roomName, new RoomOptions() { maxPlayers = 10 }, null);
127         }
128
129         GUILayout.EndHorizontal();
130
131         // Create a room (fails if exist!)
132         GUILayout.BeginHorizontal();
133         GUILayout.FlexibleSpace();
134         //this.roomName = GUILayout.TextField(this.roomName);
135         if (GUILayout.Button("Join Room", GUILayout.Width(150)))
136         {
137             PhotonNetwork.JoinRoom(this.roomName);
138         }
139
140         GUILayout.EndHorizontal();
141
142
143         if (!string.IsNullOrEmpty(this.ErrorDialog))
144         {
145             GUILayout.Label(this.ErrorDialog);
146
147             if (timeToClearDialog < Time.time)
148             {
149                 timeToClearDialog = 0;
150                 this.ErrorDialog = "";
151             }
152         }
153
154         GUILayout.Space(15);
155
156         // Join random room
157         GUILayout.BeginHorizontal();
158
159         GUILayout.Label(PhotonNetwork.countOfPlayers + " users are online in " + PhotonNetwork.countOfRooms + " rooms.");
160         GUILayout.FlexibleSpace();
161         if (GUILayout.Button("Join Random", GUILayout.Width(150)))
162         {
163             PhotonNetwork.JoinRandomRoom();
164         }
165
166
167         GUILayout.EndHorizontal();
168
169         GUILayout.Space(15);
170         if (PhotonNetwork.GetRoomList().Length == 0)
171         {
172             GUILayout.Label("Currently no games are available.");
173             GUILayout.Label("Rooms will be listed here, when they become available.");
174         }
175         else
176         {
177             GUILayout.Label(PhotonNetwork.GetRoomList().Length + " rooms available:");
178
179             // Room listing: simply call GetRoomList: no need to fetch/poll whatever!
180             this.scrollPos = GUILayout.BeginScrollView(this.scrollPos);
181             foreach (RoomInfo roomInfo in PhotonNetwork.GetRoomList())
182             {
183                 GUILayout.BeginHorizontal();
184                 GUILayout.Label(roomInfo.name + " " + roomInfo.playerCount + "/" + roomInfo.maxPlayers);
185                 if (GUILayout.Button("Join", GUILayout.Width(150)))
186                 {
187                     PhotonNetwork.JoinRoom(roomInfo.name);
188                 }
189
190                 GUILayout.EndHorizontal();
191             }
192
193             GUILayout.EndScrollView();
194         }
195
196         GUILayout.EndArea();
197     }
File name: RandomMatchmaker.cs Copy
31     void OnGUI()
32     {
33         GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
34
35         if (PhotonNetwork.connectionStateDetailed == PeerState.Joined)
36         {
37             bool shoutMarco = GameLogic.playerWhoIsIt == PhotonNetwork.player.ID;
38
39             if (shoutMarco && GUILayout.Button("Marco!"))
40             {
41                 myPhotonView.RPC("Marco", PhotonTargets.All);
42             }
43             if (!shoutMarco && GUILayout.Button("Polo!"))
44             {
45                 myPhotonView.RPC("Polo", PhotonTargets.All);
46             }
47         }
48     }
File name: PhotonConverter.cs Copy
189     static void ConvertToPhotonAPI(string file)
190     {
191         string text = File.ReadAllText(file);
192
193         bool isJS = file.Contains(".js");
194
195         file = file.Replace("\\", "/"); // Get Class name for JS
196         string className = file.Substring(file.LastIndexOf("/")+1);
197         className = className.Substring(0, className.IndexOf("."));
198
199
200         //REGEXP STUFF
201         //Valid are: Space { } , /n /r
202         //string NOT_VAR = @"([^A-Za-z0-9_\[\]\.]+)";
203         string NOT_VAR_WITH_DOT = @"([^A-Za-z0-9_]+)";
204
205         //string VAR_NONARRAY = @"[^A-Za-z0-9_]";
206
207
208         //NetworkView
209         {
210             text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkView" + NOT_VAR_WITH_DOT, "$1PhotonView$2");
211             text = PregReplace(text, NOT_VAR_WITH_DOT + "networkView" + NOT_VAR_WITH_DOT, "$1photonView$2");
212             text = PregReplace(text, NOT_VAR_WITH_DOT + "stateSynchronization" + NOT_VAR_WITH_DOT, "$1synchronization$2");
213             text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkStateSynchronization" + NOT_VAR_WITH_DOT, "$1ViewSynchronization$2"); // map Unity enum to ours
214             //.RPC
215             text = PregReplace(text, NOT_VAR_WITH_DOT + "RPCMode.Server" + NOT_VAR_WITH_DOT, "$1PhotonTargets.MasterClient$2");
216             text = PregReplace(text, NOT_VAR_WITH_DOT + "RPCMode" + NOT_VAR_WITH_DOT, "$1PhotonTargets$2");
217         }
218
219         //NetworkMessageInfo: 100%
220         {
221             text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkMessageInfo" + NOT_VAR_WITH_DOT, "$1PhotonMessageInfo$2");
222             text = PregReplace(text, NOT_VAR_WITH_DOT + "networkView" + NOT_VAR_WITH_DOT, "$1photonView$2");
223         }
224
225         //NetworkViewID:
226         {
227             text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkViewID" + NOT_VAR_WITH_DOT, "$1int$2"); //We simply use an int
228         }
229
230         //NetworkPlayer
231         {
232             text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkPlayer" + NOT_VAR_WITH_DOT, "$1PhotonPlayer$2");
233         }
234
235         //Network
236         {
237             //Monobehaviour callbacks
238             {
239                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnPlayerConnected" + NOT_VAR_WITH_DOT, "$1OnPhotonPlayerConnected$2");
240                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnPlayerDisconnected" + NOT_VAR_WITH_DOT, "$1OnPhotonPlayerDisconnected$2");
241                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnNetworkInstantiate" + NOT_VAR_WITH_DOT, "$1OnPhotonInstantiate$2");
242                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnSerializeNetworkView" + NOT_VAR_WITH_DOT, "$1OnPhotonSerializeView$2");
243                 text = PregReplace(text, NOT_VAR_WITH_DOT + "BitStream" + NOT_VAR_WITH_DOT, "$1PhotonStream$2");
244
245                 //Not completely the same meaning
246                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnServerInitialized" + NOT_VAR_WITH_DOT, "$1OnCreatedRoom$2");
247                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnConnectedToServer" + NOT_VAR_WITH_DOT, "$1OnJoinedRoom$2");
248
249                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnFailedToConnectToMasterServer" + NOT_VAR_WITH_DOT, "$1OnFailedToConnectToPhoton$2");
250                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnFailedToConnect" + NOT_VAR_WITH_DOT, "$1OnFailedToConnect_OBSELETE$2");
251             }
252
253             //Variables
254             {
255
256                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.connections" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.playerList$2");
257                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.isServer" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isMasterClient$2");
258                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.isClient" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isNonMasterClientInRoom$2");
259
260                 text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkPeerType" + NOT_VAR_WITH_DOT, "$1ConnectionState$2");
261                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.peerType" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.connectionState$2");
262                 text = PregReplace(text, NOT_VAR_WITH_DOT + "ConnectionState.Server" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isMasterClient$2");
263                 text = PregReplace(text, NOT_VAR_WITH_DOT + "ConnectionState.Client" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isNonMasterClientInRoom$2");
264                 text = PregReplace(text, NOT_VAR_WITH_DOT + "PhotonNetwork.playerList.Length" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.playerList.Count$2");
265
266                 /*DROPPED:
267                     minimumAllocatableViewIDs
268                     natFacilitatorIP is dropped
269                     natFacilitatorPort is dropped
270                     connectionTesterIP
271                     connectionTesterPort
272                     proxyIP
273                     proxyPort
274                     useProxy
275                     proxyPassword
276                  */
277             }
278
279             //Methods
280             {
281                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.InitializeServer" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.CreateRoom$2");
282                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.Connect" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.JoinRoom$2");
283                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.GetAveragePing" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.GetPing$2");
284                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.GetLastPing" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.GetPing$2");
285                 /*DROPPED:
286                     TestConnection
287                     TestConnectionNAT
288                     HavePublicAddress
289                 */
290             }
291
292             //Overall
293             text = PregReplace(text, NOT_VAR_WITH_DOT + "Network" + NOT_VAR_WITH_DOT, "$1PhotonNetwork$2");
294
295
296         //Changed methods
297              string ignoreMe = @"([A-Za-z0-9_\[\]\(\) ]+)";
298
299          text = PregReplace(text, NOT_VAR_WITH_DOT + "PhotonNetwork.GetPing\\(" + ignoreMe+"\\);", "$1PhotonNetwork.GetPing();");
300         text = PregReplace(text, NOT_VAR_WITH_DOT + "PhotonNetwork.CloseConnection\\(" + ignoreMe+","+ignoreMe+"\\);", "$1PhotonNetwork.CloseConnection($2);");
301
302         }
303
304         //General
305         {
306             if (text.Contains("Photon")) //Only use the PhotonMonoBehaviour if we use photonView and friends.
307             {
308                 if (isJS)//JS
309                 {
310                     if (text.Contains("extends MonoBehaviour"))
311                         text = PregReplace(text, "extends MonoBehaviour", "extends Photon.MonoBehaviour");
312                     else
313                         text = "class " + className + " extends Photon.MonoBehaviour {\n" + text + "\n}";
314                 }
315                 else //C#
316                     text = PregReplace(text, ": MonoBehaviour", ": Photon.MonoBehaviour");
317             }
318         }
319
320         File.WriteAllText(file, text);
321     }

ConnectionState 111 lượt xem

Gõ tìm kiếm nhanh...