Permalink
Browse files

Added simple example of using ping & pong

  • Loading branch information...
1 parent 5174de4 commit cfb4b406949f8769208f181d8022b69203a62230 @mloughran committed Dec 21, 2011
Showing with 26 additions and 2 deletions.
  1. +2 −2 examples/echo.rb
  2. +24 −0 examples/ping.rb
View
4 examples/echo.rb
@@ -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|
ws.onopen { ws.send "Hello Client!"}
ws.onmessage { |msg| ws.send "Pong: #{msg}" }
ws.onclose { puts "WebSocket closed" }
ws.onerror { |e| puts "Error: #{e.message}" }
-end
+end
View
24 examples/ping.rb
@@ -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.