websocket "chat" with angelo!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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