Wt examples 4.7.1
simpleChat.C
Go to the documentation of this file.
1/*
2 * Copyright (C) 2008 Emweb bv, Herent, Belgium.
3 *
4 * See the LICENSE file for terms of use.
5 */
6
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>
12#include <Wt/WText.h>
13#include <Wt/WTimer.h>
14
15#include "SimpleChatServer.h"
16#include "PopupChatWidget.h"
17
22
25class ChatApplication : public Wt::WApplication
26{
27public:
30 ChatApplication(const Wt::WEnvironment& env, SimpleChatServer& server);
31
32private:
34 Wt::WText *javaScriptError_;
35 const Wt::WEnvironment& env_;
36 std::unique_ptr<Wt::WTimer> timer_;
37
40 void addChatWidget();
41 void javaScriptTest();
42 void emptyFunc();
43};
44
45ChatApplication::ChatApplication(const Wt::WEnvironment& env,
46 SimpleChatServer& server)
47 : WApplication(env),
48 server_(server),
49 env_(env)
50{
51 setTitle("Wt Chat");
52 useStyleSheet("chatapp.css");
53
54 messageResourceBundle().use(appRoot() + "simplechat");
55
57
58 root()->addWidget(std::make_unique<Wt::WText>(Wt::WString::tr("introduction")));
59
60 SimpleChatWidget *chatWidget =
61 root()->addWidget(std::make_unique<SimpleChatWidget>(server_));
62 chatWidget->setStyleClass("chat");
63
64 root()->addWidget(std::make_unique<Wt::WText>(Wt::WString::tr("details")));
65
66 Wt::WPushButton *b =
67 root()->addWidget(std::make_unique<Wt::WPushButton>("I'm schizophrenic ..."));
68 b->clicked().connect(b, &Wt::WPushButton::hide);
69 b->clicked().connect(this, &ChatApplication::addChatWidget);
70}
71
73{
74 if(!env_.javaScript()){
76 root()->addWidget(std::make_unique<Wt::WText>(Wt::WString::tr("serverpushwarning")));
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_ = std::make_unique<Wt::WTimer>();
81 timer_->setInterval(std::chrono::milliseconds{5000});
82 timer_->timeout().connect(this, &ChatApplication::emptyFunc);
83 timer_->start();
84 }
85}
86
88{}
89
91{
92 SimpleChatWidget *chatWidget2 =
93 root()->addWidget(std::make_unique<SimpleChatWidget>(server_));
94 chatWidget2->setStyleClass("chat");
95}
96
99class ChatWidget : public Wt::WApplication
100{
101public:
102 ChatWidget(const Wt::WEnvironment& env, SimpleChatServer& server);
103
104private:
105 Wt::JSignal<Wt::WString> login_;
106};
107
108ChatWidget::ChatWidget(const Wt::WEnvironment& env, SimpleChatServer& server)
109 : Wt::WApplication(env),
110 login_(this, "login")
111{
112 setCssTheme("");
113 useStyleSheet("chatwidget.css");
114 useStyleSheet("chatwidget_ie6.css", "lt IE 7");
115
116 messageResourceBundle().use(appRoot() + "simplechat");
117
118 const std::string *div = env.getParameter("div");
119 std::string defaultDiv = "div";
120 if (!div)
121 div = &defaultDiv;
122
123 if (div) {
124 setJavaScriptClass(*div);
125 std::unique_ptr<PopupChatWidget> chatWidgetPtr =
126 std::make_unique<PopupChatWidget>(server, *div);
127 PopupChatWidget *chatWidget = chatWidgetPtr.get();
128 bindWidget(std::move(chatWidgetPtr), *div);
129
130 login_.connect(chatWidget, &PopupChatWidget::setName);
131
132 std::string chat = javaScriptClass();
133 doJavaScript("if (window." + chat + "User) "
134 + chat + ".emit(" + chat + ", 'login', " + chat + "User);"
135 + "document.body.appendChild(" + chatWidget->jsRef() + ");");
136 } else {
137 std::cerr << "Missing: parameter: 'div'" << std::endl;
138 quit();
139 }
140}
141
142std::unique_ptr<Wt::WApplication> createApplication(const Wt::WEnvironment& env,
143 SimpleChatServer& server)
144{
145 return std::make_unique<ChatApplication>(env, server);
146}
147
148std::unique_ptr<Wt::WApplication> createWidget(const Wt::WEnvironment& env, SimpleChatServer& server)
149{
150 return std::make_unique<ChatWidget>(env, server);
151}
152
153int main(int argc, char **argv)
154{
155 Wt::WServer server(argc, argv, WTHTTP_CONFIGURATION);
156 SimpleChatServer chatServer(server);
157
158 /*
159 * We add two entry points: one for the full-window application,
160 * and one for a widget that can be integrated in another page.
161 */
162 server.addEntryPoint(Wt::EntryPointType::Application,
163 std::bind(createApplication, std::placeholders::_1,
164 std::ref(chatServer)));
165 server.addEntryPoint(Wt::EntryPointType::WidgetSet,
166 std::bind(createWidget, std::placeholders::_1,
167 std::ref(chatServer)), "/chat.js");
168
169 if (server.start()) {
170 int sig = Wt::WServer::waitForShutdown();
171 std::cerr << "Shutting down: (signal = " << sig << ")" << std::endl;
172 server.stop();
173 }
174}
175
A chat demo application.
Definition: simpleChat.C:26
Wt::WText * javaScriptError_
Definition: simpleChat.C:34
void addChatWidget()
Add another chat client.
Definition: simpleChat.C:90
void emptyFunc()
Definition: simpleChat.C:87
SimpleChatServer & server_
Definition: simpleChat.C:33
const Wt::WEnvironment & env_
Definition: simpleChat.C:35
ChatApplication(const Wt::WEnvironment &env, SimpleChatServer &server)
Create a new instance.
Definition: simpleChat.C:45
std::unique_ptr< Wt::WTimer > timer_
Definition: simpleChat.C:36
void javaScriptTest()
Definition: simpleChat.C:72
A chat application widget.
Definition: simpleChat.C:100
ChatWidget(const Wt::WEnvironment &env, SimpleChatServer &server)
Definition: simpleChat.C:108
Wt::JSignal< Wt::WString > login_
Definition: simpleChat.C:105
A popup chat widget.
void setName(const Wt::WString &name)
A simple chat server.
A self-contained chat widget.
std::unique_ptr< Wt::WApplication > createApplication(const Wt::WEnvironment &env, SimpleChatServer &server)
Definition: simpleChat.C:142
int main(int argc, char **argv)
Definition: simpleChat.C:153
std::unique_ptr< Wt::WApplication > createWidget(const Wt::WEnvironment &env, SimpleChatServer &server)
Definition: simpleChat.C:148

Generated on Fri May 6 2022 for the C++ Web Toolkit (Wt) by doxygen 1.9.3