Spockets ==

Spockets is a simple library for dealing with multiple sockets. You supply a socket, and one or more blocks to execute, and Spockets will make sure those blocks get executed when something comes in over the wire. It’s just that simple.

There is one requirement for spockets and that is the ActionPool. This just allows spockets to use a thread pool for executing blocks so you don’t end up having to wait on slow blocks. You can even provide your own pool for spockets to use, so all the action can stay in one local pool.

install (stable):

gem install spockets

install (unstable):

gem sources -a http://gems.github.com
gem install spox-spockets

or

git clone http://github.com/spox/spockets.git
cd spockets
gem build spockets.gemspec
gem install ./

It has RDocs. They are short, but will be helpful and you should really consider giving them a look. If you want to view them online, you can see them here:

dev.modspox.com/~sine/spockets

There is also a trac site. It has examples as well as a bug tracker. It’s located at:

dev.modspox.com/trac/spockets

Examples are usually helpful, so here we go:

Code:

require 'socket'
require 'spockets'
spockets = Spockets::Spockets.new

se = TCPServer.new(3000)
loop do
    s = se.accept
    puts "Socket: #{s}"
    spockets.add(s){|string| puts "#{s}: #{string}" }
end
sleep

Connecting:

> telnet 192.168.0.95 3000
Trying 192.168.0.95...
Connected to 192.168.0.95.
Escape character is '^]'.
goodbyeworld
^]
telnet> quit
Connection closed.

> telnet 192.168.0.95 3000
Trying 192.168.0.95...
Connected to 192.168.0.95.
Escape character is '^]'.
foobar
complete
^]
telnet> quit
Connection closed.

Output:

Socket: #<TCPSocket:0x98ec5ac>
Socket: #<TCPSocket:0x98ec37c>
#<TCPSocket:0x98ec37c>: foobar
#<TCPSocket:0x98ec5ac>: goodbyeworld
#<TCPSocket:0x98ec37c>: complete