-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added simple example of using ping & pong
- Loading branch information
Showing
2 changed files
with
26 additions
and
2 deletions.
There are no files selected for viewing
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
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 |
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
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 |