thanks for the replies - here's an example of a Mojolicious::Lite app that is not working for me. This is based on the examples I mentioned, but likley I'm the one who broke it, of course:

#!/usr/bin/perl use Mojolicious::Lite; use ZMQ; use ZMQ::Constants qw|ZMQ_PUB ZMQ_SUB ZMQ_SUBSCRIBE ZMQ_FD ZMQ_DONTWAI +T ZMQ_NOBLOCK|; use strict; # Template with browser-side code get '/' => 'index'; # WebSocket echo service websocket '/msgs' => sub { my $c = shift; # Opened $c->app->log->debug('WebSocket opened'); # Increase inactivity timeout for connection a bit $c->inactivity_timeout(300); my $ctx = ZMQ::Context->new(10); my $subsocket = $ctx->socket(ZMQ_SUB); $subsocket->setsockopt(ZMQ_SUBSCRIBE,'event'); $subsocket->connect ('tcp://127.0.0.1:5555'); my $subsocket_fd = IO::Handle->new_from_fd($subsocket->getsockopt(ZM +Q_FD),'r'); Mojo::IOLoop->singleton->reactor->io($subsocket_fd=> sub { my ( $r, $w ) = @_; if (my $message = $subsocket->recvmsg(ZMQ_DONTWAIT)) { $c->send($message->data); } undef $w; } ); Mojo::IOLoop->start unless Mojo::IOLoop->is_running; }; app->start; __DATA__ @@ index.html.ep <!DOCTYPE html> <html> <head><title>Echo</title></head> <body> <script> var ws = new WebSocket('<%= url_for('msgs')->to_abs %>'); ws.onmessage = function (msg) { document.body.innerHTML += msg.data + '<br/>'; }; </script> msgs<br> </body> </html>
when I run this with morbo, it starts up fine, but when I access it with the web browser, I get:

Assertion failed: pfd.revents & POLLIN (signaler.cpp:242)

in the console, and the browser complains that

WebSocket connection to 'ws://myserver:3000/msgs' failed: Connection closed before receiving a handshake response

I've tried a bunch of variations of this, putting the IO loop in different places, using AnyEvent instead of Mojo::IOLoop, using ZMQ::FFI instead of ZMQ, etc., but all essentially with the same result.


In reply to Re: Mojolicious websocket question by antichef
in thread Mojolicious websocket question by antichef

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.