Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Completely untested, this should be close:

#!/usr/bin/perl use strict; use warnings; use threads: + ## Added use threads::shared; + ## Added use Data::Dumper; use Net::WebSocket::Server; use JSON; my %data; my $json :shared; + ## Modded async { + ## Added Net::WebSocket::Server->new( listen => 8080, on_connect => sub { my ($serv, $conn) = @_; $conn->on( handshake => sub { my ($conn, $handshake) = @_; }, utf8 => sub { my ($conn) = @_; lock $json; + ## Added $_->send_utf8($json) for $conn->server->connection +s; }, ); }, )->start; }->detach; + ## Added open my $fh, '<', 'toto1' or die "can't open fifo toto1: $?"; while ( my $line = <$fh> ) { my @list = ( split /\s+/, $line ); # skip empty lines next if $#list <= 1; shift @list if $list[0] eq ''; # test for iostat header next if $list[0] eq 'avg-cpu:'; next if $list[0] eq 'Device:'; # test for vmstat header next if $list[0] eq 'procs'; if ( $#list == 11 ) { ( undef, $data{io}{ $list[0] }{rrqms}, $data{io}{ $list[0] }{wrqms}, $data{io}{ $list[0] }{reads}, $data{io}{ $list[0] }{writes}, $data{io}{ $list[0] }{rsecs}, $data{io}{ $list[0] }{wsecs}, $data{io}{ $list[0] }{avgrqsz}, $data{io}{ $list[0] }{avgqusz}, $data{io}{ $list[0] }{await}, $data{io}{ $list[0] }{svctm}, $data{io}{ $list[0] }{util} ) = @list; } elsif ( $#list == 15 ) { ( $data{vm}{r}, $data{vm}{b}, $data{vm}{swpd}, $data{vm}{free}, $data{vm}{buff}, $data{vm}{cache}, $data{vm}{swapin}, $data{vm}{swapout}, $data{vm}{ioin}, $data{vm}{ioout}, $data{vm}{sysin}, $data{vm}{syscs}, $data{vm}{user}, $data{vm}{sys}, $data{vm}{idle}, $data{vm}{wait} ) = @list; } elsif ( $#list == 5 ) { $data{vm}{nice} = $list[1]; } { ## + Added lock $json; ## + Added $json = encode_json( \%data); } ## + Added sleep 1; }

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Simultaneously reading from a file and serving data by BrowserUk
in thread Simultaneously reading from a file and serving data by wazoox

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2022-05-29 08:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (101 votes). Check out past polls.

    Notices?