Sha256: 14d2a4ebbd4ba067d0e16f831ab1b35373970eaf47e85a84bbec6c471b2eeaff

Contents?: true

Size: 978 Bytes

Versions: 1

Compression:

Stored size: 978 Bytes

Contents

require "sinatra/hijacker/version"
require "tubesock"

module Sinatra
  module Hijacker

    def call env
      if websocket? env
        env['sinatra.hijacker.websocket'] = Tubesock.hijack(env).tap &:listen
        env['REQUEST_METHOD'] = 'WEBSOCKET'
        super
        [101, {}, []]
      else
        super
      end
    end

    # Taken from https://github.com/simulacre/sinatra-websocket/
    # Originally taken from skinny https://github.com/sj26/skinny and updated to support Firefox
    def websocket? env
      env['HTTP_CONNECTION'] && env['HTTP_UPGRADE'] &&
        env['HTTP_CONNECTION'].split(',').map(&:strip).map(&:downcase).include?('upgrade') &&
        env['HTTP_UPGRADE'].downcase == 'websocket'
    end
    
    def self.registered app
      app.helpers do
        def ws
          env['sinatra.hijacker.websocket']
        end
      end
    end
    
    def websocket(path, opts = {}, &bk)
      route 'WEBSOCKET',     path, opts, &bk
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sinatra-hijacker-0.0.1 lib/sinatra/hijacker.rb