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