是的,你可以做到这一点,但你需要创建自己的WRun
并使用addEntryPoint
。这在实际上tutorial提到,去如下:
内WRun()
WRun()实际上是创建和配置WServer实例的便利功能。如果您想要更多的控制权,例如,如果您有多个“入口点”,或者想要控制服务器的启动和停止,则可以直接使用WServer API。
这里有一个例子,两个应用程序都是Hello World应用程序,但请注意,它们在按钮上以及按下时有不同的标题和不同的消息。您可以在src/http/Serve.C
和src/Wt/WServer
上找到WRun
的另一个实施。
two_helloworlds.cc
#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>
class HelloApplication : public Wt::WApplication
{
public:
HelloApplication(const Wt::WEnvironment& env, const std::string& title);
private:
Wt::WLineEdit *nameEdit_;
Wt::WText *greeting_;
void greet();
};
HelloApplication::HelloApplication(const Wt::WEnvironment& env, const std::string& title)
: Wt::WApplication(env)
{
setTitle(title);
root()->addWidget(new Wt::WText("Your name, please ? "));
nameEdit_ = 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, &HelloApplication::greet);
}
void HelloApplication::greet()
{ greeting_->setText("Hello there, " + nameEdit_->text());
}
class GoodbyeApplication : public Wt::WApplication{
public:
GoodbyeApplication(const Wt::WEnvironment& env, const std::string& title);
private: Wt::WLineEdit *nameEdit_;
Wt::WText *greeting_;
void greet();
};
GoodbyeApplication::GoodbyeApplication(const Wt::WEnvironment& env, const std::string& title)
: Wt::WApplication(env)
{
setTitle(title);
root()->addWidget(new Wt::WText("Your name, please ? "));
nameEdit_ = 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, &GoodbyeApplication::greet);
}
void GoodbyeApplication::greet()
{
greeting_->setText("Goodbye, " + nameEdit_->text());
}
Wt::WApplication *createApplication(const Wt::WEnvironment& env)
{
return new HelloApplication(env, "First app");
}
Wt::WApplication *createSecondApplication(const Wt::WEnvironment& env)
{
return new GoodbyeApplication(env, "Second app");
}
int YourWRun(int argc, char *argv[], Wt::ApplicationCreator createApplication, Wt::ApplicationCreator createSecondApplication)
{
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, createApplication);
server.addEntryPoint(Wt::Application, createSecondApplication,"/second");
if (server.start()) {
int sig = Wt::WServer::waitForShutdown(argv[0]);
std::cerr << "Shutdown (signal = " << sig << ")" << std::endl;
server.stop();
/*
if (sig == SIGHUP)
WServer::restart(argc, argv, environ);
*/
}
} 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)
{
return YourWRun(argc, argv, &createApplication, &createSecondApplication);
}
与
g++ -g -o two_helloworlds two_helloworlds.cc -I/usr/local/include -L/usr/local/lib -lwthttp -lwt -lboost_random -lboost_regex -lboost_signals -lboost_system -lboost_thread -lboost_filesystem -lboost_program_options -lboost_date_time
编译并与
./two_helloworlds --docroot . --http-address 0.0.0.0 --http-port 8080
上执行localhost:8080
您将访问其中一个应用程序,并在localhost:8080/second
上访问其他应用程序。
好吧,所以我玩了你发布的代码,这是我想出的。 如果在服务器启动之前创建入口点,则可以在同一端口上运行多个WT应用程序。最简单的方法是让两个WT应用程序在同一个项目中,这样服务器就可以创建入口点。 如果在服务器启动之前没有创建入口点,那么在同一端口上运行多个WT应用程序是不可能的,而如果两个应用程序在两个不同的项目中完全分离,则无法做到这一点。 你能确认这是否正确? – user2115945 2013-04-15 11:45:02
另外,你发布的例子非常容易阅读和理解,你太棒了! – user2115945 2013-04-15 11:45:58
这是正确的,如果您将两个应用放在同一个项目中,并在启动服务器之前设置其入口点,它应该像魅力一样工作。 – 2013-04-15 16:45:10