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.
This library makes use of a thread pool for running user defined blocks. This setup allows us to use a single thread for socket monitoring, as well as prevents long running blocks from delaying everybody else.
gem install spockets
git clone http://github.com/spox/spockets.git cd spockets && gem build *.gemspec && gem install ./
rip makes it easy to install directly from a github repository.
Spockets is currently tested on:
-
Ruby 1.8.6-p383
-
Ruby 1.8.7-p248
-
Ruby 1.9.1-p376
-
JRuby 1.4.0
It has RDocs. They are short, but will be helpful and you should really consider giving them a look.
Examples are usually helpful, so here we go:
Code:
require 'socket' require 'spockets' spockets = Spockets::Spockets.new server = TCPServer.new('localhost', 4000) socket = TCPSocket.new('localhost', 4000) rsocket = server.accept output = [] spockets.add(rsocket){|s|output << s.strip} spockets.add(rsocket, 'data'){|s, d| output << "#{d}: #{s.strip}"} spockets.on_close(rsocket){|s| output << "closed: #{s}"} socket.puts "fubar" socket.close sleep(0.01) p output => ["fubar", "data: fubar", "closed: #<TCPSocket:0xb7637bdc>"]
If you find any bugs, please report them through github. If you are in need of any help, you can generally find me on DALnet and Freenode.
Spockets is licensed under the LGPLv3 Copyright (c) 2009 spox <[email protected]>