Add a ZMQ perl exmple

This commit is contained in:
Ian Norton 2017-10-11 12:18:58 -04:00
parent d1db9a8e3e
commit dcd4a8c54c

38
examples/Perl5/client.pl Normal file
View File

@ -0,0 +1,38 @@
#!/usr/bin/env perl
use JSON::XS 'decode_json';
use ZMQ::FFI qw(ZMQ_SUB);
use Time::HiRes q(usleep);
use Compress::Zlib;
sub msg {
printf STDERR @_;
}
my $endpoint = "tcp://eddn.edcd.io:9500";
my $ctx = ZMQ::FFI->new();
my $s = $ctx->socket(ZMQ_SUB);
$s->connect($endpoint);
$s->subscribe('');
while(1)
{
usleep 100_000 until ($s->has_pollin);
my $data = $s->recv();
# turn the json into a perl hash
my $pj = decode_json(uncompress($data));
my $schema = $pj->{'$schemaRef'};
msg "schema = %s\n", $schema;
msg " software = %s\n", $pj->{header}->{softwareName};
if ($schema eq "https://eddn.edcd.io/schemas/journal/1") {
my $event = $pj->{message}->{event};
if ($event eq "FSDJump") {
msg " StarSystem = %s\n", $pj->{message}->{StarSystem};
}
}
msg "------\n";
}
$s->unsubscribe('');