Permalink
Browse files

Added example: log_socket_data.rb

  • Loading branch information...
1 parent 351f5a9 commit b16c4918ceaef9361e1223f42d20f874d1c041ac Kaoru Kobo committed Dec 1, 2010
Showing with 34 additions and 0 deletions.
  1. +34 −0 examples/log_socket_data.rb
@@ -0,0 +1,34 @@
+require "socket"
+
+require "rubygems"
+require "rhook"
+
+# Trace TCPSocket output data.
+TCPSocket._rhook.hack(:write) do |inv|
+ STDERR.print("> ", inv.args[0].inspect, "\n")
+ inv.call
+end
+
+# Trace TCPSocket input data.
+TCPSocket._rhook.hack(:read) do |inv|
+ inv.call
+ STDERR.print("< ", inv.returned.inspect, "\n")
+ inv.returned
+end
+
+
+require "rack"
+require "rack/handler/webrick"
+
+th = Thread.start {
+ Rack::Handler::WEBrick.run(lambda { |env|
+ [200, {}, []]
+ }, :Port => 9292)
+}
+sleep 1
+
+sock = TCPSocket.new("localhost", 9292)
+sock.print("GET / HTTP/1.0\n\n")
+sock.flush
+
+sock.read

0 comments on commit b16c491

Please sign in to comment.