Skip to content

Instantly share code, notes, and snippets.

@mokevnin
Created September 23, 2011 10:34
Show Gist options
  • Save mokevnin/1237103 to your computer and use it in GitHub Desktop.
Save mokevnin/1237103 to your computer and use it in GitHub Desktop.
#TODO demonize and logging
require 'rubygems'
require 'em-zeromq'
require 'evma_httpserver'
Thread.abort_on_exception = true
class EMTestPullHandler
attr_reader :received
def initialize(response)
@response = response
end
def on_readable(socket, messages)
messages.each do |m|
puts m.copy_out_string
@response.send_response
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)
end
def handler(response)
# Сначала проверяем базу на наличие новых сообщений
# ...
@sub = CTX.connect(ZMQ::SUB, 'tcp://127.0.0.1:5555', EMTestPullHandler.new(response))
# авторизация по сессионой куке?
@sub.subscribe 'ruby'
end
end
EventMachine.run {
Signal.trap("INT") { EventMachine.stop }
Signal.trap("TERM") { EventMachine.stop }
CTX = EM::ZeroMQ::Context.new(1)
EventMachine.start_server '127.0.0.1', 8080, ChatLongPolling
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment