-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathws_client.cpp
More file actions
220 lines (179 loc) · 6.2 KB
/
ws_client.cpp
File metadata and controls
220 lines (179 loc) · 6.2 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// #include <libwebsockets.h>
// #include <string.h>
// #include <iostream>
// static int callback_ws(struct lws *wsi, enum lws_callback_reasons reason,
// void *user, void *in, size_t len) {
// switch (reason) {
// case LWS_CALLBACK_CLIENT_ESTABLISHED:
// std::cout << "[Connected]\n";
// lws_callback_on_writable(wsi);
// break;
// case LWS_CALLBACK_CLIENT_RECEIVE:
// std::cout << "[Message] " << std::string((char*)in, len) << "\n";
// break;
// case LWS_CALLBACK_CLIENT_WRITEABLE: {
// const char* msg = "{\"type\":\"hello\"}";
// unsigned char buf[LWS_PRE + 512];
// size_t msg_len = strlen(msg);
// memcpy(&buf[LWS_PRE], msg, msg_len);
// lws_write(wsi, &buf[LWS_PRE], msg_len, LWS_WRITE_TEXT);
// break;
// }
// case LWS_CALLBACK_CLOSED:
// std::cout << "[Disconnected]\n";
// break;
// default:
// break;
// }
// return 0;
// }
// static struct lws_protocols protocols[] = {
// { "my-protocol", callback_ws, 0, 4096 },
// { NULL, NULL, 0, 0 } // terminator
// };
// int main() {
// struct lws_context_creation_info info;
// memset(&info, 0, sizeof(info));
// info.port = CONTEXT_PORT_NO_LISTEN;
// info.protocols = protocols;
// info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
// struct lws_context *context = lws_create_context(&info);
// struct lws_client_connect_info ccinfo = {};
// ccinfo.context = context;
// ccinfo.address = "echo.websocket.events";
// ccinfo.port = 443;
// ccinfo.path = "/";
// ccinfo.host = ccinfo.address;
// ccinfo.origin = ccinfo.address;
// ccinfo.ssl_connection = LCCSCF_USE_SSL;
// ccinfo.protocol = "my-protocol";
// lws_client_connect_via_info(&ccinfo);
// while (lws_service(context, 100) >= 0);
// lws_context_destroy(context);
// return 0;
// }
// /// g++ -std=c++20 -O2 ws_client.cpp -o ws_client -lwebsockets -lssl -lcrypto -lz -lpthread -lws2_32
// File: ws_client.cpp
#include <libwebsockets.h>
#include <iostream>
#include <string>
#include <thread>
#include <atomic>
#include <functional>
#include <cstring>
class uClient {
public:
using OnMessageCallback = std::function<void(const std::string&)>;
uClient() {}
~uClient() { close(); }
bool connect(const std::string& uri) {
host = "echo.websocket.events"; // hardcoded for now
path = "/";
port = 443;
static struct lws_protocols protocols[] = {
{ "my-protocol", &uClient::callback, 0, 4096, 0, this, 0 },
{ NULL, NULL, 0, 0, 0, NULL, 0 }
};
struct lws_context_creation_info info;
memset(&info, 0, sizeof(info));
info.port = CONTEXT_PORT_NO_LISTEN;
info.protocols = protocols;
info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
context = lws_create_context(&info);
if (!context) return false;
struct lws_client_connect_info ccinfo = {};
ccinfo.context = context;
ccinfo.address = host.c_str();
ccinfo.port = port;
ccinfo.path = path.c_str();
ccinfo.host = host.c_str();
ccinfo.origin = host.c_str();
ccinfo.ssl_connection = LCCSCF_USE_SSL;
ccinfo.protocol = "my-protocol";
ccinfo.userdata = this;
wsi = lws_client_connect_via_info(&ccinfo);
if (!wsi) return false;
running = true;
clientThread = std::thread([this]() {
while (running) {
lws_service(context, 100);
}
});
return true;
}
void send(const std::string& message) {
sendBuffer = message;
wantToSend = true;
if (wsi) lws_callback_on_writable(wsi);
}
void close() {
running = false;
if (clientThread.joinable()) {
clientThread.join();
}
if (context) {
lws_context_destroy(context);
context = nullptr;
}
}
void setOnMessage(OnMessageCallback callback) {
onMessage = callback;
}
private:
static int callback(struct lws* wsi, enum lws_callback_reasons reason,
void* user, void* in, size_t len) {
uClient* self = reinterpret_cast<uClient*>(user);
switch (reason) {
case LWS_CALLBACK_CLIENT_ESTABLISHED:
std::cout << "[uClient] Connected\n";
break;
case LWS_CALLBACK_CLIENT_RECEIVE:
if (self && self->onMessage) {
self->onMessage(std::string((const char*)in, len));
}
break;
case LWS_CALLBACK_CLIENT_WRITEABLE:
if (self && self->wantToSend) {
unsigned char buf[LWS_PRE + 1024];
std::memcpy(&buf[LWS_PRE], self->sendBuffer.c_str(), self->sendBuffer.size());
lws_write(wsi, &buf[LWS_PRE], self->sendBuffer.size(), LWS_WRITE_TEXT);
self->wantToSend = false;
}
break;
case LWS_CALLBACK_CLOSED:
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
std::cout << "[uClient] Disconnected\n";
break;
default:
break;
}
return 0;
}
std::thread clientThread;
std::atomic<bool> running{false};
struct lws_context* context = nullptr;
struct lws* wsi = nullptr;
OnMessageCallback onMessage;
std::string sendBuffer;
std::atomic<bool> wantToSend{false};
std::string host;
std::string path;
int port = 443;
};
// -----------------------------
// 🧪 Test it
int main() {
uClient client;
client.setOnMessage([](const std::string& msg) {
std::cout << "[Server] " << msg << "\n";
});
if (client.connect("wss://echo.websocket.events")) {
std::this_thread::sleep_for(std::chrono::seconds(2));
client.send("Hello from one-file client!");
std::this_thread::sleep_for(std::chrono::seconds(5));
client.close();
} else {
std::cerr << "Failed to connect.\n";
}
return 0;
}