Wt examples  4.10.4
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 
13 namespace Wt {
14  class WServer;
15 }
16 
17 #include <set>
18 #include <map>
19 #include <thread>
20 #include <mutex>
21 
26 
29 class ChatEvent
30 {
31 public:
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 
57  Wt::TextFormat format) const;
58 
59 private:
64 
65  /*
66  * Both user and html will be formatted as html
67  */
70  { }
71 
74  : type_(type), user_(user), data_(data)
75  { }
76 
77  friend class SimpleChatServer;
78 };
79 
80 typedef std::function<void (const ChatEvent&)> ChatEventCallback;
81 
85 {
86 public:
87  /*
88  * A reference to a client.
89  */
90  class Client
91  {
92  };
93 
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 
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 
148 private:
149  struct ClientInfo {
150  std::string sessionId;
152  };
153 
154  typedef std::map<Client *, ClientInfo> ClientMap;
155 
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.
const Wt::WString & user() const
Get the user who caused the event.
Wt::WString user_
const Wt::WString & data() const
Get the extra data for this event.
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_
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
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.
SimpleChatServer & operator=(const SimpleChatServer &)=delete
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.
static const WString Empty
TextFormat