Skip to content

Instantly share code, notes, and snippets.

@aberant
Created August 31, 2009 14:12
Show Gist options
  • Save aberant/178479 to your computer and use it in GitHub Desktop.
Save aberant/178479 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'eventmachine'
require File.join( File.dirname( __FILE__), '..', 'osc-ruby')
module OSC
Channel = EM::Channel.new
class Connection < EventMachine::Connection
def receive_data data
Channel << OSC::OSCPacket.messages_from_network( data )
end
end
class EMServer
def initialize
setup_dispatcher
@tuples = []
end
def run
EM::run { EventMachine::open_datagram_socket "localhost", 3333, Connection }
end
def add_method(address_pattern, &proc)
matcher = AddressPattern.new( address_pattern )
@tuples << [matcher, proc]
end
private
def setup_dispatcher
Channel.subscribe do |messages|
messages.each do |message|
diff = ( message.time || 0 ) - Time.now.to_ntp
if diff <= 0
sendmesg( message )
else
EventMachine.defer do
sleep( diff )
sendmesg( message )
end
end
end
end
end
def sendmesg(mesg)
@tuples.each do |matcher, obj|
if matcher.match?( mesg.address )
obj.call( mesg )
end
end
end
end
end
@server = OSC::EMServer.new
@server.add_method '/tuio/2Dobj' do |msg|
puts msg.inspect
end
@server.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment