Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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.

    ...roboticus

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

      Many thanks

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2017-12-14 15:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (396 votes). Check out past polls.

    Notices?