Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

AnyEvent::WebSocket::Server get all connections

by Black Vagrant (Novice)
on Oct 11, 2017 at 11:44 UTC ( #1201155=perlquestion: print w/replies, xml ) Need Help??
Black Vagrant has asked for the wisdom of the Perl Monks concerning the following question:

Good day, how i can get all connected clients to my websocket server for send "global" message For example:
my $server = AnyEvent::WebSocket::Server->new(); my $tcp_server = tcp_server undef, 3000, sub { my ($fh) = @_; $server->establish($fh)->cb( sub { my $connection = eval { shift->recv }; ... $connection->on( each_message => sub { my ($connection, $message) = @_; # how i can get all connections # and then send message to all clients? foreach my $connection (@all_connections) { $connection->send($message) or die $!; } } ); ... } ); };

Replies are listed 'Best First'.
Re: AnyEvent::WebSocket::Server get all connections
by roboticus (Chancellor) on Oct 11, 2017 at 12:44 UTC

    Black Vagrant:

    I've not used AnyEvent::WebSocket::Server before, but a brief review of the documentation suggests that you should be able to add your connections to a structure when you create them and remove them when the connection closes, something like this:

    . . . # My box o' global connections my @cnx; . . . $server->establish($fh)->cb( sub { my $connection = eval { shift->recv }; # Add new connection to the box push @cnx, $connection; # Remove connection when it closes $connection->on(finish => sub { @cnx = grep { $_ ne $connection } @cnx; } . . . } ); . . . sub send_global_message { my $msg = shift; for my $c (@cnx) { $c->send($msg); } }

    Note: standard warranty applies--Untested, if broken you can keep both pieces, can cause hair loss, impotence, edema, etc.


    When your only tool is a hammer, all problems look like your thumb.

      Many thanks

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1201155]
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2018-06-22 21:48 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.