forked from paullouisageneau/libdatachannel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebsocket.cpp
71 lines (52 loc) · 1.7 KB
/
websocket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Copyright (c) 2019 Paul-Louis Ageneau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "rtc/rtc.hpp"
#if RTC_ENABLE_WEBSOCKET
#include <atomic>
#include <chrono>
#include <iostream>
#include <memory>
#include <thread>
using namespace rtc;
using namespace std;
template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
void test_websocket() {
InitLogger(LogLevel::Debug);
const string myMessage = "Hello world from libdatachannel";
WebSocket::Configuration config;
config.disableTlsVerification = true;
WebSocket ws(std::move(config));
ws.onOpen([&ws, &myMessage]() {
cout << "WebSocket: Open" << endl;
ws.send(myMessage);
});
ws.onError([](string error) { cout << "WebSocket: Error: " << error << endl; });
ws.onClosed([]() { cout << "WebSocket: Closed" << endl; });
std::atomic<bool> received = false;
ws.onMessage([&received, &myMessage](variant<binary, string> message) {
if (holds_alternative<string>(message)) {
string str = std::move(get<string>(message));
if ((received = (str == myMessage)))
cout << "WebSocket: Received expected message" << endl;
else
cout << "WebSocket: Received UNEXPECTED message" << endl;
}
});
ws.open("wss://echo.websocket.org:443/");
int attempts = 20;
while ((!ws.isOpen() || !received) && attempts--)
this_thread::sleep_for(1s);
if (!ws.isOpen())
throw runtime_error("WebSocket is not open");
if (!received)
throw runtime_error("Expected message not received");
ws.close();
this_thread::sleep_for(1s);
cout << "Success" << endl;
}
#endif