64 lines
2.3 KiB
C++
64 lines
2.3 KiB
C++
/*
|
|
* (c) Copyright, Real-Time Innovations, 2024.
|
|
* All rights reserved.
|
|
*
|
|
* No duplications, whole or partial, manual or electronic, may be made
|
|
* without express written permission. Any such copies, or
|
|
* revisions thereof, must display this notice unaltered.
|
|
* This code contains trade secrets of Real-Time Innovations, Inc.
|
|
*/
|
|
#ifndef WEBSOCKET_HANDLER_HPP
|
|
#define WEBSOCKET_HANDLER_HPP
|
|
|
|
#include <rti/apputils/http/server/WebSocketConnection.hpp>
|
|
#include <rti/apputils/StringBuffer.hpp>
|
|
|
|
namespace rti { namespace apputils { namespace websocket {
|
|
|
|
/**
|
|
* This class is a generic handler for WebSocket communication. It
|
|
* implements methods that handle all the different events that can be
|
|
* triggered in the context of a WebSocket connection.
|
|
*/
|
|
class WebSocketHandler {
|
|
public:
|
|
/**
|
|
* This method is called when a client establishes a connection with the
|
|
* server. When that happens, we set the status of the WebSocketConnection
|
|
* to WebSocketStatus_CREATED.
|
|
* @param conn WebSocketConnection connection that will be created
|
|
*/
|
|
virtual void handle_connection(WebSocketConnection& conn) = 0;
|
|
|
|
/**
|
|
* This method is called when the websocket's handshake protocol has
|
|
* ended successfully. The state of the connection is marked as
|
|
* WebSocketStateKind::OPEN.
|
|
* @param conn WebSocketConnection connection that will be updated
|
|
*/
|
|
virtual void handle_ready_state(WebSocketConnection& conn) = 0;
|
|
|
|
/**
|
|
* This method is called every time a client sends a message. The message is
|
|
* wrapped in a StringBuffer. Upon reception, the method identifies the
|
|
* message kind and calls the appropriate Handler for that kind of message.
|
|
* @param conn WebSocketConnection connection that sent the message
|
|
* @param msg StringBuffer with the corresponding message
|
|
*/
|
|
virtual void handle_data(
|
|
WebSocketConnection& conn,
|
|
const StringBuffer& msg) = 0;
|
|
|
|
/**
|
|
* This method is called when a client closes a connection with the server.
|
|
* It detaches all conditions that have been previously attached to the
|
|
* connection.
|
|
* @param conn WebSocketConnection connection that will be closed
|
|
*/
|
|
virtual void handle_close(WebSocketConnection& conn) = 0;
|
|
};
|
|
|
|
}}} // namespace rti::apputils::websocket
|
|
|
|
#endif // WEBSOCKET_HANDLER_HPP
|