serverpush.cc

Koen Deforche, 02/26/2010 03:02 pm

Download (1.5 KB)

 
1
#include <Wt/WApplication>
2
#include <Wt/WContainerWidget>
3
#include <Wt/WPushButton>
4
#include <Wt/WLineEdit>
5
6
#include <iostream>
7
#include <boost/thread.hpp>
8
9
using namespace Wt;
10
using namespace boost;
11
using namespace std;
12
13
class ExampleWidget;
14
15
class CometTestApplication : public WApplication
16
{
17
public:
18
  CometTestApplication(const WEnvironment& env);
19
    ExampleWidget *t;
20
};
21
22
class ExampleWidget : public WLineEdit
23
{
24
    boost::thread updater;
25
   
26
public:
27
    void startBigWork()
28
    {
29
       wApp->enableUpdates(true); // enable server-push temporarily
30
31
       updater = boost::thread(boost::bind(&ExampleWidget::doWork, this, WApplication::instance(), text()));
32
    }
33
34
    ExampleWidget()
35
    {
36
       keyWentUp().connect(SLOT(this, ExampleWidget::startBigWork));
37
    }
38
39
    void doWork(WApplication *app, const WString s)
40
    {
41
      WApplication::UpdateLock uiLock = app->getUpdateLock(); // RAII lock
42
      cout << endl << s << endl;
43
       
44
      setText(s + " " + s);
45
46
      cout << endl << text() << endl;
47
48
      // if using server push, push now the changes to the client
49
      app->triggerUpdate();
50
51
      app->enableUpdates(false); // disable server-push again
52
   }
53
};
54
55
CometTestApplication::CometTestApplication(const WEnvironment& env)
56
  : WApplication(env)
57
{
58
    t = new ExampleWidget();
59
   
60
    root()->addWidget(t);
61
}
62
63
WApplication *createApplication(const WEnvironment& env)
64
{
65
  return new CometTestApplication(env);
66
}
67
68
int main(int argc, char **argv)
69
{
70
  return WRun(argc, argv, &createApplication);
71
}