public void Pong(Guid pingId, long clientTimeSent) { //simulate up latency here //System.Threading.Thread.Sleep(2000); var serverTimeReceived = DateTime.UtcNow; var ping = Pings[pingId]; var clientPing = TimeSyncMessage.NewFrom(ping); clientPing.ConnectionId = Context.ConnectionId; clientPing.ClientTimeSent = JavaScriptDateConverter.Convert(clientTimeSent); clientPing.ServerTimeReceived = serverTimeReceived; Pongs.AddOrUpdate(clientPing.ConnectionId, c => clientPing, (c, old) => { if (old.Roundtrip > clientPing.Roundtrip) { //this was more accurate...use it return(clientPing); } else { return(old); } }); ShowPings(); }
public static TimeSyncMessage NewFrom(TimeSyncMessage message) { return(new TimeSyncMessage { PingId = message.PingId, ConnectionId = message.ConnectionId, ServerTimeSent = message.ServerTimeSent, ClientTimeSent = message.ClientTimeSent, ServerTimeReceived = message.ServerTimeReceived }); }
public static void StartPing_Static() { var ping = new TimeSyncMessage { PingId = Guid.NewGuid(), ServerTimeSent = DateTime.UtcNow }; //simulate down latency here //System.Threading.Thread.Sleep(2000); var hub = GlobalHost.ConnectionManager.GetHubContext <TimeSync>(); hub.Clients.All.timeSyncRequest(ping); Pings.AddOrUpdate(ping.PingId, k => ping, (k, old) => ping); }