in reply to Mojolicious websocket question
when I run this with morbo, it starts up fine, but when I access it with the web browser, I get:#!/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>
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.
|
|---|