Skip to content
This repository was archived by the owner on Jun 12, 2018. It is now read-only.

[suggest] Send/Broadcast and Text/BinaryMode  #100

@nicezic

Description

@nicezic

Hello, @eidheim

First, thanks to make this Simple WebSocket.

I needed to send binary mode.
I found the article, #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 );

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions