在桌面应用程序中嵌入网络服务器:wxRuby 和 Sinatra

发布于 2024-08-18 10:03:10 字数 1101 浏览 12 评论 0原文

我很乐意为我的基于 Windows 的桌面应用程序提供一个 Web 界面,反之亦然。我的桌面应用程序是用 wxRuby 编写的,网络服务器是 Sinatra(使用 webrick)。最简单的想法就是把它们混在一起,这是行不通的。

这段代码不起作用。网络服务器和 GUI 应用程序不同时运行。桌面应用程序先运行,关闭后再运行;西纳特拉启动。

require 'wx'
require 'sinatra'
configure do set :server, 'webrick' end

get '/' do
  "Sinatra says hello"
end

class MyApp < Wx::App
  def on_init
    @frame = Wx::Frame.new( nil, -1, "Application" )
    @frame.show
  end
end

app = MyApp.new
app.main_loop

所以我想把最后两行改为

Thread.new do
    app = MyApp.new
    app.main_loop
end

Again。桌面应用程序运行直至关闭,然后网络服务器启动。所以我尝试在线程中启动 Sinatra。

Thread.new do
    require 'sinatra'
    configure do set :server, 'webrick' end

    get '/' do
        "Sinatra says hello"
    end
end

require 'wx'

class MyApp < Wx::App
    def on_init
        @frame = Wx::Frame.new( nil, -1, "Application" )
        @frame.show
    end
end

app = MyApp.new
app.main_loop

再次。桌面应用程序运行直至关闭,然后网络服务器启动。

请提出建议,但请记住,我真的很想只有一个过程。 如果您的解决方案是两个进程;我想要不需要轮询的强大的进程间通信。

谢谢! 杰夫

I would love to give my windows based desktop applications a web interface and vice versa. My desktop application is written in wxRuby and the webserver is Sinatra (using webrick). The simplest idea was just to mash them together, this does not work.

This code does not work. The webserver and gui app do not run simultaneously. The desktop application runs first, and then after it is closed; sinatra starts.

require 'wx'
require 'sinatra'
configure do set :server, 'webrick' end

get '/' do
  "Sinatra says hello"
end

class MyApp < Wx::App
  def on_init
    @frame = Wx::Frame.new( nil, -1, "Application" )
    @frame.show
  end
end

app = MyApp.new
app.main_loop

So I thought about changing the last two lines to

Thread.new do
    app = MyApp.new
    app.main_loop
end

Again. Desktop App runs until closed, then webserver starts. So I tried starting Sinatra in a Thread.

Thread.new do
    require 'sinatra'
    configure do set :server, 'webrick' end

    get '/' do
        "Sinatra says hello"
    end
end

require 'wx'

class MyApp < Wx::App
    def on_init
        @frame = Wx::Frame.new( nil, -1, "Application" )
        @frame.show
    end
end

app = MyApp.new
app.main_loop

Again. Desktop App runs until closed, then webserver starts.

Please advise, but keep in mind that I would really like to just have one process. If your solution is two processes; I would like strong inter-process communication that does not require polling.

Thanks!
Jeff

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

救星 2024-08-25 10:03:10

这至少会启动,不确定这是否违反了一些线程规则。

require 'win32/process'
require 'sinatra/base'

class MyWebServer < Sinatra::Base
  get '/' do
    'Hello world!'
  end
end

Thread.new do
  MyWebServer.run! :host => 'localhost', :port => 4567
end

require 'wx'

class MyGui < Wx::App
    def on_init
        t = Wx::Timer.new(self, 55)
        evt_timer(55) { Thread.pass }
        t.start(1)
        evt_idle { Thread.pass }
        @frame = Wx::Frame.new( nil, -1, "Application" )
        @frame.show
        true
    end
end

app = MyGui.new
app.main_loop

This at least starts up, not sure if this breaks some threading rules.

require 'win32/process'
require 'sinatra/base'

class MyWebServer < Sinatra::Base
  get '/' do
    'Hello world!'
  end
end

Thread.new do
  MyWebServer.run! :host => 'localhost', :port => 4567
end

require 'wx'

class MyGui < Wx::App
    def on_init
        t = Wx::Timer.new(self, 55)
        evt_timer(55) { Thread.pass }
        t.start(1)
        evt_idle { Thread.pass }
        @frame = Wx::Frame.new( nil, -1, "Application" )
        @frame.show
        true
    end
end

app = MyGui.new
app.main_loop
━╋う一瞬間旳綻放 2024-08-25 10:03:10

你可以使用 bowline,但我还没有使用它。

you can use bowline, but i'm not yet using it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文