Sha256: fa2878cda5b664714e0a24978a994626ee8343b48aee04f75fd69e8a15b1cb27

Contents?: true

Size: 1008 Bytes

Versions: 1

Compression:

Stored size: 1008 Bytes

Contents

require "sinatra/hijacker/version"
require "rack/hijack_wrapper"
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.2 lib/sinatra/hijacker.rb