多个WT应用程序可以在同一个网页上运行吗?

时间:2013-05-02 14:38:23

标签: c++ visual-studio-2010 embed webpage wt

所以最近我问了一个问题,看看Can multiple WT applications run on same port?是否答案是肯定的,(对于JorgeNúñez来说,这个真棒答案是+1)。但是,现在我试图通过将它们嵌入到一种主机WT应用程序中来进一步查看他的解决方案以查看是否可以在同一页面上运行多个WT应用程序。我所做的是创建了一个主机WT应用程序,它具有root()的访问器并具有WTabWidget。然后在CreateHostApplication函数中,我为2个测试应用程序创建了选项卡,这些应用程序也有root()的访问器,并将root()添加到属于我的宿主应用程序的选项卡,然后将所有应用程序添加到他们的个人标签,我退回了主持人。

很酷的部分是测试应用程序中的小部件出现在我们预期的选项卡中,我没想到的是connect()调用连接按钮到函数失败。因此,只要单击它们并在框中编辑文本,小部件就可以正常运行,但它们不执行任何其他操作,因为它们未连接到任何自定义功能。

稍微调试一下之后,我很确定测试应用程序上的连接调用失败了,因为它们实际上并不是由浏览器托管,这就把我带到了这里。我无法找到解决此问题的方法,是否有办法通过此设置使连接调用正常工作?

以下代码是JorgeNúñez的解决方案,具有上述修改。我正在使用visual studio2010进行开发。提前感谢您的帮助!

#include <Wt/WApplication>

#include <Wt/WBreak>          
#include <Wt/WContainerWidget>
#include <Wt/WLineEdit>       
#include <Wt/WPushButton>     
#include <Wt/WText>           
#include <Wt/WException>      
#include <Wt/WLogger>         
#include <Wt/WServer>         
#include <Wt/WTabWidget>

using namespace Wt;


class Host : public Wt::WApplication
{
public:
  Host(const Wt::WEnvironment& env);
  WContainerWidget* GetRoot()
  {
    return root();
  }
};
Host::Host(const Wt::WEnvironment& env) : Wt::WApplication(env)
{

}

class TestApp1 : public Wt::WApplication
{
public:
  TestApp1(const Wt::WEnvironment& env, const std::string& title);
  WContainerWidget* GetRoot()
  {
    return root();
  }

private:
  Wt::WLineEdit* _name_edit;
  Wt::WText* _greeting;

  void Greet();
};
TestApp1::TestApp1(const Wt::WEnvironment& env, const std::string& title) : Wt::WApplication(env)
{
  setTitle(title);

  root()->addWidget(new Wt::WText("Your name, please ? "));
  _name_edit = new Wt::WLineEdit(root());

  Wt::WPushButton* button = new Wt::WPushButton("Greet me.", root());
  root()->addWidget(new Wt::WBreak());

  _greeting = new Wt::WText(root());
  button->clicked().connect(this, &TestApp1::Greet);
}
void TestApp1::Greet()
{
  _greeting->setText("Hello there, " + _name_edit->text());
}

class TestApp2 : public Wt::WApplication
{
public:
  TestApp2(const Wt::WEnvironment& env, const std::string& title);
  WContainerWidget* GetRoot()
  {
    return root();
  }

private:    Wt::WLineEdit *_name_edit;
            Wt::WText *_greeting;

            void greet();
};
TestApp2::TestApp2(const Wt::WEnvironment& env, const std::string& title) : Wt::WApplication(env)
{
  setTitle(title);

  root()->addWidget(new Wt::WText("Your name, please ? "));
  _name_edit = new Wt::WLineEdit(root());

  Wt::WPushButton* button = new Wt::WPushButton("Say goodbye.", root());
  root()->addWidget(new Wt::WBreak());

  _greeting = new Wt::WText(root());
  button->clicked().connect(this, &TestApp2::greet);
}
void TestApp2::greet()
{
  _greeting->setText("Goodbye, " + _name_edit->text());
}

Wt::WTabWidget* tab_widget;
Wt::WApplication* CreateHostApplication(const Wt::WEnvironment& env)
{
  Host* host = new Host(env);

  WContainerWidget* root = host->GetRoot();

  tab_widget = new WTabWidget(root);

  //Create tab for the app
  WContainerWidget* Tab_TestApp1 = new WContainerWidget();
  //Get a pointer to the ACE tab
  tab_widget->addTab(Tab_TestApp1, "Test Application 1", Wt::WTabWidget::LoadPolicy::PreLoading);
  //Create app
  TestApp1* test_app_1 = new TestApp1(env, "Test Application 1");
  //Add app root to the tab
  Tab_TestApp1->addWidget(test_app_1->GetRoot());

  //Create tab for the app
  WContainerWidget* Tab_TestApp2 = new WContainerWidget();
  //Get a pointer to the ACE tab
  tab_widget->addTab(Tab_TestApp2, "Test Application 2", Wt::WTabWidget::LoadPolicy::PreLoading);
  //Create app
  TestApp2* test_app_2 = new TestApp2(env, "Test Application 2");
  //Add app root to the tab
  Tab_TestApp2->addWidget(test_app_2->GetRoot());

  return host;
}
Wt::WApplication* CreateTestApp1(const Wt::WEnvironment& env)
{
  return new TestApp1(env, "Test Application 1");
}
Wt::WApplication* CreateTestApp2(const Wt::WEnvironment& env)
{
  return new TestApp2(env, "Test Application 2");
}

int TestWRun(int argc, char* argv[],
  Wt::ApplicationCreator host_application,
  std::vector<Wt::ApplicationCreator> applications)
{
  try 
  {
    // use argv[0] as the application name to match a suitable entry
    // in the Wt configuration file, and use the default configuration
    // file (which defaults to /etc/wt/wt_config.xml unless the environment
    // variable WT_CONFIG_XML is set)
    Wt::WServer server(argv[0],"");

    // WTHTTP_CONFIGURATION is e.g. "/etc/wt/wthttpd"
    server.setServerConfiguration(argc, argv, WTHTTP_CONFIGURATION);

    // add a single entry point, at the default location (as determined
    // by the server configuration's deploy-path)
    server.addEntryPoint(Wt::Application, host_application);

    unsigned int num_apps = applications.size();
    for(unsigned int cur_app = 0; cur_app < num_apps; ++cur_app)
    {
      server.addEntryPoint(Wt::Application, applications[cur_app], "/" + boost::lexical_cast<std::string>(cur_app));
    }

    if (server.start()) 
    {
      int sig = Wt::WServer::waitForShutdown(argv[0]);

      std::cerr << "Shutdown (signal = " << sig << ")" << std::endl;
      server.stop();
    }
  } 
  catch (Wt::WServer::Exception& e) 
  {
    std::cerr << e.what() << "\n";
    return 1;
  } 
  catch (std::exception& e) 
  {
    std::cerr << "exception: " << e.what() << "\n";
    return 1;
  }
}
int main(int argc, char** argv)
{
  std::vector<Wt::ApplicationCreator> applications;
  applications.push_back(&CreateTestApp1);
  applications.push_back(&CreateTestApp2);

  return TestWRun(argc, argv, &CreateHostApplication,  applications);
}

1 个答案:

答案 0 :(得分:2)

虽然Wt可以实现,但您的方法并非100%正确。 AFAIK你有两个选择:

  1. 使用Wt的widgetset模式。了解如何在Wt的主页上集成聊天窗口小部件:这些是两个独立的Wt应用程序,它们同时显示在同一页面上,并且都是活动的。与Google地图小部件相比,Wt的widgetset模式最佳:您在要呈现应用程序的网页上放置一个小占位符(div + some JS)。参见examples / wt-home,examples / feature / widgetset
  2. 在Wt中完全没有问题可以从另一个应用程序中的一个应用程序重用小部件。将要重用的部分放在WContainerWidget(或WCompositeWidget)中,并将其集成到tabwidgets中。实际上,这与您现在正在做的事情非常相似,但不是采用TestApp1的root(),而是组织您的代码,这样您只需将一个小部件放在TestApp1的根目录中,并使用相同的小部件主机应用程序的选项卡。在createApplication中,您不能实例化多个WApplication对象。 Widgetgallery使用这种方法来集成部分图表示例。