Wt examples  4.0.0
Classes | Typedefs | Functions
Chat example

Classes

class  PopupChatWidget
 A popup chat widget. More...
 
class  ChatApplication
 A chat demo application. More...
 
class  ChatWidget
 A chat application widget. More...
 
class  ChatEvent
 Encapsulate a chat event. More...
 
class  SimpleChatServer
 A simple chat server. More...
 
class  SimpleChatWidget
 A self-contained chat widget. More...
 

Typedefs

typedef std::function< void(const ChatEvent &)> ChatEventCallback
 

Functions

std::unique_ptr< Wt::WApplicationcreateApplication (const Wt::WEnvironment &env, SimpleChatServer &server)
 
std::unique_ptr< Wt::WApplicationcreateWidget (const Wt::WEnvironment &env, SimpleChatServer &server)
 
int main (int argc, char **argv)
 
 ChatApplication::ChatApplication (const Wt::WEnvironment &env, SimpleChatServer &server)
 Create a new instance. More...
 
void ChatApplication::javaScriptTest ()
 
void ChatApplication::emptyFunc ()
 
void ChatApplication::addChatWidget ()
 Add another chat client. More...
 
 ChatWidget::ChatWidget (const Wt::WEnvironment &env, SimpleChatServer &server)
 

Detailed Description

Typedef Documentation

typedef std::function<void (const ChatEvent&)> ChatEventCallback

Definition at line 82 of file SimpleChatServer.h.

Function Documentation

void ChatApplication::addChatWidget ( )
private

Add another chat client.

Definition at line 90 of file simpleChat.C.

91 {
92  SimpleChatWidget *chatWidget2 =
93  root()->addWidget(Wt::cpp14::make_unique<SimpleChatWidget>(server_));
94  chatWidget2->setStyleClass("chat");
95 }
SimpleChatServer & server_
Definition: simpleChat.C:33
virtual void setStyleClass(const WString &styleClass) override
A self-contained chat widget.
WContainerWidget * root() const
virtual void addWidget(std::unique_ptr< WWidget > widget)
ChatApplication::ChatApplication ( const Wt::WEnvironment env,
SimpleChatServer server 
)

Create a new instance.

Definition at line 45 of file simpleChat.C.

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(Wt::cpp14::make_unique<Wt::WText>(Wt::WString::tr("introduction")));
59 
60  SimpleChatWidget *chatWidget =
61  root()->addWidget(Wt::cpp14::make_unique<SimpleChatWidget>(server_));
62  chatWidget->setStyleClass("chat");
63 
64  root()->addWidget(Wt::cpp14::make_unique<Wt::WText>(Wt::WString::tr("details")));
65 
66  Wt::WPushButton *b =
67  root()->addWidget(Wt::cpp14::make_unique<Wt::WPushButton>("I'm schizophrenic ..."));
70 }
SimpleChatServer & server_
Definition: simpleChat.C:33
virtual void setStyleClass(const WString &styleClass) override
EventSignal< WMouseEvent > & clicked()
void javaScriptTest()
Definition: simpleChat.C:72
WMessageResourceBundle & messageResourceBundle()
A self-contained chat widget.
void setTitle(const WString &title)
Wt::Signals::connection connect(const F &function)
void addChatWidget()
Add another chat client.
Definition: simpleChat.C:90
static WString tr(const char *key)
void use(const std::string &path, bool loadInMemory=true)
static std::string appRoot()
void useStyleSheet(const WLink &link, const std::string &media="all")
const Wt::WEnvironment & env_
Definition: simpleChat.C:35
WContainerWidget * root() const
virtual void addWidget(std::unique_ptr< WWidget > widget)
WApplication(const WEnvironment &environment)
ChatWidget::ChatWidget ( const Wt::WEnvironment env,
SimpleChatServer server 
)

Definition at line 108 of file simpleChat.C.

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  Wt::cpp14::make_unique<PopupChatWidget>(server, *div);
127  PopupChatWidget *chatWidget = chatWidgetPtr.get();
128  bindWidget(std::move(chatWidgetPtr), *div);
129 
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 }
const std::string * getParameter(const std::string &name) const
void bindWidget(std::unique_ptr< WWidget > widget, const std::string &domId)
WMessageResourceBundle & messageResourceBundle()
std::string javaScriptClass()
Wt::JSignal< Wt::WString > login_
Definition: simpleChat.C:105
void setJavaScriptClass(const std::string &className)
A popup chat widget.
void use(const std::string &path, bool loadInMemory=true)
static std::string appRoot()
void useStyleSheet(const WLink &link, const std::string &media="all")
Wt::Signals::connection connect(const F &function)
void doJavaScript(const std::string &javascript, bool afterLoaded=true)
void setCssTheme(const std::string &name)
void setName(const Wt::WString &name)
std::unique_ptr<Wt::WApplication> createApplication ( const Wt::WEnvironment env,
SimpleChatServer server 
)

Definition at line 142 of file simpleChat.C.

144 {
145  return Wt::cpp14::make_unique<ChatApplication>(env, server);
146 }
std::unique_ptr<Wt::WApplication> createWidget ( const Wt::WEnvironment env,
SimpleChatServer server 
)

Definition at line 148 of file simpleChat.C.

149 {
150  return Wt::cpp14::make_unique<ChatWidget>(env, server);
151 }
void ChatApplication::emptyFunc ( )
private

Definition at line 87 of file simpleChat.C.

88 {}
void ChatApplication::javaScriptTest ( )
private

Definition at line 72 of file simpleChat.C.

73 {
74  if(!env_.javaScript()){
76  root()->addWidget(Wt::cpp14::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_ = Wt::cpp14::make_unique<Wt::WTimer>();
81  timer_->setInterval(std::chrono::milliseconds{5000});
82  timer_->timeout().connect(this, &ChatApplication::emptyFunc);
83  timer_->start();
84  }
85 }
bool javaScript() const
std::unique_ptr< Wt::WTimer > timer_
Definition: simpleChat.C:36
Wt::WText * javaScriptError_
Definition: simpleChat.C:34
static WString tr(const char *key)
void emptyFunc()
Definition: simpleChat.C:87
const Wt::WEnvironment & env_
Definition: simpleChat.C:35
WContainerWidget * root() const
virtual void addWidget(std::unique_ptr< WWidget > widget)
int main ( int  argc,
char **  argv 
)

Definition at line 153 of file simpleChat.C.

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 }
std::unique_ptr< Wt::WApplication > createApplication(const Wt::WEnvironment &env, SimpleChatServer &server)
Definition: simpleChat.C:142
static WT_API int waitForShutdown()
std::unique_ptr< Wt::WApplication > createWidget(const Wt::WEnvironment &env, SimpleChatServer &server)
Definition: simpleChat.C:148
A simple chat server.

Generated on Mon Sep 4 2017 for the C++ Web Toolkit (Wt) by doxygen 1.8.11