Skip to content

Instantly share code, notes, and snippets.

@brianmario
Created August 12, 2009 00:06
Show Gist options
  • Save brianmario/166198 to your computer and use it in GitHub Desktop.
Save brianmario/166198 to your computer and use it in GitHub Desktop.
module EventMachine
module Protocols
module JsonProtocol
def post_init
@parser = Yajl::Parser.new
@parser.on_parse_complete = method(:receive_object)
@encoder = Yajl::Encoder.new
end
def receive_data(data)
@parser << data
end
# Invoked with ruby objects received over the network
def receive_object(obj)
# stub
end
# Sends a ruby object over the network
def send_object(obj)
# attempts to hand the caller 8kb chunks of the buffer as
# it's being generated
@encoder.encode(obj) do |chunk|
send_data(chunk)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment