#!/usr/bin/perl use strict; use v5.10.1; use Mojolicious::Lite; use Loop; use Debug; use SMGLog; $SIG{TERM} = $SIG{INT} = sub {++$Loop::ending; Mojo::IOLoop->stop;}; $SIG{HUP} = 'IGNORE'; # WebSocket weather service websocket '/weather' => sub { my $c = shift; my $msg = shift; my $tx = $c->tx; # Opened app->log->debug('WebSocket opened.'); dbg 'WebSocket opened' if isdbg 'chan'; $WS->{$tx} = $tx; # send historical data $c->send($ld->{lasthour_h}) if exists $ld->{lasthour_h}; $c->send($ld->{lastmin_h}) if exists $ld->{lastmin_h}; # disable timeout $c->inactivity_timeout(3615); # Incoming message $c->on( message => sub { my ($c, $msg) = @_; dbg "websocket: text $msg" if isdbg 'chan'; }, json => sub { my ($c, $msg) = @_; dbg "websocket: json $msg" if isdbg 'chan'; } ); # Closed $c->on(finish => sub { my ($c, $code, $reason) = @_; app->log->debug("WebSocket closed with status $code."); dbg "websocket closed with status $code" if isdbg 'chan'; delete $WS->{$tx}; }); }; get '/' => {template => 'index'}; dbginit(); if (@ARGV) { dbgadd(@ARGV); } dbgadd('chan'); dbg '***'; dbg "*** starting $0"; dbg '***'; exit 0;