从Sinatra路线访问EventMachine频道(Accessing an EventMachine channel from a Sinatra route)

编程入门 行业动态 更新时间:2024-10-26 23:37:44
从Sinatra路线访问EventMachine频道(Accessing an EventMachine channel from a Sinatra route)

我在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 end

How 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.

更多推荐

本文发布于:2023-07-09 00:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1082129.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路线   频道   EventMachine   Sinatra   channel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!