9 #ifndef CPPSERVER_ASIO_WEBSOCKET_SESSION_H 10 #define CPPSERVER_ASIO_WEBSOCKET_SESSION_H 15 #include "system/uuid.h" 20 template <
class TServer,
class TSession>
21 class WebSocketServer;
29 template <
class TServer,
class TSession>
30 class WebSocketSession :
public std::enable_shared_from_this<WebSocketSession<TServer, TSession>>
32 template <
class TSomeServer,
class TSomeSession>
49 const CppCommon::UUID&
id() const noexcept {
return _id; }
52 std::shared_ptr<Service>&
service() noexcept {
return _server->service(); }
54 std::shared_ptr<WebSocketServer<TServer, TSession>>&
server() noexcept {
return _server; }
56 websocketpp::connection_hdl&
connection() noexcept {
return _connection; }
63 uint64_t
bytes_sent() const noexcept {
return _bytes_sent; }
76 bool Disconnect(websocketpp::close::status::value code = websocketpp::close::status::normal,
const std::string& reason =
"") {
return Disconnect(
false, code, reason); }
85 size_t Send(
const void* buffer,
size_t size, websocketpp::frame::opcode::value opcode = websocketpp::frame::opcode::binary);
92 size_t Send(
const std::string& text, websocketpp::frame::opcode::value opcode = websocketpp::frame::opcode::text);
118 virtual void onError(
int error,
const std::string& category,
const std::string& message) {}
124 std::shared_ptr<WebSocketServer<TServer, TSession>> _server;
125 websocketpp::connection_hdl _connection;
126 std::atomic<bool> _connected;
128 uint64_t _messages_sent;
129 uint64_t _messages_received;
130 uint64_t _bytes_sent;
131 uint64_t _bytes_received;
137 void Connect(websocketpp::connection_hdl
connection);
145 bool Disconnect(
bool dispatch, websocketpp::close::status::value code = websocketpp::close::status::normal,
const std::string& reason =
"");
151 void SendError(std::error_code ec);
157 #endif // CPPSERVER_ASIO_WEBSOCKET_SESSION_H WebSocketSession(std::shared_ptr< WebSocketServer< TServer, TSession >> server)
Initialize the session with a given server.
virtual void onConnected()
Handle session connected notification.
C++ Server project definitions.
uint64_t bytes_received() const noexcept
Get the number of bytes received by this session.
std::shared_ptr< WebSocketServer< TServer, TSession > > & server() noexcept
Get the session server.
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
size_t Send(const void *buffer, size_t size, websocketpp::frame::opcode::value opcode=websocketpp::frame::opcode::binary)
Send data into the session.
WebSocketSession & operator=(const WebSocketSession &)=delete
bool Disconnect(websocketpp::close::status::value code=websocketpp::close::status::normal, const std::string &reason="")
Disconnect the session.
uint64_t messages_received() const noexcept
Get the number messages received by this session.
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
WebSocketConnection::message_ptr WebSocketMessage
WebSocket message.
uint64_t bytes_sent() const noexcept
Get the number of bytes sent by this session.
uint64_t messages_sent() const noexcept
Get the number messages sent by this session.
virtual ~WebSocketSession()=default
virtual void onReceived(const WebSocketMessage &message)
Handle message received notification.
websocketpp::connection_hdl & connection() noexcept
Get the session connection.
const CppCommon::UUID & id() const noexcept
Get the session Id.
bool IsConnected() const noexcept
Is the session connected?
virtual void onDisconnected()
Handle session disconnected notification.
WebSocket C++ Library definition.