RoomGroupClient initBalanceEngine()
    {
        Config config = new Config();

        config.hostname = ServerAddress;
        config.port     = ServerPort;
        config.debugLog = ClientDebugLog;

        Config udpConfig = new Config();

        udpConfig.hostname = ServerAddress;
        udpConfig.port     = UdpServerPort;
        config.debugLog    = ClientDebugLog;

        WSClient        ws  = new WSClient();
        RoomGroupClient rgc = new RoomGroupClient(config, ws, udpConfig, this.debugLog);

        ws.OnConnect += () => {
            Debug.Log("connected.");
        };

        rgc.OnReady += () => {
            Debug.Log("rgc is ready: " + rgc.GetIdentification());
        };

        rgc.OnJoinedQueue += packet => {
            Debug.Log("queue joined.");
        };

        rgc.Run();
        return(rgc);
    }
    // Called when Unity3D updates its UI
    void OnGUI()
    {
        if (rgc == null)
        {
            rgc = BalanceUnity.ACCESS().GetRGC();
        }

        if (rgc == null || !rgc.IsReady())
        {
            GUI.Label(new Rect(10, 10, 100, 20), "Getting ready..");
            return;
        }

        //MM-Queue
        if (!rgc.IsInQueue() && !rgc.IsInMatch() && !rgc.IsConfirmationOpen())
        {
            if (GUI.Button(new Rect(10, 10, 126, 30), "Join Match-Making"))
            {
                rgc.JoinMatchMakingQueue();
            }
        }

        if (rgc.IsInQueue() && !rgc.IsConfirmationOpen())
        {
            if (GUI.Button(new Rect(10, 10, 146, 30), "Leave Match-Making"))
            {
                rgc.LeaveMatchMakingQueue();
            }
        }

        //Match-Confirmation
        if (rgc.IsConfirmationOpen() && !rgc.HasClientConfirmed())
        {
            if (GUI.Button(new Rect(10, 50, 126, 30), "Confirm Match"))
            {
                rgc.ConfirmMatchRequest();
            }
        }

        //Match
        if (rgc.IsInMatch())
        {
            if (GUI.Button(new Rect(10, 90, 126, 30), "Leave Match"))
            {
                rgc.ExitMatch();
            }
        }

        /*
         * //Reconnect
         * if (rgc.IsReady())
         * {
         *  if (GUI.Button(new Rect(10, 130, 126, 30), "Reconnect"))
         *  {
         *      BalanceUnity.ACCESS().Reconnect();
         *  }
         * } */
    }
 void endRgc()
 {
     if (rgc != null)
     {
         Debug.Log("closed");
         rgc.Close();
         rgc = null;
     }
 }
    public void Reconnect()
    {
        if (rgc != null && rgc.IsInMatch())
        {
            rgc.ExitMatch();
        }

        endRgc();
        rgc = initBalanceEngine();
    }
Beispiel #5
0
    // Update is called once per frame
    void Update()
    {
        if (rgc == null)
        {
            rgc = BalanceUnity.ACCESS().GetRGC();

            rgc.OnMatchDisband += packet => {
                MainThread.Call(() =>
                {
                    removeOthers();
                    removePlayer();
                });
            };

            rgc.OnMatchEnd += packet => {
                MainThread.Call(() =>
                {
                    removeOthers();
                    removePlayer();
                });
            };

            rgc.OnMatchExit += () => {
                MainThread.Call(() =>
                {
                    removeOthers();
                    removePlayer();
                });
            };

            rgc.OnOtherMatchExit += otherId => {
                MainThread.Call(() =>
                {
                    removeSingleOther(otherId);
                });
            };

            rgc.OnUdpClientClose += () =>
            {
                Debug.Log("udp disconnected.");

                MainThread.Call(() =>
                {
                    removeOthers();
                    removePlayer();
                });
            };

            rgc.OnMatchStart += packet =>
            {
                Debug.Log("OnMatchStart.");

                MainThread.Call(() =>
                {
                    spawnPlayer();
                });
            };

            rgc.OnMatchValidated += () =>
            {
                Debug.Log("OnMatchValidated.");
            };

            rgc.OnStatesUpdate += states =>
            {
                //Debug.Log("OnStatesUpdate.");

                MainThread.Call(() =>
                {
                    handleStates(states);
                });
            };
        }

        int now = (int)(Time.time * 1000);

        if (now - lastUpdated >= updateMs)
        {
            lastUpdated = now;
            if (rgc.IsInMatch())
            {
                if (Player != null && Player.transform != null)
                {
                    rgc.SendUdpStateUpdate(getStateUpdate());
                }
            }
        }
    }
 // Use this for initialization
 void Start()
 {
     rgc = this.initBalanceEngine();
 }