Jump to content

Stopwatch (server side) to see from severals clients


elsafraslastra

Recommended Posts

Hello, I need a code to create a stopwatch (on the server side with start, stop and pause) to be able to be seen by several clients in real time while it is running (only see it without being able to stop, start  or pause).

 

                                                                                                                                                           Thank you so much.

Edited by elsafraslastra
Link to comment
Share on other sites

If you want the clock to run on the server and show the time on each client that is connected I don't know how PHP can do this for you since it doesn't run on the clients and the clock (as said) is on the server.

Getting the picture?  If the clock ticks one second by, how does it update each (or any) client?  Sounds like a lot of bandwidth for this app - sending time updates every second to every client.

How about posting a new topic relative to how to display a clock to a user?

Link to comment
Share on other sites

PS to my previous.  No doubt the answer to a new topic (as suggested) will include some JS that will operate on a timer function that uses ajax to keep up with a server-side time.   Just a thought that I haven't dug into but it seems logical.

Link to comment
Share on other sites

I would probably look into using WebSockets for this if possible.  Have the clients connect to a WebSocket server and obtain the initial time to display.  Each client handle updating the display itself using some local timing, potentially with a periodic sync.  Whenever the admin pauses/starts the timer, broadcast that event + the current time to each client so they can pause/start their display and sync it.

 

  • Like 1
Link to comment
Share on other sites

You could do a PHp script to connect to the server every 30 seconds, and then use a javascript time/clock script to update every second. the PHP connects will be to get the current/latest tier details.

 

Though, you didn't say how long this timer will go on for ? if its a few minutes or less, the above may not be useful. If its longer - it might. 

Link to comment
Share on other sites

On 6/8/2023 at 10:08 AM, elsafraslastra said:

Hello, I need a code to create a stopwatch (on the server side with start, stop and pause) to be able to be seen by several clients in real time while it is running (only see it without being able to stop, start  or pause).

 

                                                                                                                                                           Thank you so much.

Game designers/developers do that all the time with a countdown timer - example -> https://www.phototechguru.com/trivia.php. though the server side would be a little tricky if it is automated. Like someone stated websockets might be worth looking into if that is the case?

Edited by Strider64
Link to comment
Share on other sites

On 6/8/2023 at 12:19 PM, elsafraslastra said:

:happy-03:

I mean that it could be two pages, one with the buttons enabled (admin) and the other only showing the elapsed time (client). And transfer from one page to another the value of the variables. Do you think it will be possible?

Yes, and as others have already stated, you need 3 things:

  • Client javascript to run the countdown UI
  • A publish/subscribe feature
  • A persistent bi-directional socket connection between client and server

HTTP 1.x protocol, by itself is not designed for persistent connections.  When a client sends an HTTP Get request to a server, the server responds with content (usually the HTML of the web page) and the connection to the server is closed.  HTTP is not bi-directional nor persistent.

In an application like this, you need some way around the way HTTP 1 was designed to work.  This is why multiple people have suggested that an application like this should use websockets. Websockets is an alternative protocol designed for persistence.  Since Websocket protocol was developed, HTTP 2.x was released, which overlaps to a degree with websockets.  

The problem with websocket protocol, is that, while it is relatively simple and has both client javascript support, as well as PHP support, from the serverside, you need to run the websocket server in addition to your web server, which adds a degree of complication that can be difficult for someone without a lot of system administration experience to retrofit.  

The main options for building a PHP based websocket server are:

I'd suggest you take a look at these, and perhaps experiment with some of the examples provided.  In terms of publish/subscribe, your control client will publish the sync/start/stop/pause, and the other clients subscribe to these events.

I will say that this is a non-trivial application, with a lot of design elements to think about.

Link to comment
Share on other sites

I can help

This is the code

<?php
session_start();

// Initialize session variables
if (!isset($_SESSION['start_time'])) {
    $_SESSION['start_time'] = 0;
    $_SESSION['pause_time'] = 0;
    $_SESSION['total_pause_time'] = 0;
}

// Start the stopwatch
if (isset($_POST['start'])) {
    $_SESSION['start_time'] = time();
    $_SESSION['pause_time'] = 0;
    $_SESSION['total_pause_time'] = 0;
}

// Pause the stopwatch
if (isset($_POST['pause'])) {
    $_SESSION['pause_time'] = time();
}

// Resume the stopwatch
if (isset($_POST['resume'])) {
    $_SESSION['total_pause_time'] += time() - $_SESSION['pause_time'];
    $_SESSION['pause_time'] = 0;
}

// Stop the stopwatch
if (isset($_POST['stop'])) {
    $_SESSION['start_time'] = 0;
    $_SESSION['pause_time'] = 0;
    $_SESSION['total_pause_time'] = 0;
}

// Calculate the elapsed time
function getElapsedTime()
{
    if ($_SESSION['start_time'] > 0) {
        $elapsed_time = time() - $_SESSION['start_time'] - $_SESSION['total_pause_time'];
        return $elapsed_time;
    }
    return 0;
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Stopwatch</title>
</head>
<body>
    <h1>Stopwatch</h1>
    <h2><?php echo gmdate("H:i:s", getElapsedTime()); ?></h2>

    <form method="POST">
        <?php if ($_SESSION['start_time'] == 0) { ?>
            <button type="submit" name="start">Start</button>
        <?php } else { ?>
            <?php if ($_SESSION['pause_time'] == 0) { ?>
                <button type="submit" name="pause">Pause</button>
            <?php } else { ?>
                <button type="submit" name="resume">Resume</button>
            <?php } ?>
            <button type="submit" name="stop">Stop</button>
        <?php } ?>
    </form>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.