37 : m_localEndpoint(localEndpoint)
38 , m_pingInterval(10_s)
40 setUri(FaceUri(m_localEndpoint,
"ws"));
44 m_server.clear_access_channels(websocketpp::log::alevel::all);
45 m_server.clear_error_channels(websocketpp::log::elevel::all);
49 m_server.set_tcp_pre_bind_handler([isV6 = m_localEndpoint.address().is_v6()] (
const auto& acceptor) {
51 acceptor->set_option(boost::asio::ip::v6_only(true));
53 return websocketpp::lib::error_code{};
55 m_server.set_open_handler(std::bind(&WebSocketChannel::handleOpen,
this, _1));
56 m_server.set_close_handler(std::bind(&WebSocketChannel::handleClose,
this, _1));
57 m_server.set_message_handler(std::bind(&WebSocketChannel::handleMessage,
this, _1, _2));
60 m_server.set_pong_handler(std::bind(&WebSocketChannel::handlePong,
this, _1));
61 m_server.set_pong_timeout_handler(std::bind(&WebSocketChannel::handlePongTimeout,
this, _1));
64 m_server.set_reuse_addr(
true);
68 WebSocketChannel::setPingInterval(time::milliseconds interval)
70 BOOST_ASSERT(!m_server.is_listening());
72 m_pingInterval = interval;
76 WebSocketChannel::setPongTimeout(time::milliseconds timeout)
78 BOOST_ASSERT(!m_server.is_listening());
80 m_server.set_pong_timeout(
static_cast<long>(timeout.count()));
84 WebSocketChannel::handlePongTimeout(websocketpp::connection_hdl hdl)
86 auto it = m_channelFaces.find(hdl);
87 if (it != m_channelFaces.end()) {
88 static_cast<WebSocketTransport*
>(it->second->getTransport())->handlePongTimeout();
96 WebSocketChannel::handlePong(websocketpp::connection_hdl hdl)
98 auto it = m_channelFaces.find(hdl);
99 if (it != m_channelFaces.end()) {
100 static_cast<WebSocketTransport*
>(it->second->getTransport())->handlePong();
108 WebSocketChannel::handleMessage(websocketpp::connection_hdl hdl,
109 websocket::Server::message_ptr msg)
111 auto it = m_channelFaces.find(hdl);
112 if (it != m_channelFaces.end()) {
113 static_cast<WebSocketTransport*
>(it->second->getTransport())->receiveMessage(msg->get_payload());
121 WebSocketChannel::handleOpen(websocketpp::connection_hdl hdl)
123 NFD_LOG_CHAN_TRACE(
"Incoming connection from " << m_server.get_con_from_hdl(hdl)->get_remote_endpoint());
125 auto linkService = make_unique<GenericLinkService>();
126 auto transport = make_unique<WebSocketTransport>(hdl, m_server, m_pingInterval);
127 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
128 face->setChannel(weak_from_this());
130 BOOST_ASSERT(m_channelFaces.count(hdl) == 0);
131 m_channelFaces[hdl] = face;
134 m_onFaceCreatedCallback(face);
138 WebSocketChannel::handleClose(websocketpp::connection_hdl hdl)
140 auto it = m_channelFaces.find(hdl);
141 if (it != m_channelFaces.end()) {
157 m_onFaceCreatedCallback = onFaceCreated;
159 m_server.listen(m_localEndpoint);
160 m_server.start_accept();
void setUri(const FaceUri &uri) noexcept
WebSocketChannel(const websocket::Endpoint &localEndpoint)
Create a WebSocket channel for the given localEndpoint.
#define NFD_LOG_CHAN_DEBUG(msg)
Log a message at DEBUG level.
#define NFD_LOG_CHAN_INFO(msg)
Log a message at INFO level.
#define NFD_LOG_CHAN_WARN(msg)
Log a message at WARN level.
#define NFD_LOG_CHAN_TRACE(msg)
Log a message at TRACE level.
#define NFD_LOG_INIT(name)
std::function< void(const shared_ptr< Face > &)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
void connectFaceClosedSignal(Face &face, std::function< void()> f)
Invokes a callback when a face is closed.
boost::asio::ip::tcp::endpoint Endpoint
boost::asio::io_context & getGlobalIoService()
Returns the global io_context instance for the calling thread.