File: event.pl

package info (click to toggle)
libprotocol-websocket-perl 0.26-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 428 kB
  • sloc: perl: 3,579; makefile: 10
file content (40 lines) | stat: -rw-r--r-- 670 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env perl

use strict;
use warnings;

use Event qw(time);
require Event::io;
use IO::Socket::INET;

my $socket = IO::Socket::INET->new(
    LocalAddr => 'localhost',
    LocalPort => 3000,
    Listen    => 1,
    Blocking  => 0
);

$socket->blocking(0);

Event->io(
    fd      => $socket,
    timeout => 0.1,
    poll    => "r",
    repeat  => 1,
    cb      => sub {
        my $e   = shift;
        my $got = $e->got;

        if ($got eq "r") {
            sysread(STDIN, my $buf, 80);
            chop $buf;

            my $len = length($buf);
            Event::unloop if !$len;

            print "read[$len]:$buf:\n";
        }
    }
);

Event::loop;