Wt examples  3.3.5
/usr/src/RPM/BUILD/wt-3.3.5-rc2/examples/simplechat/simpleChat.C
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 Emweb bvba, Heverlee, Belgium.
00003  *
00004  * See the LICENSE file for terms of use.
00005  */
00006 
00007 #include <Wt/WApplication>
00008 #include <Wt/WContainerWidget>
00009 #include <Wt/WEnvironment>
00010 #include <Wt/WPushButton>
00011 #include <Wt/WServer>
00012 #include <Wt/WText>
00013 #include <Wt/WTimer>
00014 
00015 #include "SimpleChatServer.h"
00016 #include "PopupChatWidget.h"
00017 
00018 using namespace Wt;
00019 
00024 
00027 class ChatApplication : public WApplication
00028 {
00029 public:
00032   ChatApplication(const WEnvironment& env, SimpleChatServer& server);
00033 
00034 private:
00035   SimpleChatServer& server_;
00036    Wt::WText *javaScriptError_;
00037    const WEnvironment& env_;
00038    Wt::WTimer *timer_;
00039 
00042   void addChatWidget();
00043   void javaScriptTest();
00044   void emptyFunc();
00045 };
00046 
00047 ChatApplication::ChatApplication(const WEnvironment& env,
00048                                  SimpleChatServer& server)
00049   : WApplication(env),
00050     server_(server),
00051     env_(env)
00052 {
00053   setTitle("Wt Chat");
00054   useStyleSheet("chatapp.css");
00055 
00056   messageResourceBundle().use(appRoot() + "simplechat");
00057 
00058   javaScriptTest();
00059 
00060   root()->addWidget(new WText(WString::tr("introduction")));
00061 
00062   SimpleChatWidget *chatWidget =
00063       new SimpleChatWidget(server_, root());
00064   chatWidget->setStyleClass("chat");
00065 
00066   root()->addWidget(new WText(WString::tr("details")));
00067 
00068   WPushButton *b = new WPushButton("I'm schizophrenic ...", root());
00069   b->clicked().connect(b, &WPushButton::hide);
00070   b->clicked().connect(this, &ChatApplication::addChatWidget);
00071 }
00072 
00073 void ChatApplication::javaScriptTest()
00074 {
00075   if(!env_.javaScript()){
00076     javaScriptError_ = new WText(WString::tr("serverpushwarning"), root());
00077 
00078     // The 5 second timer is a fallback for real server push. The updated
00079     // server state will piggy back on the response to this timeout.
00080     timer_ = new Wt::WTimer(root());
00081     timer_->setInterval(5000);
00082     timer_->timeout().connect(this, &ChatApplication::emptyFunc);
00083     timer_->start();
00084   }
00085 }
00086 
00087 void ChatApplication::emptyFunc()
00088 {}
00089 
00090 void ChatApplication::addChatWidget()
00091 {
00092   SimpleChatWidget *chatWidget2 = new SimpleChatWidget(server_, root());
00093   chatWidget2->setStyleClass("chat");
00094 }
00095 
00098 class ChatWidget : public WApplication
00099 {
00100 public:
00101   ChatWidget(const WEnvironment& env, SimpleChatServer& server);
00102 
00103 private:
00104   JSignal<WString> login_;
00105 };
00106 
00107 ChatWidget::ChatWidget(const WEnvironment& env, SimpleChatServer& server)
00108   : WApplication(env),
00109     login_(this, "login")
00110 {
00111   setCssTheme("");
00112   useStyleSheet("chatwidget.css");
00113   useStyleSheet("chatwidget_ie6.css", "lt IE 7");
00114 
00115   messageResourceBundle().use(appRoot() + "simplechat");
00116 
00117   const std::string *div = env.getParameter("div");
00118   std::string defaultDiv = "div";
00119   if (!div)
00120    div = &defaultDiv;
00121 
00122   if (div) {
00123     setJavaScriptClass(*div);
00124     PopupChatWidget *chatWidget = new PopupChatWidget(server, *div);
00125     bindWidget(chatWidget, *div);
00126 
00127     login_.connect(chatWidget, &PopupChatWidget::setName);
00128 
00129     std::string chat = javaScriptClass();
00130     doJavaScript("if (window." + chat + "User) "
00131                  + chat + ".emit(" + chat + ", 'login', " + chat + "User);"
00132                  + "document.body.appendChild(" + chatWidget->jsRef() + ");");
00133   } else {
00134     std::cerr << "Missing: parameter: 'div'" << std::endl;
00135     quit();
00136   }
00137 }
00138 
00139 WApplication *createApplication(const WEnvironment& env,
00140                                 SimpleChatServer& server)
00141 {
00142   return new ChatApplication(env, server);
00143 }
00144 
00145 WApplication *createWidget(const WEnvironment& env, SimpleChatServer& server)
00146 {
00147   return new ChatWidget(env, server);
00148 }
00149 
00150 int main(int argc, char **argv)
00151 {
00152   Wt::WServer server(argc, argv, WTHTTP_CONFIGURATION);
00153   SimpleChatServer chatServer(server);
00154 
00155   /*
00156    * We add two entry points: one for the full-window application,
00157    * and one for a widget that can be integrated in another page.
00158    */
00159   server.addEntryPoint(Wt::Application,
00160                        boost::bind(createApplication, _1,
00161                                    boost::ref(chatServer)));
00162   server.addEntryPoint(Wt::WidgetSet,
00163                        boost::bind(createWidget, _1,
00164                                    boost::ref(chatServer)), "/chat.js");
00165 
00166   if (server.start()) {
00167     int sig = Wt::WServer::waitForShutdown();
00168     std::cerr << "Shutting down: (signal = " << sig << ")" << std::endl;
00169     server.stop();
00170   }
00171 }
00172 

Generated on Tue Mar 22 2016 for the C++ Web Toolkit (Wt) by doxygen 1.7.6.1