Skip to content

Instantly share code, notes, and snippets.

@janx
Created April 30, 2014 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janx/709c60fb5475b9413b3d to your computer and use it in GitHub Desktop.
Save janx/709c60fb5475b9413b3d to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'json'
require 'openssl'
require 'eventmachine'
require 'websocket-eventmachine-client'
access_key = 'your access key'
secret_key = 'your secret key'
EM.run do
ws = WebSocket::EventMachine::Client.connect(uri: 'ws://localhost:8080')
ws.onopen do
puts "Connected."
end
ws.onmessage do |msg, type|
p type
msg = JSON.parse(msg)
p msg
key = msg.keys.first
data = msg[key]
case key
when 'challenge'
str = "#{access_key}#{data}"
answer = OpenSSL::HMAC.hexdigest 'SHA256', secret_key, str
payload = JSON.dump({
auth: {
access_key: access_key,
answer: answer
}
})
ws.send payload
else
end
end
ws.onclose do
puts "Closed."
EM.stop
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment