pongd is now an IPC service real-life example
parent
63d2999ccb
commit
02a6ac97c1
17
src/pongd.cr
17
src/pongd.cr
|
@ -20,8 +20,9 @@ OptionParser.parse do |parser|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
service = IPC::Service.new (service_name)
|
||||||
|
|
||||||
IPC::Service.new (service_name) do |event|
|
service.loop do |event|
|
||||||
case event
|
case event
|
||||||
when IPC::Event::Timer
|
when IPC::Event::Timer
|
||||||
if verbosity >= 1
|
if verbosity >= 1
|
||||||
|
@ -36,13 +37,23 @@ IPC::Service.new (service_name) do |event|
|
||||||
puts "#{CBLUE}IPC::Event::Disconnection#{CRESET}, client: #{event.connection.fd}"
|
puts "#{CBLUE}IPC::Event::Disconnection#{CRESET}, client: #{event.connection.fd}"
|
||||||
end
|
end
|
||||||
when IPC::Event::Message
|
when IPC::Event::Message
|
||||||
|
begin
|
||||||
if verbosity >= 1
|
if verbosity >= 1
|
||||||
puts "#{CGREEN}IPC::Event::Message#{CRESET}, client: #{event.connection.fd}"
|
puts "#{CGREEN}IPC::Event::Message#{CRESET}, client: #{event.connection.fd}"
|
||||||
if verbosity == 2
|
if verbosity >= 2
|
||||||
puts "#{CBLUE}message: #{event.message} #{CRESET}"
|
m = String.new event.message.payload
|
||||||
|
puts "#{CBLUE}message type #{event.message.type}: #{m} #{CRESET}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
event.connection.send event.message
|
event.connection.send event.message
|
||||||
|
if verbosity >= 2
|
||||||
|
puts "#{CBLUE}message sent#{CRESET}"
|
||||||
|
end
|
||||||
|
|
||||||
|
rescue e
|
||||||
|
puts "#{CRED}#{e.message}#{CRESET}"
|
||||||
|
service.remove_fd event.connection.fd
|
||||||
|
end
|
||||||
else
|
else
|
||||||
if verbosity >= 1
|
if verbosity >= 1
|
||||||
puts "#{CRED}Exception: message = #{event.message} #{CRESET}"
|
puts "#{CRED}Exception: message = #{event.message} #{CRESET}"
|
||||||
|
|
Reference in New Issue