Doxygen with github
Loading...
Searching...
No Matches
ksWSServer.h
1/*
2 * Copyright (c) 2020-2026, Krzysztof Strehlau
3 *
4 * This file is a part of the ksIotFrameworkLib IoT library.
5 * All licensing information can be found inside LICENSE.md file.
6 *
7 * https://github.com/cziter15/ksIotFrameworkLib/blob/master/LICENSE
8 */
9
10#pragma once
11
12#include <memory>
13#include <cstdint>
14#include <functional>
15#include <string_view>
16#include <cstddef>
17
18namespace ksf::misc
19{
25 typedef std::function<void(uint8_t client, std::string_view message)> ksWsServerMessageFunc_t;
26
31 {
32 private:
33 class Impl;
34 std::unique_ptr<Impl> impl;
35
36 public:
41 ksWSServer(uint16_t port);
42
46 virtual ~ksWSServer();
47
58 uint64_t getRequiredAuthToken() const;
59
69 void setRequiredAuthToken(uint64_t authToken);
70
74 void begin();
75
79 void loop();
80
85 void setMessageHandler(ksWsServerMessageFunc_t func);
86
88 bool sendTXT(uint8_t client, const char* payload, std::size_t length = 0);
89
91 bool broadcastTXT(const char* payload, std::size_t length = 0);
92
94 void enableHeartbeat(uint32_t pingInterval, uint32_t pongTimeout, uint8_t disconnectTimeoutCount);
95 };
96}
Definition ksWSServer.cpp:18
A WebSocket server with authentication and text message handling.
Definition ksWSServer.h:31
virtual ~ksWSServer()
Destructs the server, releasing the resources.
void begin()
Starts the server.
Definition ksWSServer.cpp:145
void loop()
Handles WebSocket server logic.
Definition ksWSServer.cpp:150
ksWSServer(uint16_t port)
Prepares ksWSServer on specified port without actually starting it.
Definition ksWSServer.cpp:139
uint64_t getRequiredAuthToken() const
Returns simple authentication token for WebSocket authentication process.
Definition ksWSServer.cpp:155
bool broadcastTXT(const char *payload, std::size_t length=0)
Sends a text message to all clients. A zero length uses a null-terminated payload.
Definition ksWSServer.cpp:175
void setMessageHandler(ksWsServerMessageFunc_t func)
Installs a message handler to receive WebSocket text messages.
Definition ksWSServer.cpp:165
void enableHeartbeat(uint32_t pingInterval, uint32_t pongTimeout, uint8_t disconnectTimeoutCount)
Configures heartbeat intervals in milliseconds and the missed-pong limit.
Definition ksWSServer.cpp:180
void setRequiredAuthToken(uint64_t authToken)
Sets simple authtoken for WebSocket authentication.
Definition ksWSServer.cpp:160
bool sendTXT(uint8_t client, const char *payload, std::size_t length=0)
Sends a text message to one client. A zero length uses a null-terminated payload.
Definition ksWSServer.cpp:170