Wt examples
3.3.0
Main Page
Modules
Namespaces
Classes
Files
File List
File Members
build
buildd
witty-3.3.0
examples
simplechat
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 bvba, Heverlee, 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
:
61
Type
type_
;
62
Wt::WString
user_
;
63
Wt::WString
data_
;
64
Wt::WString
message_
;
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
97
SimpleChatServer
(
Wt::WServer
& server);
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
;
149
ChatEventCallback
eventCallback
;
150
};
151
152
typedef
std::map<Client *, ClientInfo>
ClientMap
;
153
154
Wt::WServer
&
server_
;
155
boost::recursive_mutex
mutex_
;
156
ClientMap
clients_
;
157
UserSet
users_
;
158
159
void
postChatEvent
(
const
ChatEvent
& event);
160
};
161
164
#endif // SIMPLECHATSERVER_H_
Generated on Fri May 31 2013 for
the C++ Web Toolkit (Wt)
by
1.8.3.1