3
我有一個在EventMachine上運行的簡單的Sinatra App,如this example。從Sinatra路線訪問EventMachine頻道
該應用程序正在運行,現在我想允許我在Sinatra中定義的路線使用創建的EventMachine通道訪問websocket。我天真地嘗試了以下,但當然在Sinatra應用程序中,@channel
變量未定義,所以這不起作用。
require 'em-websocket'
require 'sinatra'
EventMachine.run do
@channel = EM::Channel.new
class App < Sinatra::Base
get '/' do
erb :index
end
post '/test' do
@channel.push "Post request hit endpoint"
status 200
end
end
EventMachine::WebSocket.start :host => '0.0.0.0', :port => 8080 do |socket|
socket.onopen do
sid = @channel.subscribe { |msg| socket.send msg }
@channel.push "Subscriber ID #{sid} connected!"
socket.onmessage do |msg|
@channel.push "Subscriber <#{sid}> sent message: #{msg}"
end
socket.onclose do
@channel.unsubscribe(sid)
end
end
end
App.run! :port => 3000
end
我怎麼能訪問我的Sinatra應用程序中打開的EventMachine頻道?
您是否嘗試過在[配置塊](http://www.sinatrarb.com/intro#Configuration)內部安裝EM?然後你可以通過'settings.channel'訪問它(如果它有效,我只是建議這個,我沒有嘗試過)。 – iain
@inin的問題是,如何在Sinatra之外找到它?我嘗試過的唯一辦法就是將其設置爲一個全局變量,我認爲這是可以的,但是通常會皺起眉頭...... – Andrew
將整個模塊包裝到模塊中並使用模塊的類實例變量如何?它的範圍和全球! :) – iain