Skip to content

Commit

Permalink
updated WebSocket example application
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 8, 2013
1 parent 4551cdc commit 10f2712
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
27 changes: 11 additions & 16 deletions examples/websocket.pl
Expand Up @@ -3,7 +3,7 @@
use Mojolicious::Lite;
use Mojo::JSON 'j';

websocket '/' => sub {
websocket '/test' => sub {
my $self = shift;
$self->on(
text => sub {
Expand All @@ -25,29 +25,24 @@
<!DOCTYPE html>
<html>
<head>
<title>WebSocket</title>
% my $url = url_for->to_abs->scheme('ws');
<title>WebSocket Test</title>
%= javascript begin
var ws;
if ("WebSocket" in window) {
ws = new WebSocket('<%= $url %>');
ws = new WebSocket('<%= url_for('test')->to_abs %>');
}
if(typeof(ws) !== 'undefined') {
function wsmessage(event) {
alert(JSON.parse(event.data).test);
}
function wsopen(event) {
ws.send(JSON.stringify({test: "WebSocket support works! ♥"}));
}
ws.onmessage = wsmessage;
ws.onopen = wsopen;
ws.onmessage = function (event) {
document.body.innerHTML += JSON.parse(event.data).test;
};
ws.onopen = function (event) {
ws.send(JSON.stringify({test: 'WebSocket support works! ♥'}));
};
}
else {
alert("Sorry, your browser does not support WebSockets.");
document.body.innerHTML += 'Browser does not support WebSockets.';
}
% end
</head>
<body>
Testing WebSockets, please make sure you have JavaScript enabled.
</body>
<body>Testing WebSockets: </body>
</html>
4 changes: 2 additions & 2 deletions lib/Mojolicious/Lite.pm
Expand Up @@ -884,14 +884,14 @@ L<Mojolicious::Controller/"send">.
<!DOCTYPE html>
<html>
<head>
<title>WebSocket</title>
<title>Echo</title>
%= javascript begin
var ws = new WebSocket('<%= url_for('echo')->to_abs %>');
ws.onmessage = function (event) {
document.body.innerHTML += event.data + '<br/>';
};
ws.onopen = function (event) {
ws.send("I ♥ Mojolicious!");
ws.send('I ♥ Mojolicious!');
};
% end
</head>
Expand Down

0 comments on commit 10f2712

Please sign in to comment.