Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 56 lines (42 sloc) 1.358 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
$: << 'lib' << '../lib'

require 'eventmachine'
require 'spdy'

class SPDYHandler < EM::Connection
  def post_init
    @parser = SPDY::Parser.new
    @parser.on_headers do |stream_id, headers|
      p [:SPDY_HEADERS, headers]

      sr = SPDY::Protocol::Control::SynReply.new({:zlib_session => @parser.zlib_session})
      h = {'Content-Type' => 'text/plain', 'status' => '200 OK', 'version' => 'HTTP/1.1'}
      sr.create({:stream_id => stream_id, :headers => h})
      send_data sr.to_binary_s

      p [:SPDY, :sent, :SYN_REPLY]

      d = SPDY::Protocol::Data::Frame.new
      d.create(:stream_id => stream_id, :data => "This is SPDY.")
      send_data d.to_binary_s

      p [:SPDY, :sent, :DATA]

      d = SPDY::Protocol::Data::Frame.new
      d.create(:stream_id => stream_id, :flags => 1)
      send_data d.to_binary_s

      p [:SPDY, :sent, :DATA_FIN]
    end

  end

  def receive_data(data)
    p [:received, data]
    @parser << data
  end

  def unbind
    p [:SPDY, :connection_closed]
    @parser.zlib_session.reset
  end
end

EM.run do
  EM.start_server '0.0.0.0', 10000, SPDYHandler
end

# (1) start the SPDY eventmachine server
# > ruby spdy_server.rb
#
# (2) start Chrome and force it to use SPDY over SSL.. on OSX:
# > /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --use-spdy=ssl
#
# (3) visit https://localhost:10000/
Something went wrong with that request. Please try again.