加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 多个WT应用程序可以在同一端口上运行吗?

发布时间:2020-12-16 09:44:06 所属栏目:百科 来源:网络整理
导读:更新1:我最近发现WT使用TCP(HTTP)进行通信,如果这有助于任何人. 标题说明了一遍,是否可以在同一个端口上运行2个不同的WT应用程序或项目?我知道WT已经通过其启动参数来控制应用程序的托管方式,如下所示.我正在使用Visual Studio 2010,并在debugging-命令参
更新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应用程序?

预先感谢您的任何帮助!

解决方法

是的,您可以实现这一目标,但您需要创建自己的WRun并使用addEntryPoint.这实际上是在 tutorial中提到的,如下所示:

Inside WRun()

WRun() is actually a convenience function which creates and configures a WServer instance. If you want more control,for example if you have multiple “entry points”,or want to control the server starting and stopping,you can use the WServer API directly instead.

这里有一个示例,两个应用程序都是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,please ? "));
    nameEdit_ = new Wt::WLineEdit(root());
    Wt::WPushButton *button = new Wt::WPushButton("Say goodbye.",&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,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,&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 /秒你将访问另一个.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读