Project

General

Profile

Bug #5694 ยป ackidtest.cpp

Roel Standaert, 05/03/2017 05:40 PM

 
#include <Wt/WApplication>
#include <Wt/WBoostAny>
#include <Wt/WContainerWidget>
#include <Wt/WPushButton>
#include <Wt/WServer>
#include <Wt/WText>

#include <atomic>
#include <chrono>
#include <thread>

// Compile with -std=c++14
//
// Demonstrates ackId issue, set
// server -> application-settings -> session-management -> server-push-timeout
// to 3 to test
//
// Works fine when server-push-timeout > 3

class App : public Wt::WApplication {
public:
App(const Wt::WEnvironment &env)
: WApplication{env},
stopped_{false}
{
Wt::WPushButton *btn = new Wt::WPushButton(Wt::utf8("Start"));
root()->addWidget(btn);

Wt::WText *txt = new Wt::WText();
root()->addWidget(txt);

btn->clicked().connect(std::bind([this,txt,btn]{
using namespace std::chrono_literals;
enableUpdates(true);
t_ = std::thread([this,txt]{
int i = 0;
while (!stopped_) {
std::this_thread::sleep_for(2500ms);
// poll request has not timed out on client side yet (+/- 500ms left)
UpdateLock l{this};
txt->setText(Wt::asString(i++));
std::this_thread::sleep_for(600ms);
// poll request has timed out, so we won't be able
// to send the triggered update
triggerUpdate();
}
});
delete btn;
}));
}

virtual ~App()
{
stopped_ = true;
if (t_.joinable())
t_.join();
}

private:
std::thread t_;
std::atomic<bool> stopped_;
};

int main(int argc, char *argv[])
{
return Wt::WRun(argc, argv, [](const Wt::WEnvironment &env) {
return new App(env);
});
}
    (1-1/1)