public override void ConnectRequest(UdpEndPoint endpoint, IProtocolToken token)
        {
            BoltLog.Warn("Connect Request");

            //token should be ServerConnectToken
            if (token != null)
            {
                BoltLog.Warn(token.GetType().ToString());

                ServerConnectToken t = token as ServerConnectToken;
                BoltLog.Warn("Server Token: null? " + (t == null));
                BoltLog.Warn("Data: " + t.data);
            }
            else
            {
                BoltLog.Warn("Received token is null");
            }

            ServerAcceptToken acceptToken = new ServerAcceptToken
            {
                data = "Accepted"
            };

            BoltNetwork.Accept(endpoint, acceptToken);
        }
        public override void Connected(BoltConnection connection)
        {
            BoltLog.Warn("Connected");

            ServerAcceptToken acceptToken = connection.AcceptToken as ServerAcceptToken;

            if (acceptToken != null)
            {
                BoltLog.Info("AcceptToken: " + acceptToken.GetType());
                BoltLog.Info("AcceptToken: " + acceptToken.data);
            }
            else
            {
                BoltLog.Warn("AcceptToken is NULL");
            }

            ServerConnectToken connectToken = connection.ConnectToken as ServerConnectToken;

            if (connectToken != null)
            {
                BoltLog.Info("ConnectToken: " + connectToken.GetType());
                BoltLog.Info("ConnectToken: " + connectToken.data);
            }
            else
            {
                BoltLog.Warn("ConnectToken is NULL");
            }
        }
        void OnGUI()
        {
            GUILayout.BeginArea(new Rect(10, 10, Screen.width - 20, Screen.height - 20));

            switch (_state)
            {
            // starting Bolt is the same regardless of the transport layer
            case State.SelectMode:
                if (GUILayout.Button("Start Server", GUILayout.ExpandWidth(true)))
                {
                    BoltLauncher.StartServer();
                    _state = State.ModeServer;
                }

                if (GUILayout.Button("Start Client", GUILayout.ExpandWidth(true)))
                {
                    BoltLauncher.StartClient();
                    _state = State.ModeClient;
                }

                break;

            // Publishing a session into the matchmaking server
            case State.ModeServer:
                if (BoltNetwork.IsRunning && BoltNetwork.IsServer)
                {
                    if (GUILayout.Button("Publish HostInfo And Load Map", GUILayout.ExpandWidth(true)))
                    {
                        // RoomProtocolToken token = new RoomProtocolToken()
                        // {
                        //  ArbitraryData = "My DATA",
                        //  password = "******"
                        // };

                        // Uncomment if you want to pass custom properties into your room
                        // This is just an example data
                        //PhotonCloudRoomProperties token = new PhotonCloudRoomProperties();
                        //properties.AddRoomProperty("t", 1);
                        //properties.AddRoomProperty("m", 4);

                        PhotonRoomProperties token = new PhotonRoomProperties();
                        token.IsOpen    = true;
                        token.IsVisible = true;

                        token.AddRoomProperty("t", 1);
                        token.AddRoomProperty("m", 2);

                        string matchName = "MyPhotonGame #" + UnityEngine.Random.Range(1, 100);

                        BoltMatchmaking.CreateSession(
                            sessionID: matchName,
                            token: token,
                            sceneToLoad: "PhotonGame"
                            );

                        // BoltNetwork.SetServerInfo(matchName, token);
                        // BoltNetwork.LoadScene("PhotonGame");
                    }
                }
                break;

            // for the client, after Bolt is innitialized, we should see the list
            // of available sessions and join one of them
            case State.ModeClient:

                if (BoltNetwork.IsRunning && BoltNetwork.IsClient)
                {
                    GUILayout.Label("Session List");

                    foreach (var session in BoltNetwork.SessionList)
                    {
                        // Simple session
                        UdpSession udpSession = session.Value as UdpSession;

                        // Skip if is not a Photon session
                        if (udpSession.Source != UdpSessionSource.Photon)
                        {
                            continue;
                        }

                        // Photon Session
                        PhotonSession photonSession = udpSession as PhotonSession;

                        string sessionDescription = String.Format("{0} / {1} ({2})",
                                                                  photonSession.Source, photonSession.HostName, photonSession.Id);

                        RoomProtocolToken token = photonSession.GetProtocolToken() as RoomProtocolToken;

                        if (token != null)
                        {
                            sessionDescription += String.Format(" :: {0}", token.ArbitraryData);
                        }
                        else
                        {
                            object value_t = -1;
                            object value_m = -1;

                            if (photonSession.Properties.ContainsKey("t"))
                            {
                                value_t = photonSession.Properties["t"];
                            }

                            if (photonSession.Properties.ContainsKey("m"))
                            {
                                value_t = photonSession.Properties["m"];
                            }

                            sessionDescription += String.Format(" :: {0}/{1}", value_t, value_m);
                        }

                        if (GUILayout.Button(sessionDescription, GUILayout.ExpandWidth(true)))
                        {
                            ServerConnectToken connectToken = new ServerConnectToken
                            {
                                data = "ConnectTokenData"
                            };

                            BoltNetwork.Connect(photonSession, connectToken);
                        }
                    }
                }
                break;
            }

            GUILayout.EndArea();
        }
Ejemplo n.º 4
0
        void ShowSessionList(Dictionary <Guid, InternalSession> sessionList)
        {
            foreach (var session in sessionList)
            {
                InternalSession internalSession = session.Value;

                UdpSession udpSession = internalSession.session as UdpSession;

                // Skip if is not a Photon session
                if (udpSession.Source != UdpSessionSource.Photon)
                {
                    BoltLog.Info("UdpSession with different Source: {0}", udpSession.Source);
                    continue;
                }

                PhotonSession photonSession = udpSession as PhotonSession;

                if (photonSession == null)
                {
                    continue;
                }

                string sessionDescription = String.Format("{0} ({1})",
                                                          photonSession.HostName, photonSession.Id);

                IProtocolToken token = photonSession.GetProtocolToken();

                // Normal Token
                RoomProtocolToken roomToken = token as RoomProtocolToken;

                if (roomToken != null)
                {
                    sessionDescription += String.Format(" :: DATA {0}", roomToken.ArbitraryData);
                }
                else
                {
                    object prop_type = -1;
                    object prop_map  = -1;

                    if (photonSession.Properties.ContainsKey("t"))
                    {
                        prop_type = photonSession.Properties["t"];
                    }

                    if (photonSession.Properties.ContainsKey("m"))
                    {
                        prop_map = photonSession.Properties["m"];
                    }

                    sessionDescription += String.Format(" :: {0} / {1}", prop_type, prop_map);
                }

                sessionDescription += string.Format(" :: {0}", internalSession.receivedAt);

                if (GUILayout.Button(sessionDescription, GUILayout.ExpandWidth(true)))
                {
                    ServerConnectToken connectToken = new ServerConnectToken
                    {
                        data = "ConnectTokenData"
                    };

                    BoltNetwork.Connect(photonSession, connectToken);
                }
            }
        }