Skip to content

Instantly share code, notes, and snippets.

/websocket.rb Secret

Created January 10, 2013 15:03
Show Gist options
  • Save anonymous/98bd71ac4622a34b7719 to your computer and use it in GitHub Desktop.
Save anonymous/98bd71ac4622a34b7719 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby1.9.1
require 'eventmachine'
require 'em-websocket'
require 'json'
require 'unicode'
require 'amqp'
require 'redis'
@conf = {
:host => "localhost",
:user => "websocket",
:password => "websocket",
:vhost => "/",
:logging => false,
:port => 5672
}
SOCKETS = []
@redis = Redis.new
# Creating a thread for the EM event loop
Thread.new do
EventMachine.run do
# Creates a websocket listener
EventMachine::WebSocket.start(:host => "donkeyscience.com", :port => 10081,
:secure => true,
:tls_options => {
:private_key_file => '/opt/nginx/ssl/donkeyscience.com.key',
:cert_chain_file => '/opt/nginx/ssl/cert-donkeyscience.com.crt'
}) do |ws|
ws.onopen do
SOCKETS << ws
SOCKETS.each { |s|
puts s.object_id.to_s
}
end
ws.onmessage do |msg|
SOCKETS.each { |s|
puts s.object_id.to_s
}
end
ws.onclose do
SOCKETS.delete ws
SOCKETS.each { |s|
puts s.object_id.to_s
}
end
end
end
end
# Creating a thread for the AMQP subscribe block
Thread.new do
AMQP.start(@conf) do |connection|
puts "Connected to AMQP broker. Running #{AMQP::VERSION} version of the gem..."
channel = AMQP::Channel.new(connection)
twitter_queue = channel.queue('twitter.websocket')
twitter_queue.purge
twitter_queue.subscribe(:ack => true) do |header, body|
# do stuff here / Send to all sockets
puts body
SOCKETS.each {|s| s.send body }
header.ack
end
end
end
sleep
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment