A simple chat server. More...
#include <SimpleChatServer.h>
Inherits Wt::WObject.
Public Types | |
| typedef std::set< Wt::WString > | UserSet |
| Typedef for a collection of user names. | |
Public Member Functions | |
| SimpleChatServer () | |
| Create a new chat server. | |
| bool | login (const Wt::WString &user) |
| Try to login with given user name. | |
| void | logout (const Wt::WString &user) |
| Logout from the server. | |
| Wt::WString | suggestGuest () |
| Get a suggestion for a guest user name. | |
| void | sendMessage (const Wt::WString &user, const Wt::WString &message) |
| Send a message on behalve of a user. | |
| Wt::Signal< ChatEvent > & | chatEvent () |
| Signal that will convey chat events. | |
| UserSet | users () |
| Get the users currently logged in. | |
Private Attributes | |
| Wt::Signal< ChatEvent > | chatEvent_ |
| boost::mutex | mutex_ |
| UserSet | users_ |
A simple chat server.
Definition at line 68 of file SimpleChatServer.h.
| typedef std::set<Wt::WString> SimpleChatServer::UserSet |
Typedef for a collection of user names.
Definition at line 101 of file SimpleChatServer.h.
| SimpleChatServer::SimpleChatServer | ( | ) |
| Wt::Signal<ChatEvent>& SimpleChatServer::chatEvent | ( | ) | [inline] |
Signal that will convey chat events.
Every client should connect to this signal, and process events.
Definition at line 97 of file SimpleChatServer.h.
{ return chatEvent_; }
| bool SimpleChatServer::login | ( | const Wt::WString & | user | ) |
Try to login with given user name.
Returns false if the login was not successfull.
Definition at line 46 of file SimpleChatServer.C.
{
boost::mutex::scoped_lock lock(mutex_);
if (users_.find(user) == users_.end()) {
users_.insert(user);
chatEvent_.emit(ChatEvent(ChatEvent::Login, user));
return true;
} else
return false;
}
| void SimpleChatServer::logout | ( | const Wt::WString & | user | ) |
Logout from the server.
Definition at line 60 of file SimpleChatServer.C.
{
boost::mutex::scoped_lock lock(mutex_);
UserSet::iterator i = users_.find(user);
if (i != users_.end()) {
users_.erase(i);
chatEvent_.emit(ChatEvent(ChatEvent::Logout, user));
}
}
| void SimpleChatServer::sendMessage | ( | const Wt::WString & | user, | |
| const Wt::WString & | message | |||
| ) |
Send a message on behalve of a user.
Definition at line 86 of file SimpleChatServer.C.
{
boost::mutex::scoped_lock lock(mutex_);
chatEvent_.emit(ChatEvent(user, message));
}
| WString SimpleChatServer::suggestGuest | ( | ) |
| SimpleChatServer::UserSet SimpleChatServer::users | ( | ) |
Get the users currently logged in.
Definition at line 93 of file SimpleChatServer.C.
{
return users_;
}
Wt::Signal<ChatEvent> SimpleChatServer::chatEvent_ [private] |
Definition at line 108 of file SimpleChatServer.h.
boost::mutex SimpleChatServer::mutex_ [private] |
Definition at line 109 of file SimpleChatServer.h.
UserSet SimpleChatServer::users_ [private] |
Definition at line 111 of file SimpleChatServer.h.
1.7.1