Wt examples
3.3.2
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
koen
project
wt
public-git
wt
examples
simplechat
simpleChat.C
Go to the documentation of this file.
1
/*
2
* Copyright (C) 2008 Emweb bvba, Heverlee, Belgium.
3
*
4
* See the LICENSE file for terms of use.
5
*/
6
7
#include <Wt/WApplication>
8
#include <Wt/WContainerWidget>
9
#include <Wt/WEnvironment>
10
#include <Wt/WPushButton>
11
#include <Wt/WServer>
12
#include <Wt/WText>
13
#include <Wt/WTimer>
14
15
#include "
SimpleChatServer.h
"
16
#include "
PopupChatWidget.h
"
17
18
using namespace
Wt;
19
24
27
class
ChatApplication
:
public
WApplication
28
{
29
public
:
32
ChatApplication
(
const
WEnvironment
& env,
SimpleChatServer
& server);
33
34
private
:
35
SimpleChatServer
&
server_
;
36
Wt::WText
*
javaScriptError_
;
37
const
WEnvironment
&
env_
;
38
Wt::WTimer
*
timer_
;
39
42
void
addChatWidget();
43
void
javaScriptTest();
44
void
emptyFunc();
45
};
46
47
ChatApplication::ChatApplication
(
const
WEnvironment
& env,
48
SimpleChatServer
& server)
49
:
WApplication
(env),
50
server_(server),
51
env_(env)
52
{
53
setTitle
(
"Wt Chat"
);
54
useStyleSheet
(
"chatapp.css"
);
55
56
messageResourceBundle
().
use
(
appRoot
() +
"simplechat"
);
57
58
javaScriptTest
();
59
60
root
()->addWidget(
new
WText
(WString::tr(
"introduction"
)));
61
62
SimpleChatWidget
*chatWidget =
63
new
SimpleChatWidget
(
server_
,
root
());
64
chatWidget->
setStyleClass
(
"chat"
);
65
66
root
()->addWidget(
new
WText
(WString::tr(
"details"
)));
67
68
WPushButton
*b =
new
WPushButton
(
"I'm schizophrenic ..."
,
root
());
69
b->clicked().connect(b, &WPushButton::hide);
70
b->clicked().connect(
this
, &
ChatApplication::addChatWidget
);
71
}
72
73
void
ChatApplication::javaScriptTest
()
74
{
75
if
(!
env_
.
javaScript
()){
76
javaScriptError_
=
new
WText
(WString::tr(
"serverpushwarning"
),
root
());
77
78
// The 5 second timer is a fallback for real server push. The updated
79
// server state will piggy back on the response to this timeout.
80
timer_
=
new
Wt::WTimer
(
root
());
81
timer_
->
setInterval
(5000);
82
timer_
->
timeout
().connect(
this
, &
ChatApplication::emptyFunc
);
83
timer_
->
start
();
84
}
85
}
86
87
void
ChatApplication::emptyFunc
()
88
{}
89
90
void
ChatApplication::addChatWidget
()
91
{
92
SimpleChatWidget
*chatWidget2 =
new
SimpleChatWidget
(
server_
,
root
());
93
chatWidget2->
setStyleClass
(
"chat"
);
94
}
95
98
class
ChatWidget
:
public
WApplication
99
{
100
public
:
101
ChatWidget
(
const
WEnvironment
& env,
SimpleChatServer
& server);
102
103
private
:
104
JSignal<WString>
login_
;
105
};
106
107
ChatWidget::ChatWidget
(
const
WEnvironment
& env,
SimpleChatServer
& server)
108
:
WApplication
(env),
109
login_(this,
"login"
)
110
{
111
setCssTheme
(
""
);
112
useStyleSheet
(
"chatwidget.css"
);
113
useStyleSheet
(
"chatwidget_ie6.css"
,
"lt IE 7"
);
114
115
messageResourceBundle
().
use
(
appRoot
() +
"simplechat"
);
116
117
const
std::string *div = env.
getParameter
(
"div"
);
118
std::string defaultDiv =
"div"
;
119
if
(!div)
120
div = &defaultDiv;
121
122
if
(div) {
123
setJavaScriptClass
(*div);
124
PopupChatWidget
*chatWidget =
new
PopupChatWidget
(server, *div);
125
bindWidget
(chatWidget, *div);
126
127
login_
.
connect
(chatWidget, &
PopupChatWidget::setName
);
128
129
std::string chat =
javaScriptClass
();
130
doJavaScript
(
"if (window."
+ chat +
"User) "
131
+ chat +
".emit("
+ chat +
", 'login', "
+ chat +
"User);"
132
+
"document.body.appendChild("
+ chatWidget->jsRef() +
");"
);
133
}
else
{
134
std::cerr <<
"Missing: parameter: 'div'"
<< std::endl;
135
quit
();
136
}
137
}
138
139
WApplication
*
createApplication
(
const
WEnvironment
& env,
140
SimpleChatServer
& server)
141
{
142
return
new
ChatApplication
(env, server);
143
}
144
145
WApplication
*
createWidget
(
const
WEnvironment
& env,
SimpleChatServer
& server)
146
{
147
return
new
ChatWidget
(env, server);
148
}
149
150
int
main
(
int
argc,
char
**argv)
151
{
152
Wt::WServer
server(argv[0]);
153
SimpleChatServer
chatServer(server);
154
155
server.
setServerConfiguration
(argc, argv, WTHTTP_CONFIGURATION);
156
157
/*
158
* We add two entry points: one for the full-window application,
159
* and one for a widget that can be integrated in another page.
160
*/
161
server.
addEntryPoint
(
Wt::Application
,
162
boost::bind(
createApplication
, _1,
163
boost::ref(chatServer)));
164
server.
addEntryPoint
(
Wt::WidgetSet
,
165
boost::bind(
createWidget
, _1,
166
boost::ref(chatServer)),
"/chat.js"
);
167
168
if
(server.
start
()) {
169
int
sig =
Wt::WServer::waitForShutdown
();
170
std::cerr <<
"Shutting down: (signal = "
<< sig <<
")"
<< std::endl;
171
server.
stop
();
172
}
173
}
174
Generated on Wed Mar 12 2014 for
the C++ Web Toolkit (Wt)
by
1.8.4