DataChannelServer
A C++ library for creating WebRTC DataChannel servers.
Classes | Public Member Functions | List of all members
data_channel::Server Class Reference

A DataChannel server. More...

Public Member Functions

 Server (int port, DataChannelSettings settings=DataChannelSettings())
 Create the server listening on a certain port. More...
 
void SetConnectionHandler (std::function< void(std::shared_ptr< DataChannel >)> handler)
 Get notified whenever there is a connection. More...
 
void Start ()
 Run the server. More...
 
void Stop ()
 Stop the server. More...
 

Detailed Description

A DataChannel server.

Example usage:

std::map<int, std::shared_ptr<data_channel::DataChannel>> channels;
int next_id = 0;
data_channel::Server server(9014);
server.SetConnectionHandler([&channels](std::shared_ptr<data_channel::DataChannel> channel) {
int id = next_id++;
channels[id] = channel;
channel->SendMessage("foo");
channel->SetOnMessageHandler([&clients](const std::string& message_str) {
std::cout << "Got message " << message_str << std::endl;
});
channel->SetOnCloseHandler([&clients,id](){
clients.erase(id);
});
});
server.Start();

Constructor & Destructor Documentation

data_channel::Server::Server ( int  port,
DataChannelSettings  settings = DataChannelSettings() 
)
inline

Create the server listening on a certain port.

You can also specify the settings for the created DataChannels. By default, it will create reliable, ordered channels.

Parameters
portThe port the server will be listening on.
settingsThe settings for the created DataChannels.

Member Function Documentation

void data_channel::Server::SetConnectionHandler ( std::function< void(std::shared_ptr< DataChannel >)>  handler)
inline

Get notified whenever there is a connection.

The user ownes the resulting DataChannel and it gets closed when the provided shared_ptr becomes freed.

Parameters
handlerThe callback to be used whenever there is a new connection.
void data_channel::Server::Start ( )
inline

Run the server.

This will block and run the internal io_service until Stop is called.

void data_channel::Server::Stop ( )
inline

Stop the server.

Stops listening on the socket.


The documentation for this class was generated from the following file: