2013-04-11 171 views
3

更新1:我最近发现WT使用TCP(HTTP)进行通信,如果这有助于任何人。多个WT应用程序可以在同一个端口上运行吗?

标题说明了一切,是否可以在同一端口上运行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应用程序?

非常感谢您的帮助!

回答

7

是的,你可以做到这一点,但你需要创建自己的WRun并使用addEntryPoint。这在实际上tutorial提到,去如下:

内WRun()

WRun()实际上是创建和配置WServer实例的便利功能。如果您想要更多的控制权,例如,如果您有多个“入口点”,或者想要控制服务器的启动和停止,则可以直接使用WServer API。

这里有一个例子,两个应用程序都是Hello World应用程序,但请注意,它们在按钮上以及按下时有不同的标题和不同的消息。您可以在src/http/Serve.Csrc/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上访问其他应用程序。

+0

好吧,所以我玩了你发布的代码,这是我想出的。 如果在服务器启动之前创建入口点,则可以在同一端口上运行多个WT应用程序。最简单的方法是让两个WT应用程序在同一个项目中,这样服务器就可以创建入口点。 如果在服务器启动之前没有创建入口点,那么在同一端口上运行多个WT应用程序是不可能的,而如果两个应用程序在两个不同的项目中完全分离,则无法做到这一点。 你能确认这是否正确? – user2115945 2013-04-15 11:45:02

+0

另外,你发布的例子非常容易阅读和理解,你太棒了! – user2115945 2013-04-15 11:45:58

+0

这是正确的,如果您将两个应用放在同一个项目中,并在启动服务器之前设置其入口点,它应该像魅力一样工作。 – 2013-04-15 16:45:10

相关问题