summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/em-websocket-0.5.3/examples/ping.rb
blob: fe569c3b0b78a36dccaa1526b642f2732922d212 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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