Wt examples  3.7.1
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 <boost/noncopyable.hpp>
11 
12 #include <Wt/WSignal>
13 #include <Wt/WString>
14 
15 namespace Wt {
16  class WServer;
17 }
18 
19 #include <set>
20 #include <map>
21 #include <boost/thread.hpp>
22 
27 
30 class ChatEvent
31 {
32 public:
35  enum Type { Login, Logout, Rename, Message };
36 
39  Type type() const { return type_; }
40 
43  const Wt::WString& user() const { return user_; }
44 
47  const Wt::WString& message() const { return message_; }
48 
51  const Wt::WString& data() const { return data_; }
52 
57  const Wt::WString formattedHTML(const Wt::WString& user,
58  Wt::TextFormat format) const;
59 
60 private:
65 
66  /*
67  * Both user and html will be formatted as html
68  */
69  ChatEvent(const Wt::WString& user, const Wt::WString& message)
70  : type_(Message), user_(user), message_(message)
71  { }
72 
73  ChatEvent(Type type, const Wt::WString& user,
74  const Wt::WString& data = Wt::WString::Empty)
75  : type_(type), user_(user), data_(data)
76  { }
77 
78  friend class SimpleChatServer;
79 };
80 
81 typedef boost::function<void (const ChatEvent&)> ChatEventCallback;
82 
85 class SimpleChatServer : boost::noncopyable
86 {
87 public:
88  /*
89  * A reference to a client.
90  */
91  class Client
92  {
93  };
94 
98 
107  bool connect(Client *client, const ChatEventCallback& handleEvent);
108 
114  bool disconnect(Client *client);
115 
120  bool login(const Wt::WString& user);
121 
124  void logout(const Wt::WString& user);
125 
128  bool changeName(const Wt::WString& user, const Wt::WString& newUser);
129 
132  Wt::WString suggestGuest();
133 
136  void sendMessage(const Wt::WString& user, const Wt::WString& message);
137 
140  typedef std::set<Wt::WString> UserSet;
141 
144  UserSet users();
145 
146 private:
147  struct ClientInfo {
148  std::string sessionId;
150  };
151 
152  typedef std::map<Client *, ClientInfo> ClientMap;
153 
155  boost::recursive_mutex mutex_;
156  ClientMap clients_;
157  UserSet users_;
158 
159  void postChatEvent(const ChatEvent& event);
160 };
161 
164 #endif // SIMPLECHATSERVER_H_
Type type() const
Get the event type.
boost::recursive_mutex mutex_
std::map< Client *, ClientInfo > ClientMap
const Wt::WString & data() const
Get the extra data for this event.
Encapsulate a chat event.
std::set< Wt::WString > UserSet
Typedef for a collection of user names.
Type
Enumeration for the event type.
boost::function< void(const ChatEvent &)> ChatEventCallback
ChatEvent(const Wt::WString &user, const Wt::WString &message)
Wt::WServer & server_
TextFormat
const Wt::WString & message() const
Get the message of the event.
static const WString Empty
Wt::WString message_
Wt::WString user_
Wt::WString data_
A simple chat server.
ChatEvent(Type type, const Wt::WString &user, const Wt::WString &data=Wt::WString::Empty)
const Wt::WString & user() const
Get the user who caused the event.

Generated on Tue Dec 15 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.13