Skip to content

Commit 060660f

Browse files
committed
Added basic connection guide
1 parent eb7102d commit 060660f

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

includes/api/WebSocketClient.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class WebSocketClient {
3434

3535
void startStream() const;
3636

37-
void run();
37+
int run();
3838

3939
private:
4040
WebSocketConnection connection;

includes/enums/Commands.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ enum Commands {
1616
SAVE,
1717
HELP,
1818
EXIT,
19-
UNKNOWN,
19+
CLOSE,
20+
UNKNOWN
2021
};
2122

2223
inline Commands stringToCommand(const std::string &commandStr) {
@@ -44,6 +45,9 @@ inline Commands stringToCommand(const std::string &commandStr) {
4445
if (commandStr == "exit") {
4546
return EXIT;
4647
}
48+
if (commandStr == "close") {
49+
return CLOSE;
50+
}
4751
return UNKNOWN;
4852
}
4953

@@ -56,6 +60,7 @@ inline void printHelp() {
5660
<< " start - Start streaming\n"
5761
<< " save - Take a screenshot and save it\n"
5862
<< " help - List available commands\n"
63+
<< " close - Close the websocket connection\n"
5964
<< " exit - Terminate the program\n";
6065
}
6166

main.cpp

+26-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,35 @@
22
// Created by Katie on 15/11/2024.
33
//
44

5+
#include <chrono>
6+
#include <iostream>
7+
#include <thread>
58
#include "includes/api/WebSocketClient.h"
69

710
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+
832
// 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+
1135
return 0;
1236
}

src/api/WebSocketClient.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,16 @@ void WebSocketClient::startStream() const {
7777
}
7878
}
7979

80-
void WebSocketClient::run() {
80+
int WebSocketClient::run() {
8181
WSADATA wsaData;
8282
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
8383
std::cerr << "WSAStartup failed" << std::endl;
84-
return;
84+
return 1;
8585
}
8686

8787
if (!connection.connect()) {
8888
std::cerr << "Failed to connect to WebSocket server" << std::endl;
89-
return;
89+
return 1;
9090
}
9191

9292
printHelp();
@@ -125,7 +125,7 @@ void WebSocketClient::run() {
125125
case START:
126126
keepRunning = true;
127127
startStream();
128-
break; // we can interrupt with Ctrl+C, so we need that
128+
break;
129129
case SAVE:
130130
saveScreenShot();
131131
break;
@@ -135,6 +135,9 @@ void WebSocketClient::run() {
135135
case EXIT:
136136
running = false;
137137
break;
138+
case CLOSE:
139+
std::cout << "Closing websocket connection..." << std::endl;
140+
return 1;
138141
default:
139142
std::cout << "Unknown command" << std::endl;
140143
break;
@@ -146,4 +149,5 @@ void WebSocketClient::run() {
146149
}
147150

148151
connection.closeConnection();
152+
return 0;
149153
}

0 commit comments

Comments
 (0)