File tree 4 files changed +41
-8
lines changed
4 files changed +41
-8
lines changed Original file line number Diff line number Diff line change @@ -34,7 +34,7 @@ class WebSocketClient {
34
34
35
35
void startStream () const ;
36
36
37
- void run ();
37
+ int run ();
38
38
39
39
private:
40
40
WebSocketConnection connection;
Original file line number Diff line number Diff line change @@ -16,7 +16,8 @@ enum Commands {
16
16
SAVE,
17
17
HELP,
18
18
EXIT,
19
- UNKNOWN,
19
+ CLOSE,
20
+ UNKNOWN
20
21
};
21
22
22
23
inline Commands stringToCommand (const std::string &commandStr) {
@@ -44,6 +45,9 @@ inline Commands stringToCommand(const std::string &commandStr) {
44
45
if (commandStr == " exit" ) {
45
46
return EXIT;
46
47
}
48
+ if (commandStr == " close" ) {
49
+ return CLOSE;
50
+ }
47
51
return UNKNOWN;
48
52
}
49
53
@@ -56,6 +60,7 @@ inline void printHelp() {
56
60
<< " start - Start streaming\n "
57
61
<< " save - Take a screenshot and save it\n "
58
62
<< " help - List available commands\n "
63
+ << " close - Close the websocket connection\n "
59
64
<< " exit - Terminate the program\n " ;
60
65
}
61
66
Original file line number Diff line number Diff line change 2
2
// Created by Katie on 15/11/2024.
3
3
//
4
4
5
+ #include < chrono>
6
+ #include < iostream>
7
+ #include < thread>
5
8
#include " includes/api/WebSocketClient.h"
6
9
7
10
int main () {
11
+ std::string address;
12
+ std::string port;
13
+ std::string endpoint;
14
+
15
+ int connectionStatus = 0 ;
16
+ do {
17
+ std::cout << " Enter WebSocket server address:\n " ;
18
+ std::cin >> address;
19
+
20
+ std::cout << " Enter WebSocket server port:\n " ;
21
+ std::cin >> port;
22
+
23
+ std::cout << " Enter WebSocket endpoint:\n " ;
24
+ std::cin >> endpoint;
25
+
26
+ WebSocketClient client (address, port, " /publisher/" + endpoint);
27
+ connectionStatus = client.run ();
28
+
29
+ std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
30
+ } while (connectionStatus != 0 );
31
+
8
32
// WebSocketClient client("192.168.1.102", "8080", "/publisher/matrix");
9
- WebSocketClient client (" localhost" , " 8080" , " /publisher/matrix-test" );
10
- client. run ();
33
+ // WebSocketClient client("localhost", "8080", "/publisher/matrix-test");
34
+
11
35
return 0 ;
12
36
}
Original file line number Diff line number Diff line change @@ -77,16 +77,16 @@ void WebSocketClient::startStream() const {
77
77
}
78
78
}
79
79
80
- void WebSocketClient::run () {
80
+ int WebSocketClient::run () {
81
81
WSADATA wsaData;
82
82
if (WSAStartup (MAKEWORD (2 , 2 ), &wsaData) != 0 ) {
83
83
std::cerr << " WSAStartup failed" << std::endl;
84
- return ;
84
+ return 1 ;
85
85
}
86
86
87
87
if (!connection.connect ()) {
88
88
std::cerr << " Failed to connect to WebSocket server" << std::endl;
89
- return ;
89
+ return 1 ;
90
90
}
91
91
92
92
printHelp ();
@@ -125,7 +125,7 @@ void WebSocketClient::run() {
125
125
case START:
126
126
keepRunning = true ;
127
127
startStream ();
128
- break ; // we can interrupt with Ctrl+C, so we need that
128
+ break ;
129
129
case SAVE:
130
130
saveScreenShot ();
131
131
break ;
@@ -135,6 +135,9 @@ void WebSocketClient::run() {
135
135
case EXIT:
136
136
running = false ;
137
137
break ;
138
+ case CLOSE:
139
+ std::cout << " Closing websocket connection..." << std::endl;
140
+ return 1 ;
138
141
default :
139
142
std::cout << " Unknown command" << std::endl;
140
143
break ;
@@ -146,4 +149,5 @@ void WebSocketClient::run() {
146
149
}
147
150
148
151
connection.closeConnection ();
152
+ return 0 ;
149
153
}
You can’t perform that action at this time.
0 commit comments