-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebSockets.cpp
More file actions
99 lines (78 loc) · 2.31 KB
/
Copy pathWebSockets.cpp
File metadata and controls
99 lines (78 loc) · 2.31 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
/*
* =====================================================================================
*
* Filename: WebSockets.cpp
*
* Description:
*
* Version: 1.0
* Created: 03/03/2022 09:57:37 AM
* Revision: none
* Compiler: gcc
*
* Author: Yair Gadelov (yg), yair.gadelov@gmail.com
* Company: Israel
*
* =====================================================================================
*/
#include "WebSockets.h"
#include <string>
#include <string.h>
//https://github.com/warmcat/libwebsockets/blob/main/minimal-examples-lowlevel/ws-client/minimal-ws-client-rx/libwebsockets.org.cer
int callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason,
void *user, void *in, size_t len)
{
int n;
WebSockets::Protocol * p = 0;
if ((WebSockets::Protocol*)lws_get_protocol(wsi))
p = (WebSockets::Protocol*)lws_get_protocol(wsi)->user;
switch (reason) {
/* because we are protocols[0] ... */
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
lwsl_err("CLIENT_CONNECTION_ERROR: %s\n",
in ? (char *)in : "(null)");
p = (WebSockets::Protocol*)lws_get_protocol(wsi)->user;
//// p->ClearWsi();
break;
case LWS_CALLBACK_CLIENT_ESTABLISHED:
lwsl_user("%s: established\n", __func__);
p = (WebSockets::Protocol*)lws_get_protocol(wsi)->user;
// lws_callback_on_writable(wsi);
p->setConnected(true);
break;
case LWS_CALLBACK_CLIENT_RECEIVE_PONG:
printf ("LWS_CALLBACK_CLIENT_RECEIVE_PONG");
break;
case LWS_CALLBACK_CLIENT_WRITEABLE:
{
// WebSockets * WebSockets = (WebSockets*)lws_get_protocol(wsi)->user;
// std::string msg = ws->getMsg();
// char c[255];
// c[0] = 0;
// strncpy (c, msg.c_str(),msg.size());
// c[msg.size()] = 0;
// n = lws_write(wsi, (unsigned char*) c, strlen(c) , LWS_WRITE_TEXT);
break;
}
case LWS_CALLBACK_CLIENT_RECEIVE:
time(&p->last_update_time);
if (lws_is_first_fragment(wsi) ) {
p->ClearMsg();
}
p->MsgAppend((char*)in,len);
if (lws_is_final_fragment(wsi)) {
bool res = p->CallBack();
if (res == false) {
//p->ClearWsi() ;
return -1;
}
};
break;
case LWS_CALLBACK_CLIENT_CLOSED:
//p->ClearWsi();
break;
default:
break;
}
return lws_callback_http_dummy(wsi, reason, user, in, len);
}