123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- using System.Collections.Generic;
- using System.Threading;
- using System.Threading.Tasks;
- using BubbleSocketCore.Websocket;
- using Microsoft.Extensions.Hosting;
- namespace BubbleSocketCore.Simulation
- {
- class TickService : IHostedService
- {
- private Timer simulationTimer;
- public static int simulationTickRateInMs { get; private set; } = 333;
- private Timer broadcastTimer;
- private int broadcastTickRateInMs = 333;
- public Task StartAsync(CancellationToken cancellationToken)
- {
- simulationTimer = new Timer(o => SimulationUpdate(), null, System.TimeSpan.Zero, System.TimeSpan.FromMilliseconds(simulationTickRateInMs));
- broadcastTimer = new Timer(o => BroadcastUpdate(), null, System.TimeSpan.Zero, System.TimeSpan.FromMilliseconds(broadcastTickRateInMs));
- return Task.CompletedTask;
- }
- public Task StopAsync(CancellationToken cancellationToken)
- {
- simulationTimer.Change(Timeout.Infinite, 0);
- broadcastTimer.Change(Timeout.Infinite, 0);
- return Task.CompletedTask;
- }
- public async void SimulationUpdate()
- {
- // Simulation.GetInstance().AdvanceSimulation();
- await Task.CompletedTask;
- }
- public async void BroadcastUpdate()
- {
- var payload = new List<object>();
- payload.Add("tick");
- ConnectionManager.GetInstance().GetActive().ForEach(connection =>
- {
- connection.SendAsync(SimulationActionType.Resync, payload, CancellationToken.None);
- });
- await Task.CompletedTask;
- }
- }
- }
|