更新1:我最近发现WT使用TCP(HTTP)进行通信,如果这有助于任何人。
标题说明了一遍,是否可以在同一个端口上运行2个不同的WT应用程序或项目?我知道WT已经通过其启动参数来控制应用程序的托管方式,如下所示。我正在使用Visual Studio 2010,并在debugging->命令参数框中输入以下参数,如下所示:
--http-address=0.0.0.0 --http-port=8080 --deploy-path=/hello --docroot=.
以上参数将要求用户在
打开网页http://127.0.0.1:8080/hello
所以,不过,如果我主持另一个具有以下参数的项目
,那该怎么办?--http-address=0.0.0.0 --http-port=8080 --deploy-path=/world --docroot=.
所以我需要连接到
http://127.0.0.1:8080/world
以上实际上不起作用,它只承载连接的第一个应用程序,第二个应用程序在第一个关闭之前不会连接。所以这就把我带到了这里。有没有其他方法可以在同一个端口上运行多个WT应用程序?
提前感谢您的帮助!
答案 0 :(得分:7)
是的,您可以实现这一目标,但您需要创建自己的WRun
并使用addEntryPoint
。实际上在tutorial中提到了这一点,如下所示:
内部WRun()
WRun()实际上是一个创建和配置WServer实例的便捷函数。如果您想要更多控制,例如,如果您有多个“入口点”,或者想要控制服务器启动和停止,则可以直接使用WServer API。
这里有一个示例,两个应用程序都是Hello World应用程序,但请注意它们在按钮上按下时有不同的标题和不同的消息。您可以在WRun
和src/http/Serve.C
上找到src/Wt/WServer
的其他实现。
<强> 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
您将访问另一个应用程序。