Snippets
Created by
Brian Medley
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | use Mojolicious::Lite;
use DBI;
use PerlIO::via::DBLog;
use DBLog;
use DateTime;
use 5.20.0;
use experimental 'signatures';
helper db => sub {
my $self = shift;
return $self->{dbh} if $self->{dbh};
my $dsn = "dbi:SQLite:dbname=joy";
$self->stash("dblog") // $self->stash("dblog", DBLog->new($self->req_count));
open(my $fh, '>:via(DBLog)', $self->stash("dblog")) or die;
my $dbh = DBI->connect($dsn);
$dbh->trace('SQL', $fh);
$self->stash("fh", $fh);
return $self->{dbh} = $dbh;
};
helper req_count => sub {
state $count = 0;
++$count;
};
helper print_log => sub {
my $self = shift;
my $text = '';
my $dblog = $self->stash("dblog");
foreach my $log(@{$dblog->{queries}}) {
$text = $text . "$log\n";
}
return $text;
};
get '/' => 'index';
my $clients = {};
websocket '/echo' => sub {
my $self = shift;
app->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) = @_;
my $dt = DateTime->now( time_zone => 'Asia/Tokyo');
my $sth = $self->db->prepare("INSERT INTO test (name) VALUES (?)");
$sth->execute($msg);
if ("timings" eq $msg) {
for (keys %$clients) {
$clients->{$_}->send({json => {
hms => $dt->hms,
text => $self->print_log,
}});
}
return;
}
for (keys %$clients) {
$clients->{$_}->send({json => {
hms => $dt->hms,
text => $msg,
}});
}
});
$self->on(finish => sub {
app->log->debug('Client disconnected');
delete $clients->{$id};
});
};
app->db->do("CREATE TABLE IF NOT EXISTS test (name VARCHAR NOT NULL, quantity INTEGER NOT NULL DEFAULT 0)");
app->start;
__DATA__
@@ index.html.ep
<html>
<head>
<title>WebSocket Client</title>
<script
type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"
></script>
<script type="text/javascript" src="ws.js"></script>
<style type="text/css">
textarea {
width: 40em;
height:10em;
}
</style>
</head>
<body>
<h1>Mojolicious + WebSocket</h1>
<p><input type="text" id="msg" /></p>
<textarea id="log" readonly></textarea>
</body>
</html>
@@ ws.js
$(function () {
$('#msg').focus();
var log = function (text) {
$('#log').val( $('#log').val() + text + "\n");
};
var ws = new WebSocket('ws://localhost:3000/echo');
ws.onopen = function () {
log('Connection opened');
};
ws.onmessage = function (msg) {
var res = JSON.parse(msg.data);
log('[' + res.hms + '] ' + res.text);
};
$('#msg').keydown(function (e) {
if (e.keyCode == 13 && $('#msg').val()) {
ws.send($('#msg').val());
$('#msg').val('');
}
});
});
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.