7 #include <Wt/WApplication.h> 8 #include <Wt/WContainerWidget.h> 9 #include <Wt/WEnvironment.h> 10 #include <Wt/WPushButton.h> 11 #include <Wt/WServer.h> 13 #include <Wt/WTimer.h> 35 const Wt::WEnvironment&
env_;
52 useStyleSheet(
"chatapp.css");
54 messageResourceBundle().use(appRoot() +
"simplechat");
58 root()->addWidget(Wt::cpp14::make_unique<Wt::WText>(Wt::WString::tr(
"introduction")));
61 root()->addWidget(Wt::cpp14::make_unique<SimpleChatWidget>(
server_));
62 chatWidget->setStyleClass(
"chat");
64 root()->addWidget(Wt::cpp14::make_unique<Wt::WText>(Wt::WString::tr(
"details")));
67 root()->addWidget(Wt::cpp14::make_unique<Wt::WPushButton>(
"I'm schizophrenic ..."));
68 b->clicked().
connect(b, &Wt::WPushButton::hide);
74 if(!
env_.javaScript()){
76 root()->addWidget(Wt::cpp14::make_unique<Wt::WText>(Wt::WString::tr(
"serverpushwarning")));
80 timer_ = Wt::cpp14::make_unique<Wt::WTimer>();
81 timer_->setInterval(std::chrono::milliseconds{5000});
93 root()->addWidget(Wt::cpp14::make_unique<SimpleChatWidget>(
server_));
94 chatWidget2->setStyleClass(
"chat");
109 :
Wt::WApplication(env),
110 login_(this,
"login")
113 useStyleSheet(
"chatwidget.css");
114 useStyleSheet(
"chatwidget_ie6.css",
"lt IE 7");
116 messageResourceBundle().use(appRoot() +
"simplechat");
118 const std::string *div = env.getParameter(
"div");
119 std::string defaultDiv =
"div";
124 setJavaScriptClass(*div);
125 std::unique_ptr<PopupChatWidget> chatWidgetPtr =
126 Wt::cpp14::make_unique<PopupChatWidget>(server, *div);
128 bindWidget(std::move(chatWidgetPtr), *div);
132 std::string chat = javaScriptClass();
133 doJavaScript(
"if (window." + chat +
"User) " 134 + chat +
".emit(" + chat +
", 'login', " + chat +
"User);" 135 +
"document.body.appendChild(" + chatWidget->jsRef() +
");");
137 std::cerr <<
"Missing: parameter: 'div'" << std::endl;
145 return Wt::cpp14::make_unique<ChatApplication>(env, server);
150 return Wt::cpp14::make_unique<ChatWidget>(env, server);
153 int main(
int argc,
char **argv)
155 Wt::WServer server(argc, argv, WTHTTP_CONFIGURATION);
162 server.addEntryPoint(Wt::EntryPointType::Application,
164 std::ref(chatServer)));
165 server.addEntryPoint(Wt::EntryPointType::WidgetSet,
167 std::ref(chatServer)),
"/chat.js");
169 if (server.start()) {
170 int sig = Wt::WServer::waitForShutdown();
171 std::cerr <<
"Shutting down: (signal = " << sig <<
")" << std::endl;
SimpleChatServer & server_
int main(int argc, char **argv)
std::unique_ptr< Wt::WApplication > createApplication(const Wt::WEnvironment &env, SimpleChatServer &server)
ChatWidget(const Wt::WEnvironment &env, SimpleChatServer &server)
std::unique_ptr< Wt::WTimer > timer_
Wt::WText * javaScriptError_
void addChatWidget()
Add another chat client.
ChatApplication(const Wt::WEnvironment &env, SimpleChatServer &server)
Create a new instance.
const Wt::WEnvironment & env_
std::unique_ptr< Wt::WApplication > createWidget(const Wt::WEnvironment &env, SimpleChatServer &server)