Skip to content

Instantly share code, notes, and snippets.

Created October 29, 2015 23:40
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 anonymous/e920a7f9dcedc8f50cfb to your computer and use it in GitHub Desktop.
Save anonymous/e920a7f9dcedc8f50cfb to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use Mojo::UserAgent;
use Mojo::IOLoop;
use POSIX;
my $args = @ARGV;
if ($args != 3) {
print "Enough arguments not supplied.\n";
print "Usage: $0 <tid> <aid> <selist>\n";
exit(0);
}
my $tid = shift;
my $aid = shift;
my $selist = shift;
my @SEList = split(',',$selist);
my $ua = Mojo::UserAgent->new;
print "Initializing and Connecting Websocket\n";
$ua->websocket('ws://127.0.0.1/ws/upload_data?publish-broadcast' => ['v1.proto'] =>
sub {
my ($ua, $tx) = @_;
unless ($tx->is_websocket) {
print "Websocket Handshake failed\n";
return;
} else {
print "Websocket connected\n";
}
$tx->on(finish =>
sub {
my ($tx, $code, $reason) = @_;
print "Websocket Closed with Code (Reason): $code\n";
});
$tx->on(message =>
sub {
# This client never receives anything from the server, so
# Discard any incoming messages
});
$tx->on(drain =>
sub {
print "JSON Data Sent.\n";
Mojo::IOLoop->stop;
});
print "Starting master data loop\n";
foreach my $i (1..10) {
my $ts = POSIX::strftime("%Y%m%d%H%M%S", localtime);
foreach my $se (@SEList) {
my $kpi1 = sprintf "%.2f", rand(100);
my $json = '{"test_id":"' . $tid . '","a_id":"' . $aid . '","se_id":"' . $se . '","timestamp":"' . $ts . '","payload":{"kpi1":"' . $kpi1 . '"}}';
print "JSON String: $json\nSending Json Data\n";
$tx->send($json);
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
sleep(5);
}
}
print "Terminating WS connection\n";
$tx->finish;
print "Ending script\n";
});
# Start event loop if necessary
print "Starting Event Loop\n";
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment