我在EventMachine上运行了一个简单的Sinatra应用程序,就像这个例子一样 。
该应用程序正在运行,现在我想允许我在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频道?
I have a simple Sinatra App running on EventMachine, like this example.
The app is working, now I'd like to allow the routes I'm defining in Sinatra to access the websocket using the EventMachine channel that is created. I naively tried the following, but of course within the Sinatra App, the @channel variable isn't defined, so this doesn't work.
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 endHow could I access the EventMachine channel I've got open within my Sinatra app?
最满意答案
如果其他人不知道我们在评论中谈论的内容,这里是一个以我建议的方式使用类实例变量的示例。 这运行,但我不知道它是否符合预期:
require 'em-websocket' require 'sinatra' require 'haml' module Example def self.em_channel @em_channel ||= EM::Channel.new end EventMachine.run do class App < Sinatra::Base configure do enable :inline_templates end get '/' do haml :index end get '/test' do Example.em_channel.push "Test request hit endpoint" status 200 end end EventMachine::WebSocket.start :host => '0.0.0.0', :port => 8080 do |socket| socket.onopen do sid = Example.em_channel.subscribe { |msg| socket.send msg } Example.em_channel.push "Subscriber ID #{sid} connected!" socket.onmessage do |msg| Example.em_channel.push "Subscriber <#{sid}> sent message: #{msg}" end socket.onclose do Example.em_channel.unsubscribe(sid) end end end App.run! end end __END__ @@ layout %html = yield @@ index %div.title Hello world.In case others don't know what we're talking about in the comments, here's an example of using a class instance variable in the way I suggested. This runs, but I don't know if it does what's expected:
require 'em-websocket' require 'sinatra' require 'haml' module Example def self.em_channel @em_channel ||= EM::Channel.new end EventMachine.run do class App < Sinatra::Base configure do enable :inline_templates end get '/' do haml :index end get '/test' do Example.em_channel.push "Test request hit endpoint" status 200 end end EventMachine::WebSocket.start :host => '0.0.0.0', :port => 8080 do |socket| socket.onopen do sid = Example.em_channel.subscribe { |msg| socket.send msg } Example.em_channel.push "Subscriber ID #{sid} connected!" socket.onmessage do |msg| Example.em_channel.push "Subscriber <#{sid}> sent message: #{msg}" end socket.onclose do Example.em_channel.unsubscribe(sid) end end end App.run! end end __END__ @@ layout %html = yield @@ index %div.title Hello world.更多推荐
发布评论