Skip to content

Instantly share code, notes, and snippets.

@jhthorsen
Created August 4, 2014 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhthorsen/076157063b4bdaa47a3f to your computer and use it in GitHub Desktop.
Save jhthorsen/076157063b4bdaa47a3f to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use Mojo::Base -strict;
use Mojo::IOLoop;
Mojo::IOLoop->server({ port => 9934 }, sub {
my ($ioloop, $stream) = @_;
my ($disconnect, $id);
$disconnect = sub {
warn "client disconnected\n";
Mojo::IOLoop->remove($id); # clean up recurring timer
};
$stream->on(close => $disconnect);
$stream->on(error => $disconnect);
$stream->on(read => sub { $_[0]->write($_[1]); });
$id = Mojo::IOLoop->recurring(5 => sub { $stream->write("ping!\n"); });
});
Mojo::IOLoop->start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment