2016-09-01 2 views
1

저는 새로운 기능으로 웹 인터페이스를 Lagacy C++ 프로그램에 추가하기 시작했습니다. hello_world 예제가 정상적으로 작동합니다.작성한 웹 페이지를 wt (C++)로 업데이트하는 방법

주어진 예는 모두 웹 페이지를 만들고 페이지가 웹 페이지의 이벤트 (예 : 버튼, 틱 상자)에 반응 할 수 있기 때문에 세션을 시작한 후에 웹 페이지를 수정하고 싶습니다. HMI가 웹 페이지의 버튼 대신 데이터 변경에 응답하는 것과 비슷합니다.

야생형 문서가 말한대로 그것은, 행할 수 있어야한다 : 실제 요청 처리 및 렌더링이 추출되고, 전체 페이지 렌더링 모델 (일반 HTML) 또는 증분 업데이트 (아약스/WebSocket을)를 사용할 수 있다는 장점으로와 구성 및 브라우저 속성에 따라 다릅니다.

I는는 "UPDATETEXT"방법 인 Hello_World에 추가한다 : 여기

class HelloApplication : public WApplication 
{ 
public: 
    HelloApplication(const WEnvironment& env); 
    void updateText(std::string value); // I add this and rest are from helloworld 
private: 
    WLineEdit *nameEdit_; 
    WText *greeting_; 
    void greet(); 
}; 

는 구현이다

HelloApplication::HelloApplication(const WEnvironment& env) 
: WApplication(env) 
{ 
    setTitle("Trading Platform Status");        // application title 

    root()->addWidget(new WText("Starting... ")); // show some text 
    nameEdit_ = new WLineEdit(root());      // allow text input 
    nameEdit_->setFocus();         // give focus 

    WPushButton *button 
     = new WPushButton("Greet me.", root());    // create a button 
    button->setMargin(5, Left);       // add 5 pixels margin 

    root()->addWidget(new WBreak());      // insert a line break 

    greeting_ = new WText(root());       // empty text 


    /* Connect signals with slots - simple Wt-way*/ 
    button->clicked().connect(this, &HelloApplication::greet); 

    /* using an arbitrary function object (binding values with boost::bind())*/ 
    nameEdit_->enterPressed().connect 
     (boost::bind(&HelloApplication::greet, this)) 
} 

void HelloApplication::greet() { 
    /*Update the text, using text input into the nameEdit_ field.*/ 
    greeting_->setText("Hello there, " + nameEdit_->text()); 
} 

인사() 원래 helloworld를으로부터이고, I는 UPDATETEXT 수단을 추가.

void HelloApplication::updateText(std::string value) 
{ 
    /* 
    * Update the text, using text input into the nameEdit_ field. 
    */ 
    greeting_->setText(value); 
} 

WApplication *createApplication(const WEnvironment& env) 
{ 
    /* 
    * You could read information from the environment to decide whether 
    * the user has permission to start a new application 
    */ 
    return new HelloApplication(env); 
} 

주에서는 별도의 스레드에서 호스트를 시작합니다.

매뉴얼은 지적했습니다 항상에서

는 WApplication 인스턴스는 정적 메소드 WApplication :() 인스턴스를 사용하여 액세스 및 시작 인수 및 설정을 검사하는 데 유용합니다 (WApplication :: 환경을 사용하여(), 응용 프로그램 제목 (WApplication :: setTitle())을 설정하거나 변경하고, 렌더링을위한 로켈 (WApplication :: setLocale()) 및 기타 많은 응용 프로그램 전반 설정을 지정합니다. 멀티 스레드 환경에서이 인스턴스에 대한 액세스는 스레드 로컬 저장소를 사용하여 구현됩니다.

int main(int argc, char* argv[]) 
{ 
    //start in new thread or it blocks the following work 
    thread website_thread(&WRun,argc, argv, &createApplication); 
    //balabalabala some work 
    ((HelloApplication*)WApplication::instance())->updateText("Finished"); 
    //balabala more work 
    return 0 
} 

"this"가 null이므로 updateText가 실패합니다. 분명히 작업을 수행하는 올바른 방법이 아닙니다. 어떤 제안?

+0

당신은 몇 가지 샘플 프로그램에서 찾아야한다 -이 하나의 질문에 넣어 질문과 오해를 많이있을 것 같다. 샘플을 실제로 만드는 것이 다음 단계입니다. –

답변

1

에 당신이 필요합니다 :

  • 하면, 예를 들어, 신호하려는 세션의 목록을 유지 WApplication이 생성 될 때 WApplication 객체를 전역 목록에 넣고 소멸자에서 제거합니다. 이 작업을 더 세밀하게 수행 할 수도 있습니다.
  • 서버 쪽 이벤트 (단순히 WApplication :: enableUpdates()를 호출)로 업데이트 할 수있는 각 응용 프로그램에 대해 서버 밀어 넣기 활성화
  • 서버 측을 수정할 때 위젯 트리가 잠길 수 있습니다. WApplication :: updateLock을 움켜 잡거나 WServer :: post()를 사용하여 세션의 컨텍스트에서 함수의 실행을 게시합니다.
  • 위젯 트리가 수정되었을 때 WApplication :: triggerUpdate()를 호출하여 변경 사항을 적용합니다. 클라이언트에 푸시됩니다.

또한 wt_config.xml 파일에서 websockets 지원을 사용하고자 할 수 있습니다.

간단한 예제는이 모든 것을 잘 보여줍니다.

WApplication :: 인스턴스는 스레드가 세션 객체를 처리하기 위해 할당 될 때 중량에 의해 설정됩니다 스레드 로컬 저장소를 사용하기 때문에 메인 스레드에서 널 (null)을 반환 정상입니다.