Skip to content

Instantly share code, notes, and snippets.

@mokevnin
Created September 22, 2011 12:21
Show Gist options
  • Save mokevnin/1234645 to your computer and use it in GitHub Desktop.
Save mokevnin/1234645 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#TODO demonize and logging
require 'rubygems'
require 'eventmachine'
require 'evma_httpserver'
require 'em-zeromq'
Thread.abort_on_exception = true
class EMTestPullHandler
attr_reader :received
def on_readable(socket, messages)
messages.each do |m|
puts m.copy_out_string
end
end
end
class ChatLongPolling < EM::Connection
include EM::HttpServer
def process_http_request
response = EM::DelegatedHttpResponse.new(self)
response.status = 200
response.content_type 'application/json'
handler(response)
response.send_response
end
def handler(response)
# Сначала проверяем базу на наличие новых сообщений
# ...
ctx = EM::ZeroMQ::Context.new(1)
sub = ctx.connect(ZMQ::SUB, 'tcp://127.0.0.1:5555', EMTestPullHandler.new)
#sub.connect 'tcp://127.0.0.1:5555'
# авторизация по сессионой куке?
sub.setsockopt ZMQ::SUBSCRIBE, 'ruby'
#while line = sub.recv
#raise line.inspect
#response.content = line
#break
#end
end
end
EventMachine.run {
Signal.trap("INT") { EventMachine.stop }
Signal.trap("TERM") { EventMachine.stop }
EventMachine.start_server '0.0.0.0', 8080, ChatLongPolling
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment