-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
79 lines (66 loc) · 2.13 KB
/
main.cpp
File metadata and controls
79 lines (66 loc) · 2.13 KB
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
72
73
74
75
76
77
78
79
// main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <json.hpp>
#include "client.h"
bool readConfig(std::string &serverAddr, std::string &username, std::string &password) {
std::ifstream configFile("config.json");
if (!configFile.is_open()) {
return false;
}
nlohmann::json config;
configFile >> config;
if (config.contains("username") && config.contains("password") && config.contains("serverAddr")) {
username = config["username"];
password = config["password"];
serverAddr = config["serverAddr"];
return true;
}
return false;
}
void saveConfig(const std::string &serverAddr, const std::string &username, const std::string &password) {
nlohmann::ordered_json config;
config["username"] = username;
config["password"] = password;
config["serverAddr"] = serverAddr;
std::ofstream configFile("config.json");
configFile << config.dump(4);
}
int main(int argc, char *argv[]) {
std::string serverAddr, username, password;
int port1, port2;
if (readConfig(serverAddr, username, password)) {
std::cout << "读取配置成功" << std::endl;
} else {
std::cout << "请输入服务器地址(如 127.0.0.1:5666): ";
std::cin >> serverAddr;
std::cout << "请输入用户名: ";
std::cin >> username;
std::cout << "请输入密码: ";
std::cin >> password;
saveConfig(serverAddr, username, password);
}
if (argc == 3) {
port1 = std::stoi(argv[1]);
port2 = std::stoi(argv[2]);
}else {
std::cout << "请输入端口1: ";
std::cin >> port1;
std::cout << "请输入端口2: ";
std::cin >> port2;
}
std::string wsUrl = "ws://" + serverAddr + "/websocket?type=main";
// 创建客户端对象
client fnos;
// 建立连接
fnos.connect(wsUrl); //异步
//获取公钥
fnos.getRSAPub();
// 登陆
fnos.login(username, password);
// 设置端口
fnos.setting_port(port1, port2, false, true);
std::this_thread::sleep_for(std::chrono::milliseconds(3000));
return 0;
}