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
|
#!./uwsgi --http-socket :9090 --coroae 100 --psgi tests/websockets_chat.pl
use Coro::AnyEvent;
use AnyEvent::Redis;
# Coro::AnyEvent uWSGI/PSGI websocket app
# you can build a Coro::AnyEvent-enabled uWSGI binary with 'make coroae'
# note: we need two redis connections, one for publishing and one for subscribing
my $app = sub {
my ($env) = @_;
my $ws_scheme = 'ws';
$ws_scheme = 'wss' if ($env->{'HTTPS'} || $env->{'wsgi.url_scheme'} eq 'https');
if ($env->{'PATH_INFO'} eq '/') {
my $host = $env->{'HTTP_HOST'};
my $html = <<EOF;
<html>
<head>
<script language="Javascript">
var s = new WebSocket("$ws_scheme://$host/foobar/");
s.onopen = function() {
alert("connected !!!");
s.send("ciao");
};
s.onmessage = function(e) {
var bb = document.getElementById('blackboard')
var html = bb.innerHTML;
bb.innerHTML = html + '<br/>' + e.data;
};
s.onerror = function(e) {
alert(e);
}
s.onclose = function(e) {
alert("connection closed");
}
function invia() {
var value = document.getElementById('testo').value;
s.send(value);
}
</script>
</head>
<body>
<h1>WebSocket</h1>
<input type="text" id="testo"/>
<input type="button" value="invia" onClick="invia();"/>
<div id="blackboard" style="width:640px;height:480px;background-color:black;color:white;border: solid 2px red;overflow:auto">
</div>
</body>
</html>
EOF
return [200, ['Content-Type' => 'text/html'], [$html]];
}
elsif ($env->{'PATH_INFO'} eq '/favicon.ico') {
return [404, [], []];
}
elsif ($env->{'PATH_INFO'} eq '/foobar/') {
# when 1 something was wrong and we need to close the connection
my $error = 0;
# when 1 there is something in the websocket
my $websocket_event = 0;
# when defined there is a redis message available
my $redis_message = undef;
# do the handshake
uwsgi::websocket_handshake($env->{'HTTP_SEC_WEBSOCKET_KEY'}, $env->{'HTTP_ORIGIN'});
print "websockets...\n" ;
# this condvar will allow us to wait for events
my $w = AnyEvent->condvar;
# connect to redis
my $redis = AnyEvent::Redis->new(
host => '127.0.0.1',
port => 6379,
encoding => 'utf8',
on_error => sub { warn @_; $error = 1; $w->send; },
on_cleanup => sub { warn "Connection closed: @_"; $error = 1; $w->send; },
);
# subscribe to the 'foobar' channel
$redis->subscribe('foobar', sub {
my ($message, $channel) = @_;
$redis_message = $message;
# wakeup !!!
$w->send;
});
# open a second connection to publish messages
my $redis_publisher = AnyEvent::Redis->new(
host => '127.0.0.1',
port => 6379,
encoding => 'utf8',
on_error => sub { warn @_; $error = 1; $w->send; },
on_cleanup => sub { warn "Connection closed: @_"; $error = 1; $w->send; },
);
# start waiting for read events in the websocket
my $ws = AnyEvent->io( fh => uwsgi::connection_fd, poll => 'r', cb => sub {
$websocket_event = 1;
# wakeup !!!
$w->send;
}
);
# the main loop
for(;;) {
# here we block until an event is available
$w->recv;
break if $error;
# any websocket message available ?
if ($websocket_event) {
my $msg = uwsgi::websocket_recv_nb;
$redis_publisher->publish('foobar', $msg) if $msg;
$websocket_event = 0;
}
# any redis message available ?
if ($redis_message) {
uwsgi::websocket_send('['.time().'] '.$redis_message);
$redis_message = undef;
}
$w = AnyEvent->condvar;
}
}
}
|