suhijo:

I've not used EV, and don't think I've used WebSockets in perl (other than a trivial demo). I looked over your code a bit anyway, and while comparing it with the documentation, it seems to me that one problem you may be having is the bit where you're trying to send the message to all your websockets.

Since @users already contains your array of websockets, I think you need to change:

sub buscar_exten{ my $exten=@_[0]; my $href; my $role; my $some; print "buscando si <$exten> esta logueada-\n"; foreach my $fn (@users) { open my $fh, ">&=$fn" ;# or warn $! and die; send($fh,"evento llamada $exten\n",0) ; print "}\n"; } }

Into something more like:

sub buscar_exten{ my $exten=@_[0]; print "buscando si <$exten> esta logueada-\n"; my $cnt=0; for my $websock (@users) { print "Sending message to socket in slot $cnt\n"; ++$cnt; $websock->queue_msg("evento llamada $exten\n"); } }

You'll also want to remove the websocket from your @users list when the websocket closes. Perhaps something like:

on_close => sub { my($code) = @_; # remove websocket from active list @users = grep { $_ ne $cgi } @users; $cgi->{self} = undef; $cgi = undef; },

(In fact, the whole $cgi->{self} bit seems to exist simply to hold the websocket open, which ought to be handled just fine by @users, so you may be able to remove the $cgi->{self} creation and clearing bits.

Take my notes with a rather large grain of salt: I've only used WebSockets a little (in C++), but it seems odd to me that you're doing an authentication check inside the websocket's message received hook, rather than checking the authentication before bothering to create the WebSocket.

...roboticus

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


In reply to Re^5: Perl an array of sockets by roboticus
in thread Perl an array of sockets by suhijo

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.