所以最近我问了一个问题,看看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);
}
答案 0 :(得分:2)
虽然Wt可以实现,但您的方法并非100%正确。 AFAIK你有两个选择: