Snippets

Brian Medley circ-user-Sar4c websocket question

Created by Brian Medley
use Mojo::Base -strict;

use Mojo::UserAgent;
use Mojo::IOLoop;

my $url = 'ws://localhost:3000/ws';
my $ua = Mojo::UserAgent->new;

$ua->websocket($url => sub {
    my ($ua, $tx) = @_;

    my $i = 1;
    my $id = Mojo::IOLoop->recurring(5 => sub {
        warn("HI: $i");
        my $json = '{"test_id":"10","a_id":"20","se_id":"30","timestamp":"20151029194500","payload":{"kpi1":"23.45"}}';

        $tx->send($json);

        $tx->finish if $i++ == 5;
    });

    # Stop recurring timer
    $tx->on(finish => sub { Mojo::IOLoop->remove($id) });
});

Mojo::IOLoop->start;

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.