MCPcopy Create free account
hub / github.com/ayushsharma82/WebSerial / begin

Method begin

src/WebSerial.cpp:50–98  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

48}
49
50void WebSerialClass::begin(AsyncWebServer *server, const char* url) {
51 _server = server;
52 _ws = new AsyncWebSocket("/wserial");
53
54 if (_authenticate) {
55 _ws->setAuthentication(_username.c_str(), _password.c_str());
56 }
57
58 // Webpage Handler
59 _server->on(url, HTTP_GET, [&](AsyncWebServerRequest *request){
60 if(_authenticate){
61 if(!request->authenticate(_username.c_str(), _password.c_str()))
62 return request->requestAuthentication();
63 }
64 AsyncWebServerResponse *response = request->beginResponse(200, "text/html", WEBSERIAL_HTML, sizeof(WEBSERIAL_HTML));
65 response->addHeader("Content-Encoding", "gzip");
66 request->send(response);
67 });
68
69 // WS Handler
70 _ws->onEvent([&](__unused AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, __unused void * arg, uint8_t *data, __unused size_t len) -> void {
71 // if(type == WS_EVT_CONNECT){
72 // } else if(type == WS_EVT_DISCONNECT){
73 // } else if(type == WS_EVT_DATA){
74 if (type == WS_EVT_CONNECT) {
75 client->setCloseClientOnQueueFull(false);
76 return;
77 }
78 if(type == WS_EVT_DATA){
79 // Detect magic bytes
80 if (data[0] == WSL_MAGIC_BYTE_1 && data[1] == WSL_MAGIC_BYTE_2) {
81 if (data[2] == WSLPacketType::WSL_MESSAGE) {
82 // Parse message size (uint16_t)
83 size_t message_size = (data[4] << 8) | data[3];
84 // Issue callback
85 if(_recv != nullptr){
86 _recv(data + 5, message_size);
87 }
88 } else if (data[2] == WSLPacketType::WSL_PING) {
89 // Send pong
90 client->binary(WSL_PONG_MSG, WSL_PONG_MSG_LEN);
91 }
92 }
93 }
94 });
95
96 // Attach AsyncWebServer with Websockets
97 _server->addHandler(_ws);
98}
99
100// onMessage Callback Handler
101void WebSerialClass::onMessage(WSLMessageHandler recv) {

Callers

nothing calls this directly

Calls 2

sendMethod · 0.80
setAuthenticationMethod · 0.45

Tested by

no test coverage detected