Wt examples 4.5.0
SimpleChatServer.h
Go to the documentation of this file.
1// This may look like C code, but it's really -*- C++ -*-
2/*
3 * Copyright (C) 2008 Emweb bv, Herent, Belgium.
4 *
5 * See the LICENSE file for terms of use.
6 */
7#ifndef SIMPLECHATSERVER_H_
8#define SIMPLECHATSERVER_H_
9
10#include <Wt/WSignal.h>
11#include <Wt/WString.h>
12
13namespace Wt {
14 class WServer;
15}
16
17#include <set>
18#include <map>
19#include <thread>
20#include <mutex>
21
26
30{
31public:
35
38 Type type() const { return type_; }
39
42 const Wt::WString& user() const { return user_; }
43
46 const Wt::WString& message() const { return message_; }
47
50 const Wt::WString& data() const { return data_; }
51
56 const Wt::WString formattedHTML(const Wt::WString& user,
57 Wt::TextFormat format) const;
58
59private:
61 Wt::WString user_;
62 Wt::WString data_;
63 Wt::WString message_;
64
65 /*
66 * Both user and html will be formatted as html
67 */
68 ChatEvent(const Wt::WString& user, const Wt::WString& message)
70 { }
71
72 ChatEvent(Type type, const Wt::WString& user,
73 const Wt::WString& data = Wt::WString::Empty)
75 { }
76
77 friend class SimpleChatServer;
78};
79
80typedef std::function<void (const ChatEvent&)> ChatEventCallback;
81
85{
86public:
87 /*
88 * A reference to a client.
89 */
90 class Client
91 {
92 };
93
96 SimpleChatServer(Wt::WServer& server);
97
100
109 bool connect(Client *client, const ChatEventCallback& handleEvent);
110
116 bool disconnect(Client *client);
117
122 bool login(const Wt::WString& user);
123
126 void logout(const Wt::WString& user);
127
130 bool changeName(const Wt::WString& user, const Wt::WString& newUser);
131
134 Wt::WString suggestGuest();
135
138 void sendMessage(const Wt::WString& user, const Wt::WString& message);
139
142 typedef std::set<Wt::WString> UserSet;
143
146 UserSet users();
147
148private:
149 struct ClientInfo {
150 std::string sessionId;
152 };
153
154 typedef std::map<Client *, ClientInfo> ClientMap;
155
156 Wt::WServer& server_;
157 std::recursive_mutex mutex_;
160
161 void postChatEvent(const ChatEvent& event);
162};
163
166#endif // SIMPLECHATSERVER_H_
std::function< void(const ChatEvent &)> ChatEventCallback
Encapsulate a chat event.
Wt::WString user_
const Wt::WString & message() const
Get the message of the event.
ChatEvent(Type type, const Wt::WString &user, const Wt::WString &data=Wt::WString::Empty)
ChatEvent(const Wt::WString &user, const Wt::WString &message)
const Wt::WString formattedHTML(const Wt::WString &user, Wt::TextFormat format) const
Get the message formatted as HTML, rendered for the given user.
Type type() const
Get the event type.
Wt::WString message_
const Wt::WString & user() const
Get the user who caused the event.
const Wt::WString & data() const
Get the extra data for this event.
Wt::WString data_
Type
Enumeration for the event type.
A simple chat server.
bool changeName(const Wt::WString &user, const Wt::WString &newUser)
Changes the name.
bool disconnect(Client *client)
Disconnect from the chat server.
SimpleChatServer(const SimpleChatServer &)=delete
SimpleChatServer & operator=(const SimpleChatServer &)=delete
std::map< Client *, ClientInfo > ClientMap
SimpleChatServer(Wt::WServer &server)
Create a new chat server.
UserSet users()
Get the users currently logged in.
void postChatEvent(const ChatEvent &event)
void logout(const Wt::WString &user)
Logout from the server.
std::set< Wt::WString > UserSet
Typedef for a collection of user names.
bool connect(Client *client, const ChatEventCallback &handleEvent)
Connects to the chat server.
void sendMessage(const Wt::WString &user, const Wt::WString &message)
Send a message on behalve of a user.
std::recursive_mutex mutex_
Wt::WServer & server_
Wt::WString suggestGuest()
Get a suggestion for a guest user name.
bool login(const Wt::WString &user)
Try to login with given user name.
Definition: ChartConfig.h:13

Generated on Fri Sep 10 2021 for the C++ Web Toolkit (Wt) by doxygen 1.9.2