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

Automatically close connection after xxx seconds if no data is sent? #42

Closed
lbngoc opened this issue Jun 29, 2016 · 4 comments
Closed
Labels

Comments

@lbngoc
Copy link

lbngoc commented Jun 29, 2016

How can I do it with React\Socket\Server ?

@WyriHaximus
Copy link
Member

WyriHaximus commented Jun 29, 2016

The loop has timer methods you can use to set timeouts: https://github.com/reactphp/event-loop#usage and https://blog.wyrihaximus.net/2015/01/reactphp-timers/

@lbngoc
Copy link
Author

lbngoc commented Jun 29, 2016

I tried with this code:

$loop = React\EventLoop\Factory::create();

$socket = new React\Socket\Server($loop);
$socket->on('connection', function ($conn) {

    $timer = React\EventLoop\Factory::create();
    $timer->addTimer(10, function() use ($conn) {
        echo 'Timer done', PHP_EOL;
        $conn->end();
    });
    $timer->run();

    $conn->on('data', function ($data) use ($conn) {
        ...
    });
});

but then I faced issue #37 :(

@WyriHaximus
Copy link
Member

Try this:

$loop = React\EventLoop\Factory::create();

$socket = new React\Socket\Server($loop);
$socket->on('connection', function ($conn) use ($loop) {
    $func = function () use ($conn) {$conn->close();};
    $timer = $loop->addTimer(1, $func);
    $conn->on('data', function ($data) use ($loop, &$timer, $func) {
       $timer->cancel();
       $timer = $loop->addTimer(1, $func);
    });
});

@clue clue added the question label Nov 13, 2016
@clue
Copy link
Member

clue commented Nov 13, 2016

Thanks @WyriHaximus! I believe this has been answered, so I'm closing this for now. Please come back with more details if this problem persists and we can reopen this 👍

@clue clue closed this as completed Nov 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants