-
-
Save Industrial/70db8f87de6d68453c2031424c378402 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'digest' | |
require 'faye/websocket' | |
require 'pp' | |
Faye::WebSocket.load_adapter('thin') | |
GUID = '3f35e4f2-3920-4797-9044-c4046ea4fb53' | |
def create_websocket_accept (nonce) | |
Digest::SHA1.base64digest "#{nonce}#{GUID}" | |
end | |
App = lambda do |env| | |
return [200, { 'Content-Type' => 'text/plain' }, ['OK']] unless Faye::WebSocket.websocket? env | |
request = Rack::Request.new env | |
protocol = request.get_header 'HTTP_SEC_WEBSOCKET_PROTOCOL' | |
version = request.get_header 'HTTP_SEC_WEBSOCKET_VERSION' | |
key = request.get_header 'HTTP_SEC_WEBSOCKET_KEY' | |
accept = create_websocket_accept key | |
pp [:protocol, protocol] | |
pp [:version, version] | |
pp [:key, key] | |
pp [:accept, accept] | |
# socket = Faye::WebSocket.new env | |
# socket.on :message do |event| | |
# p [:message, data.protocol, data.command, data.payload] | |
# socket.send event.data | |
# end | |
# socket.on :close do |event| | |
# p [:close, event.code, event.reason] | |
# ws = nil | |
# end | |
response = [101, { | |
'Upgrade' => 'websocket', | |
'Connection' => 'Upgrade', | |
'Sec-Websocket-Accept' => accept, | |
'Sec-Websocket-Protocol' => protocol, | |
'Sec-Websocket-Version' => version, | |
}, [""]] | |
pp response | |
response | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment