forked from gruis/sinatra-websocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.rb
23 lines (21 loc) · 852 Bytes
/
request.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
module SinatraWebsocket
module Ext
module Sinatra
module Request
# Taken from skinny https://github.com/sj26/skinny and updated to support Firefox
def websocket?
env['HTTP_CONNECTION'].split(',').map(&:strip).map(&:downcase).include?('upgrade') &&
env['HTTP_UPGRADE'].downcase == 'websocket'
end
# Taken from skinny https://github.com/sj26/skinny
def websocket(options={}, &blk)
env['skinny.websocket'] ||= begin
raise RuntimeError, "Not a WebSocket request" unless websocket?
SinatraWebsocket::Connection.from_env(env, options, &blk)
end
end
end
end # module::Sinatra
end # module::Ext
end # module::SinatraWebsocket
defined?(Sinatra) && Sinatra::Request.send(:include, SinatraWebsocket::Ext::Sinatra::Request)