Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added simple example of using ping & pong

  • Loading branch information...
commit cfb4b406949f8769208f181d8022b69203a62230 1 parent 5174de4
Martyn Loughran authored

Showing 2 changed files with 26 additions and 2 deletions. Show diff stats Hide diff stats

  1. +2 2 examples/echo.rb
  2. +24 0 examples/ping.rb
4 examples/echo.rb
... ... @@ -1,8 +1,8 @@
1   -require 'lib/em-websocket'
  1 +require File.expand_path('../../lib/em-websocket', __FILE__)
2 2
3 3 EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080, :debug => true) do |ws|
4 4 ws.onopen { ws.send "Hello Client!"}
5 5 ws.onmessage { |msg| ws.send "Pong: #{msg}" }
6 6 ws.onclose { puts "WebSocket closed" }
7 7 ws.onerror { |e| puts "Error: #{e.message}" }
8   -end
  8 +end
24 examples/ping.rb
... ... @@ -0,0 +1,24 @@
  1 +require File.expand_path('../../lib/em-websocket', __FILE__)
  2 +
  3 +EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080, :debug => false) do |ws|
  4 + timer = nil
  5 + ws.onopen {
  6 + puts "Ping supported: #{ws.pingable?}"
  7 + timer = EM.add_periodic_timer(1) {
  8 + p ["Sent ping", ws.ping('hello')]
  9 + }
  10 + }
  11 + ws.onpong { |value|
  12 + puts "Received pong: #{value}"
  13 + }
  14 + ws.onping { |value|
  15 + puts "Received ping: #{value}"
  16 + }
  17 + ws.onclose {
  18 + EM.cancel_timer(timer)
  19 + puts "WebSocket closed"
  20 + }
  21 + ws.onerror { |e|
  22 + puts "Error: #{e.message}"
  23 + }
  24 +end

0 comments on commit cfb4b40

Please sign in to comment.
Something went wrong with that request. Please try again.