simpleChat.C 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (C) 2008 Emweb bvba, Heverlee, Belgium.
  3. *
  4. * See the LICENSE file for terms of use.
  5. */
  6. #include <Wt/WApplication>
  7. #include <Wt/WContainerWidget>
  8. #include <Wt/WEnvironment>
  9. #include <Wt/WPushButton>
  10. #include <Wt/WServer>
  11. #include <Wt/WText>
  12. #include <Wt/WTimer>
  13. #include "SimpleChatServer.h"
  14. #include "PopupChatWidget.h"
  15. using namespace Wt;
  16. /**
  17. * @addtogroup chatexample
  18. */
  19. /*@{*/
  20. /*! \brief A chat demo application.
  21. */
  22. class ChatApplication : public WApplication
  23. {
  24. public:
  25. /*! \brief Create a new instance.
  26. */
  27. ChatApplication(const WEnvironment& env, SimpleChatServer& server);
  28. private:
  29. SimpleChatServer& server_;
  30. Wt::WText *javaScriptError_;
  31. const WEnvironment& env_;
  32. Wt::WTimer *timer_;
  33. /*! \brief Add another chat client.
  34. */
  35. void addChatWidget();
  36. void javaScriptTest();
  37. void emptyFunc();
  38. };
  39. ChatApplication::ChatApplication(const WEnvironment& env,
  40. SimpleChatServer& server)
  41. : WApplication(env),
  42. server_(server),
  43. env_(env)
  44. {
  45. setTitle("Wt Chat");
  46. useStyleSheet("chatapp.css");
  47. messageResourceBundle().use(appRoot() + "simplechat");
  48. javaScriptTest();
  49. root()->addWidget(new WText(WString::tr("introduction")));
  50. SimpleChatWidget *chatWidget =
  51. new SimpleChatWidget(server_, root());
  52. chatWidget->setStyleClass("chat");
  53. root()->addWidget(new WText(WString::tr("details")));
  54. WPushButton *b = new WPushButton("I'm schizophrenic ...", root());
  55. b->clicked().connect(b, &WPushButton::hide);
  56. b->clicked().connect(this, &ChatApplication::addChatWidget);
  57. }
  58. void ChatApplication::javaScriptTest()
  59. {
  60. if(!env_.javaScript()){
  61. javaScriptError_ = new WText(WString::tr("serverpushwarning"), root());
  62. // The 5 second timer is a fallback for real server push. The updated
  63. // server state will piggy back on the response to this timeout.
  64. timer_ = new Wt::WTimer(root());
  65. timer_->setInterval(5000);
  66. timer_->timeout().connect(this, &ChatApplication::emptyFunc);
  67. timer_->start();
  68. }
  69. }
  70. void ChatApplication::emptyFunc()
  71. {}
  72. void ChatApplication::addChatWidget()
  73. {
  74. SimpleChatWidget *chatWidget2 = new SimpleChatWidget(server_, root());
  75. chatWidget2->setStyleClass("chat");
  76. }
  77. /*! \brief A chat application widget.
  78. */
  79. class ChatWidget : public WApplication
  80. {
  81. public:
  82. ChatWidget(const WEnvironment& env, SimpleChatServer& server);
  83. private:
  84. JSignal<WString> login_;
  85. };
  86. ChatWidget::ChatWidget(const WEnvironment& env, SimpleChatServer& server)
  87. : WApplication(env),
  88. login_(this, "login")
  89. {
  90. setCssTheme("");
  91. useStyleSheet("chatwidget.css");
  92. useStyleSheet("chatwidget_ie6.css", "lt IE 7");
  93. messageResourceBundle().use(appRoot() + "simplechat");
  94. const std::string *div = env.getParameter("div");
  95. std::string defaultDiv = "div";
  96. if (!div)
  97. div = &defaultDiv;
  98. if (div) {
  99. setJavaScriptClass(*div);
  100. PopupChatWidget *chatWidget = new PopupChatWidget(server, *div);
  101. bindWidget(chatWidget, *div);
  102. login_.connect(chatWidget, &PopupChatWidget::setName);
  103. std::string chat = javaScriptClass();
  104. doJavaScript("if (window." + chat + "User) "
  105. + chat + ".emit(" + chat + ", 'login', " + chat + "User);"
  106. + "document.body.appendChild(" + chatWidget->jsRef() + ");");
  107. } else {
  108. std::cerr << "Missing: parameter: 'div'" << std::endl;
  109. quit();
  110. }
  111. }
  112. WApplication *createApplication(const WEnvironment& env,
  113. SimpleChatServer& server)
  114. {
  115. return new ChatApplication(env, server);
  116. }
  117. WApplication *createWidget(const WEnvironment& env, SimpleChatServer& server)
  118. {
  119. return new ChatWidget(env, server);
  120. }
  121. int main(int argc, char **argv)
  122. {
  123. Wt::WServer server(argc, argv, WTHTTP_CONFIGURATION);
  124. SimpleChatServer chatServer(server);
  125. /*
  126. * We add two entry points: one for the full-window application,
  127. * and one for a widget that can be integrated in another page.
  128. */
  129. server.addEntryPoint(Wt::Application,
  130. boost::bind(createApplication, _1,
  131. boost::ref(chatServer)));
  132. server.addEntryPoint(Wt::WidgetSet,
  133. boost::bind(createWidget, _1,
  134. boost::ref(chatServer)), "/chat.js");
  135. if (server.start()) {
  136. int sig = Wt::WServer::waitForShutdown();
  137. std::cerr << "Shutting down: (signal = " << sig << ")" << std::endl;
  138. server.stop();
  139. }
  140. }
  141. /*@}*/