Browse Source

Introduced onOpen() callback in client example

Paul-Louis Ageneau 4 years ago
parent
commit
bbbc8ca4e6
1 changed files with 6 additions and 4 deletions
  1. 6 4
      examples/client/main.cpp

+ 6 - 4
examples/client/main.cpp

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