Simple-WebSocket-Server
Simple-WebSocket-Server copied to clipboard
[suggest] Send/Broadcast and Text/BinaryMode
Hello, @eidheim
First, thanks to make this Simple WebSocket.
I needed to send binary mode. I found the article, https://github.com/eidheim/Simple-WebSocket-Server/issues/30, about this, and applied to my project.
FYI, here is my wrappers. Furthermore, I wish to consider these warpping functions, and merged into SimpleWeb::SocketServer.
Sincerely, YeonHo Park from South Korea. Thanks.
using WsServer = SimpleWeb::SocketServer<SimpleWeb::WS>;
WsServer m_ws_server;
void SendText( shared_ptr<WsServer::Connection> connection,
const std::string& message,
const std::function<void( const error_code& )>& callback = nullptr );
void SendBinary( shared_ptr<WsServer::Connection> connection,
const std::shared_ptr<WsServer::SendStream>& send_stream,
const std::function<void( const error_code& )>& callback = nullptr );
void BroadcastBinary( const std::shared_ptr<WsServer::SendStream>& send_stream,
const std::function<void( const error_code& )>& callback = nullptr );
void BroadcastText( const std::string& message_to_send,
const std::function<void( const error_code& )>& callback = nullptr );
void Websocket::SendText( shared_ptr<WsServer::Connection> connection,
const std::string& message,
const std::function<void( const error_code& )>& callback )
{
auto send_stream = make_shared<WsServer::SendStream>();
*send_stream << message;
connection->send( send_stream, callback, 129 ); // fin_rsv_opcode: 129 = one fragment & text.
}
void Websocket::SendBinary( shared_ptr<WsServer::Connection> connection,
const std::shared_ptr<WsServer::SendStream>& send_stream,
const std::function<void( const error_code& )>& callback )
{
connection->send( send_stream, callback, 130 ); // fin_rsv_opcode: 130 = one fragment & binary.
}
void Websocket::BroadcastBinary( const std::shared_ptr<WsServer::SendStream>& send_stream,
const std::function<void( const error_code& )>& callback )
{
for( auto& a_connection : m_ws_server.get_connections() )
SendBinary( a_connection, send_stream, callback );
}
void Websocket::BroadcastText( const string& message_to_send,
const std::function<void( const error_code& )>& callback )
{
for( auto& a_connection : m_ws_server.get_connections() )
SendText( a_connection, message_to_send, callback );
}
// example.
auto send_stream = make_shared<WsServer::SendStream>();
*send_stream << YOUR_BINARY_DATA;
Websocket::getInstance()->BroadcastBinary( send_stream );
Thank you for these suggestions, I'll think about this in the following weeks. There are also some additional related changes I have planned before the major version bump.