Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watch files and send notification to browser via phpsocket.io #248

Open
enix-app opened this issue Oct 10, 2020 · 2 comments
Open

Watch files and send notification to browser via phpsocket.io #248

enix-app opened this issue Oct 10, 2020 · 2 comments

Comments

@enix-app
Copy link

I tried walkor/workerman-filemonitor and works, but I don't know how to send notification or message with walkor/phpsocket.io to browser.

Any snippet?
Thanks.

@walkor
Copy link
Owner

walkor commented Oct 12, 2020

Here is an example.
start.php

<?php

use Workerman\Worker;
use PHPSocketIO\SocketIO;
use Workerman\Timer;

// Composer autoload
include 'vendor/autoload.php';
$io = new SocketIO(2020);

$io->on('workerStart', function($socket) use ($io) {
    // watch Applications catalogue
    $monitor_dir = realpath(__DIR__.'/yourdir');
    Timer::add(1, 'check_files_change', array($monitor_dir));

});

// check files func
function check_files_change($monitor_dir)
{
    global $last_mtime, $io;
    // recursive traversal directory
    $dir_iterator = new RecursiveDirectoryIterator($monitor_dir);
    $iterator = new RecursiveIteratorIterator($dir_iterator);
    foreach ($iterator as $file)
    {
        // only check php files
        if(pathinfo($file, PATHINFO_EXTENSION) != 'php')
        {
            continue;
        }
        // check mtime
        if($last_mtime < $file->getMTime())
        {
            echo $file." update\n";
            $last_mtime = $file->getMTime();
            $io->emit('file_update', (string)$file);
            break;
        }
    }
}

Worker::runAll();

Client side codes.

io = io('http://127.0.0.1:2020');
io.on('file_update', function(data){console.log(data);});

@enix-app
Copy link
Author

Cool and works! Big thanks!

@enix-app enix-app changed the title Watch files and send notification to browser via phpsocket.io? Watch files and send notification to browser via phpsocket.io Oct 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants