Browse Source

Wait for websocket connection with promise

Paul-Louis Ageneau 4 years ago
parent
commit
65631f06a6
1 changed files with 18 additions and 10 deletions
  1. 18 10
      examples/client/main.cpp

+ 18 - 10
examples/client/main.cpp

@@ -30,6 +30,8 @@
 #include <memory>
 #include <memory>
 #include <random>
 #include <random>
 #include <thread>
 #include <thread>
+#include <future>
+#include <stdexcept>
 #include <unordered_map>
 #include <unordered_map>
 #include "parse_cl.h"
 #include "parse_cl.h"
 
 
@@ -52,6 +54,7 @@ shared_ptr<PeerConnection> createPeerConnection(const Configuration &config,
 void printReceived(bool echoed, string id, string type, size_t length);
 void printReceived(bool echoed, string id, string type, size_t length);
 string randomId(size_t length);
 string randomId(size_t length);
 
 
+
 int main(int argc, char **argv) try {
 int main(int argc, char **argv) try {
 	auto params = std::make_unique<Cmdline>(argc, argv);
 	auto params = std::make_unique<Cmdline>(argc, argv);
 
 
@@ -79,12 +82,21 @@ int main(int argc, char **argv) try {
 
 
 	auto ws = make_shared<WebSocket>();
 	auto ws = make_shared<WebSocket>();
 
 
-	ws->onOpen([]() { cout << "WebSocket connected, signaling ready" << endl; });
+	std::promise<void> wsPromise;
+	auto wsFuture = wsPromise.get_future();
+	
+	ws->onOpen([&wsPromise]() { 
+		cout << "WebSocket connected, signaling ready" << endl;
+		wsPromise.set_value();
+	});
 
 
+	ws->onError([&wsPromise](string s) { 
+		cout << "WebSocket error" << endl;
+		wsPromise.set_exception(std::make_exception_ptr(std::runtime_error(s)));
+	});
+	
 	ws->onClosed([]() { cout << "WebSocket closed" << endl; });
 	ws->onClosed([]() { cout << "WebSocket closed" << endl; });
-
-	ws->onError([](const string &error) { cout << "WebSocket failed: " << error << endl; });
-
+	
 	ws->onMessage([&](variant<binary, string> data) {
 	ws->onMessage([&](variant<binary, string> data) {
 		if (!holds_alternative<string>(data))
 		if (!holds_alternative<string>(data))
 			return;
 			return;
@@ -129,13 +141,9 @@ int main(int argc, char **argv) try {
 		to_string(params->webSocketPort()) + "/" + localId;
 		to_string(params->webSocketPort()) + "/" + localId;
 	cout << "Url is " << url << endl;
 	cout << "Url is " << url << endl;
 	ws->open(url);
 	ws->open(url);
-
+	
 	cout << "Waiting for signaling to be connected..." << endl;
 	cout << "Waiting for signaling to be connected..." << endl;
-	while (!ws->isOpen()) {
-		if (ws->isClosed())
-			return 1;
-		this_thread::sleep_for(100ms);
-	}
+	wsFuture.get();
 
 
 	while (true) {
 	while (true) {
 		string id;
 		string id;