8 #include <Wt/WServer.h> 19 + Wt::WWebWidget::escapeText(
user_) +
" joined.</span>";
24 Wt::WWebWidget::escapeText(
user_))
25 +
" logged out.</span>";
27 return "<span class='chat-info'>" 30 (Wt::WWebWidget::escapeText(
user_) +
" is"))
31 +
" now known as " + Wt::WWebWidget::escapeText(
data_) +
".</span>";
39 +
"'>" + Wt::WWebWidget::escapeText(
user_) +
":</span>";
45 return result +
"<span class='chat-highlight'>" + msg +
"</span>";
62 std::unique_lock<std::recursive_mutex> lock(
mutex_);
79 std::unique_lock<std::recursive_mutex> lock(
mutex_);
86 std::unique_lock<std::recursive_mutex> lock(
mutex_);
100 std::unique_lock<std::recursive_mutex> lock(
mutex_);
102 UserSet::iterator i =
users_.find(user);
116 std::unique_lock<std::recursive_mutex> lock(
mutex_);
118 UserSet::iterator i =
users_.find(user);
121 if (
users_.count(newUser) == 0) {
136 std::unique_lock<std::recursive_mutex> lock(
mutex_);
138 for (
int i = 1;; ++i) {
139 std::string s =
"guest " + std::to_string(i);
154 std::unique_lock<std::recursive_mutex> lock(
mutex_);
170 if (app && app->
sessionId() == i->second.sessionId)
171 i->second.eventCallback(event);
174 std::bind(i->second.eventCallback, event));
180 std::unique_lock<std::recursive_mutex> lock(
mutex_);
std::function< void(const ChatEvent &)> ChatEventCallback
SimpleChatServer(Wt::WServer &server)
Create a new chat server.
void sendMessage(const Wt::WString &user, const Wt::WString &message)
Send a message on behalve of a user.
ChatEventCallback eventCallback
WT_API void post(const std::string &sessionId, const std::function< void()> &function, const std::function< void()> &fallBackFunction=std::function< void()>())
bool connect(Client *client, const ChatEventCallback &handleEvent)
Connects to the chat server.
Encapsulate a chat event.
UserSet users()
Get the users currently logged in.
std::set< Wt::WString > UserSet
Typedef for a collection of user names.
bool changeName(const Wt::WString &user, const Wt::WString &newUser)
Changes the name.
std::string sessionId() const
std::string toUTF8() const
void postChatEvent(const ChatEvent &event)
bool login(const Wt::WString &user)
Try to login with given user name.
bool disconnect(Client *client)
Disconnect from the chat server.
Wt::WString suggestGuest()
Get a suggestion for a guest user name.
std::recursive_mutex mutex_
void logout(const Wt::WString &user)
Logout from the server.
static WApplication * instance()
const Wt::WString formattedHTML(const Wt::WString &user, Wt::TextFormat format) const
Get the message formatted as HTML, rendered for the given user.