C# WebSockets and JavaScript sync time

I have events which occur in a C# based application which run on a timer. However, I can’t seem to ever get both server and clients time to sync with one another to show a count down to which the event is going to end.

I am used web sockets to communicate with the client. Here are some of the things which I have done in attempt to achieve a sync but either the client or server is out of sync as timer either finishes before or after rarely ever at the same time.

Sending time in second

I send a simply message like start-timer;100 which triggers 100 seconds. Then I use something like this

function setTimer(type, expire)
        {
            if(cycleTimers[type] == undefined) {
                cycleTimers[type] = {};
            }

            var d = new Date();
            d.setSeconds(d.getSeconds() + expire);

            cycleTimers[type].time = d;
            if(cycleTimers[type].interval != undefined) {
                clearInterval(cycleTimers[type].interval);
            }

            cycleTimers[type].interval = setInterval(function() {

                var time = Math.abs((new Date() - cycleTimers[type].time) / 1000);

                var minutes = ("00" + Math.floor(time / 60)).slice(-2);
                var seconds = ("00" + Math.floor(time % 60)).slice(-2);

                $(".timer-cycle").html(minutes + ":" + seconds);
                $(".timer-cycle").show();

                if(time <= 0) {
                    clearInterval(cycleTimers[type].interval);
                    $(".timer-cycle").hide();
                }
            }, 500);
        }

The reason for type, is that in the end I would like to run multiple timers and show the time left on the clients screen. However, for purpose of this example it isn’t needed.

Time in seconds with interval

I have send the time in seconds above but instead of converting it to a date I attempted to just use an interval of 1000 and decrease the seconds remaining by one on each cycle. This didn’t work either.

Unix time stamp

I also attempted to convert date time from c# to a timestamp and send it to client for expire time but they were completely different to one another.

C#

this.ExpireTime = DateTime.UtcNow.AddSeconds(Seconds);
socket.SendTimer(Seconds);

I run a task which executes every 500ms to check if this.Expired is less than current date if so then stop cycle. This works fine on emulator as I write to console the time at each tick.


Source: stackoverflow-javascript

Node.js doesn’t serve page on proxy pass from apache

I’ve found similar questions on Stack and other forums but no working answers.
I have a working node.js chat server running on port 3000 of my local machine. I have Apache running on port 80. Both work great. When I go to localhost in my address bar I get Apache. When I type localhost:3000 I get the nodes app. However, when I set up a ProxyPass to make the nodejs app run on localhost/node the page loads, but the server isn’t processing the page. It renders but node doesn’t work.

<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName localhost
ProxyPass /node http://localhost:3000/
ProxyPassReverse /node http://localhost:3000/
</VirtualHost>

This works, but I don’t want to send ALL requests to nodes

<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName localhost
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
</VirtualHost>


Source: stackoverflow-javascript