Пример #1
0
        public async Task ConnectToHub()
        {
            await SetOnbeforeunload();

            // 44399 DEVELOPMENT(IIS)
            // 443 PROD o DEV BlazorPong Env(Forced Https)
            var endpoint = GameHubEndpoint.Equals("rel") ? $"{NavigationManager.BaseUri}gamehub" : GameHubEndpoint;

            Connection = new HubConnectionBuilder()
                         .WithUrl(endpoint, _connectionTypeChoice)
                         .WithAutomaticReconnect()
                         .Build();

            Connection.On <GameObject>("UpdateGameObjectPositionOnClient", UpdateGameObjectPositionOnClient);
            Connection.On <Enums.ClientType, int>("UpdatePlayerPoints", UpdatePlayerPoints);
            Connection.On <string>("UpdateGameMessage", UpdateGameMessage);

            await LogOnClient("State: " + Connection.State.ToString() + "Type:" + _connectionTypeChoice.ToString());

            await Connection.StartAsync();

            await LogOnClient("State: " + Connection.State.ToString() + "Type:" + _connectionTypeChoice.ToString());

            await LogOnClient("ConnectionId: " + Connection.ConnectionId.ToString());

            // Ricavo che tipo di player sono(1, 2 o spettatore)
            _playerType = await Connection.InvokeAsync <Enums.ClientType>("GetClientType");

            await LogOnClient("Player type:" + _playerType.ToString());

            switch (_playerType)
            {
            case Enums.ClientType.Player1:
                PlayerTypeMessage = "You are Player1";
                break;

            case Enums.ClientType.Player2:
                PlayerTypeMessage = "You are Player2";
                break;

            default:
                PlayerTypeMessage = "You are a Spectator";
                break;
            }

            GetOrInitializeGameObjects();

            await LogOnClient("GameObjects initialization completed.");

            // Ogni decimo di secondo controlliamo se necessario fare l'update delle collisioni al server e in caso lo mandiamo
            // Iniziamo un secondo dopo l'inizializzazione del timer
            _updateServerTimer = new Timer(UpdateServer, null, 1000, 10);

            await LogOnClient("Timer Started!");
        }
Пример #2
0
        private Task UpdatePlayerPoints(Enums.ClientType clientType, int points)
        {
            switch (clientType)
            {
            case Enums.ClientType.Player1:
                Player1Points = points;
                GameMessage   = "Player1 just made a point!";
                break;

            case Enums.ClientType.Player2:
                Player2Points = points;
                GameMessage   = "Player2 just made a point!";
                break;
            }

            StateHasChanged();

            return(Task.CompletedTask);
        }