@echo off set PATH=perl;%PATH% perl -Iperl\lib start_on_windows.pl "script/project" pause #### [lib] [perl] [public] [script] [templates] [uploads] db.sqlite db.sqlite-shm db.sqlite-wal init.sql START.CMD start_on_windows.pl project.conf #### #!perl use Mojo::Base -strict; use Mojo::Server::Morbo; use Mojo::Util qw(extract_usage getopt); my $morbo = Mojo::Server::Morbo->new; my $pid = fork(); if ($pid) { # PARENT $morbo->daemon->listen(["http://*:3000"]); $morbo->run(shift); } else { # CHILD use Socket; my $host = 'localhost'; my $port = '3000'; my $timeout = 30; my $started = 0; while (not $started) { sleep 1; $started = portAlive(); } system("start http://localhost:3000"); exit; sub portAlive { my $proto = getprotobyname('tcp'); my $iaddr = inet_aton($host); my $paddr = sockaddr_in($port, $iaddr); socket(SOCKET, PF_INET, SOCK_STREAM, $proto) || warn "socket: $!"; eval { local $SIG{ALRM} = sub { die "timeout" }; alarm($timeout); connect(SOCKET, $paddr) || error(); alarm(0); }; if ($@) { close SOCKET || warn "close: $!"; print "$host is NOT listening on tcp port $port.\n"; return 0; } else { close SOCKET || warn "close: $!"; print "$host is listening on tcp port $port.\n"; return 1; } } } #### $r->websocket('/bus' => sub { my $self = shift; $self->inactivity_timeout(1000); #$log->debug(sprintf 'Client connected: %s', $self->tx); my $id = sprintf "%s", $self->tx; $clients->{$id} = $self->tx; $self->on(message => sub { my ($self, $msg) = @_; for (keys %$clients) { $clients->{$_}->send({json => {message => $msg}}); } }); $self->on(finish => sub { #$log->debug('Client disconnected'); delete $clients->{$id}; Mojo::IOLoop->timer(10 => sub { my $loop = shift; if (not scalar keys %{ $clients }) { #$log->debug('No clients! exiting!'); exit; } else { #$log->debug('Have clients! not exiting!'); } }); }); } );