8 #include <DataChannelServer/client-src/internal-api.h> 9 #include <emscripten.h> 21 inline void Connect(
const std::string &server,
int port, std::function<
void(std::shared_ptr<DataChannel>)> handler,
22 std::function<
void(
const std::string &error)> error_handler) {
23 typedef std::function<void(std::shared_ptr<DataChannel>)> ConnectHandler;
24 typedef std::function<void(const std::string &error)> ErrorHandler;
28 [](PeerConnection *peer,
void *data) {
29 ConnectHandler *handler =
reinterpret_cast<ConnectHandler *
>(data);
30 auto close_handler = [peer]() {
31 std::cout <<
"LOL SHOULD HAVE QUIT" << std::endl;
32 DeletePeerConnection(peer);
35 auto message_handler = [peer](
const std::string &message) {
36 SendPeerConnectionMessage(peer, message.data(), message.size());
39 std::shared_ptr<DataChannel> channel =
40 std::make_shared<DataChannel>(message_handler, close_handler);
43 peer, [](
const char *message,
int message_length,
void *data) {
45 std::string message_str(message, message_length);
46 channel->GetOnMessageHandler()(message_str);
49 SetOnCloseCallback(peer, [](
void *data) {
51 channel->GetOnCloseHandler()();
57 new ConnectHandler(handler),
58 [](
const char *error,
int error_length,
void *data) {
59 ErrorHandler *handler =
reinterpret_cast<ErrorHandler *
>(data);
60 std::string error_str(error, error_length);
61 (*handler)(error_str);
63 new ErrorHandler(error_handler));
64 emscripten_exit_with_live_runtime();
void Connect(const std::string &server, int port, std::function< void(std::shared_ptr< DataChannel >)> handler, std::function< void(const std::string &error)> error_handler)
Connect to a DataChannel Server.
Definition: client.h:21
A class which represents a single peer connection.
Definition: channel.h:15
The connection API for both the server and client.