Skip to content

Instantly share code, notes, and snippets.

@karmi
Created December 1, 2010 10:06
Show Gist options
  • Save karmi/723279 to your computer and use it in GitHub Desktop.
Save karmi/723279 to your computer and use it in GitHub Desktop.
Simple ping:pong WebSockets server and client in Ruby (em-http and em-websocket
Gemfile.lock
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>Simple WebSockets Ping:Pong Client</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>body { font-family: sans-serif; } p {margin: 0 0 0.25em 0;} </style>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'></script>
<script>
$(document).ready(function(){
if( !("WebSocket" in window) ) {
throw("Sorry, no WebSockets support");
};
var send = function() {
message = $('#message').val();
console.log('Ping: ' + message);
$('#output').append('<p style="color:#999; font-size: 70%">Ping: '+message+'</p>');
ws.send( message );
}
ws = new WebSocket("ws://localhost:8000/");
ws.onmessage = function(evt) {
console.log(evt.data);
if (evt.data != '.') $('#output').append('<p>'+evt.data+'</p>');
};
ws.onclose = function() {
console.log("socket closed");
};
ws.onopen = function() {
console.log("connected...");
};
$('#message').change(function() { send(); });
$('#send' ).click(function() { send(); });
});
</script>
</head>
<body>
<input id="message" type="text" name="message" value="">
<input id="send" type="submit" name="send" value="Send">
<hr>
<div id="output"></div>
</body>
</html>
source 'http://rubygems.org'
gem 'em-http-request', :require => 'em-http'
gem 'em-websocket'
# = Simple "Ping:Pong" websocket client
#
# See https://github.com/igrigorik/em-http-request/blob/master/examples/websocket-handler.rb
#
# $ bundle install
# $ bundle exec ruby ruby-websocket-client.rb
#
require 'rubygems'
require 'em-http'
HOST, PORT = '0.0.0.0', 8000
EventMachine.run do
puts '='*80, "Connecting to websockets server at ws://#{HOST}:#{PORT}", '='*80
http = EventMachine::HttpRequest.new("ws://#{HOST}:#{PORT}/websocket").get :timeout => 0
http.errback do
puts "oops, error"
end
http.callback do
puts "#{Time.now.strftime('%H:%M:%S')} : Connected to server"
end
http.stream do |msg|
puts "Recieved: #{msg}"
end
http.disconnect do
puts "Oops, dropped connection?"
end
end
# = Simple "Ping:Pong" websocket server
#
# See https://github.com/igrigorik/em-http-request/blob/master/examples/websocket-server.rb
#
# $ bundle install
# $ bundle exec ruby ruby-websocket-server.rb
#
require 'rubygems'
require 'em-websocket'
HOST, PORT = '0.0.0.0', 8000
EventMachine.run do
puts '='*80, "Starting websockets server at socket://#{HOST}:#{PORT}", '='*80
EventMachine::WebSocket.start(:host => HOST, :port => PORT) do |socket|
socket.onopen do
puts "#{Time.now.strftime('%H:%M:%S')} : Client connected", '-'*80
socket.send "Hello client!"
EventMachine.add_periodic_timer(1) { socket.send '.' } # Tick...
end
socket.onclose do
puts "#{Time.now.strftime('%H:%M:%S')} : Client disconnected", '-'*80
end
socket.onmessage do |msg|
puts "Recieved message: #{msg}"
socket.send "Pong: #{msg}"
end
end
end
@alpesh-virtueinfo
Copy link

hello
First i install gem
I try this example .rb file put in lib and controller and .html file put in view but not success
In rails directory structure where to put in this file ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment