4
我使用eventmachine從HornetQ主題讀取,推送到由EM websocket連接訂閱的頻道。我需要防止@ topic.receive循環被阻塞,所以創建了一個proc,並且調用了沒有回調的EventMachine.defer。這將無限期地運行。這工作正常。我也可以使用Thread.new。EventMachine和em-websocket - 從隊列讀取並推送到頻道
我的問題是,這是從流/隊列中讀取數據並將數據傳遞到通道的正確方法,並且是否有更好的/任何其他方式來執行此操作?
require 'em-websocket'
require 'torquebox-messaging'
class WebsocketServer
def initialize
@channel = EM::Channel.new
@topic = TorqueBox::Messaging::Topic.new('/topics/mytopic')
end
def start
EventMachine.run do
topic_to_channel = proc do
while true
msg = @topic.receive
@channel.push msg
end
end
EventMachine.defer(topic_to_channel)
EventMachine::WebSocket.start(:host => "127.0.0.1", :port => 8081, :debug => false) do |connection|
connection.onopen do
sid = @channel.subscribe { |msg| connection.send msg }
connection.onclose do
@channel.unsubscribe(sid)
end
end
end
end
end
end
WebsocketServer.new.start
我認爲em在這裏是用於websocket的實現,有沒有一個扭矩盒的選擇? –