Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kenichi
Created May 12, 2014 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenichi/258444aa16d0e49c9110 to your computer and use it in GitHub Desktop.
Save kenichi/258444aa16d0e49c9110 to your computer and use it in GitHub Desktop.
websocket "chat" with angelo!
require 'angelo'
require 'tilt'
require 'angelo/tilt/erb'
class Chat < Angelo::Base
include Angelo::Tilt::ERB
@@views = '.'
get '/' do
erb :index
end
websocket '/' do |ws|
websockets[:chat] << ws
ws.on_message do |msg|
websockets[:chat].each do |c|
c.write msg unless c == ws
end
end
end
end
Chat.run
<div id="chat">
</div>
<input type="text" id="msg"/>
<button id="say">say</button>
<script>
var ws = new WebSocket('ws://127.0.0.1:4567/');
ws.onmessage = function(e) {
console.log(e);
$('#chat').append(e.data + "<br/>");
}
$('#say').on('click', function(e) {
ws.send($('#msg').val());
$('#msg').val('');
});
</script>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
<%= yield %>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment