Wt examples  4.0.5
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::WApplication > createApplication (const Wt::WEnvironment &env, SimpleChatServer &server)
 
std::unique_ptr< Wt::WApplication > createWidget (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

◆ ChatEventCallback

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

Definition at line 80 of file SimpleChatServer.h.

Function Documentation

◆ addChatWidget()

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
A self-contained chat widget.

◆ ChatApplication()

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 ..."));
68  b->clicked().connect(b, &Wt::WPushButton::hide);
69  b->clicked().connect(this, &ChatApplication::addChatWidget);
70 }
SimpleChatServer & server_
Definition: simpleChat.C:33
void javaScriptTest()
Definition: simpleChat.C:72
A self-contained chat widget.
void addChatWidget()
Add another chat client.
Definition: simpleChat.C:90
const Wt::WEnvironment & env_
Definition: simpleChat.C:35

◆ ChatWidget()

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 
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 }
Wt::JSignal< Wt::WString > login_
Definition: simpleChat.C:105
A popup chat widget.
void setName(const Wt::WString &name)

◆ createApplication()

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 }

◆ createWidget()

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 }

◆ emptyFunc()

void ChatApplication::emptyFunc ( )
private

Definition at line 87 of file simpleChat.C.

88 {}

◆ javaScriptTest()

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 }
std::unique_ptr< Wt::WTimer > timer_
Definition: simpleChat.C:36
Wt::WText * javaScriptError_
Definition: simpleChat.C:34
void emptyFunc()
Definition: simpleChat.C:87
const Wt::WEnvironment & env_
Definition: simpleChat.C:35

◆ main()

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

Generated on Mon May 20 2019 for the C++ Web Toolkit (Wt) by doxygen 1.8.14