Skip to content

Commit

Permalink
Added simple example of using ping & pong
Browse files Browse the repository at this point in the history
  • Loading branch information
mloughran committed Dec 21, 2011
1 parent 5174de4 commit cfb4b40
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions examples/echo.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,8 +1,8 @@
require 'lib/em-websocket' require File.expand_path('../../lib/em-websocket', __FILE__)


EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080, :debug => true) do |ws| EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080, :debug => true) do |ws|
ws.onopen { ws.send "Hello Client!"} ws.onopen { ws.send "Hello Client!"}
ws.onmessage { |msg| ws.send "Pong: #{msg}" } ws.onmessage { |msg| ws.send "Pong: #{msg}" }
ws.onclose { puts "WebSocket closed" } ws.onclose { puts "WebSocket closed" }
ws.onerror { |e| puts "Error: #{e.message}" } ws.onerror { |e| puts "Error: #{e.message}" }
end end
24 changes: 24 additions & 0 deletions examples/ping.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,24 @@
require File.expand_path('../../lib/em-websocket', __FILE__)

EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080, :debug => false) do |ws|
timer = nil
ws.onopen {
puts "Ping supported: #{ws.pingable?}"
timer = EM.add_periodic_timer(1) {
p ["Sent ping", ws.ping('hello')]
}
}
ws.onpong { |value|
puts "Received pong: #{value}"
}
ws.onping { |value|
puts "Received ping: #{value}"
}
ws.onclose {
EM.cancel_timer(timer)
puts "WebSocket closed"
}
ws.onerror { |e|
puts "Error: #{e.message}"
}
end

0 comments on commit cfb4b40

Please sign in to comment.