The next episode in my “Dicking around with Ruby” series: Serv.
Serv is a very basic TCP server with a twist: it handles the footwork of waiting for lines and handling disconnections and multiple clients, and you provide the hooks for “when stuff happens”. It won’t do binary protocols, it won’t do protocols that don’t use line breaks everywhere and it won’t do UDP. However, I do believe it could be used to implement, say, an IRC server or an HTTP server.
For example:
require 'socket'
require 'serv'
serv = Serv.new(4242)
serv.add_hook(:introduce, proc {|par|
sock = par[:sock]
sock.puts "Hey there! It's #{Time.now.utc.strftime('%H:%M:%S')}" +
" UTC. Do you know where your children are?"
serv.hdo :revoke, sock
})
serv.run
The socket guts of Serv is based on a short example put forward in Sockets programming in Ruby, although there’s not a lot that could be done differently, as evidenced by a quick search for similar examples. For this reason I release Serv into the public domain. No license.
Here’s that download link again.
Thoughts?