Introduced onOpen() callback in client example

This commit is contained in:
Paul-Louis Ageneau
2021-04-14 21:54:42 +02:00
parent 5346d31cc1
commit bbbc8ca4e6

View File

@ -222,9 +222,14 @@ shared_ptr<PeerConnection> createPeerConnection(const Configuration &config,
cout << "DataChannel from " << id << " received with label \"" << dc->label() << "\"" cout << "DataChannel from " << id << " received with label \"" << dc->label() << "\""
<< endl; << endl;
dc->onOpen([wdc = make_weak_ptr(dc)]() {
if (auto dc = wdc.lock())
dc->send("Hello from " + localId);
});
dc->onClosed([id]() { cout << "DataChannel from " << id << " closed" << endl; }); dc->onClosed([id]() { cout << "DataChannel from " << id << " closed" << endl; });
dc->onMessage([id, wdc = make_weak_ptr(dc)](variant<binary, string> data) { dc->onMessage([id](variant<binary, string> data) {
if (holds_alternative<string>(data)) if (holds_alternative<string>(data))
cout << "Message from " << id << " received: " << get<string>(data) << endl; cout << "Message from " << id << " received: " << get<string>(data) << endl;
else else
@ -232,8 +237,6 @@ shared_ptr<PeerConnection> createPeerConnection(const Configuration &config,
<< " received, size=" << get<binary>(data).size() << endl; << " received, size=" << get<binary>(data).size() << endl;
}); });
dc->send("Hello from " + localId);
dataChannelMap.emplace(id, dc); dataChannelMap.emplace(id, dc);
}); });
@ -251,4 +254,3 @@ string randomId(size_t length) {
generate(id.begin(), id.end(), [&]() { return characters.at(dist(rng)); }); generate(id.begin(), id.end(), [&]() { return characters.at(dist(rng)); });
return id; return id;
} }