@@ -146,37 +146,10 @@ enum class WebSocketReadyState : uint8_t {
146146struct WebSocketHeader_t ;
147147/* * @endcond */
148148
149- /* * @class WebSocket */
150- template <class NetClient > class WebSocket {
151- template <class , class , uint8_t > friend class WebSocketServer ;
152-
153- public:
154- /* *
155- * @param ws Closing endpoint.
156- * @param code Close event code.
157- * @param reason Contains a message for a close event, c-string, non
158- * NULL-terminated. Might be empty.
159- * @param length The number of characters in the reason c-string.
160- */
161- using onCloseCallback = void (*)(
162- WebSocket &ws, WebSocketCloseCode code, const char *reason,
163- uint16_t length);
164-
165- /* *
166- * @param ws Source of a message.
167- * @param dataType Type of a message.
168- * @param message Non NULL-terminated.
169- * @param length Number of data bytes.
170- */
171- using onMessageCallback = void (*)(
172- WebSocket &ws, WebSocketDataType dataType, const char *message,
173- uint16_t length);
174-
149+ /* * @class IWebSocket */
150+ class IWebSocket {
175151public:
176- WebSocket (const WebSocket &) = delete ;
177- virtual ~WebSocket ();
178-
179- WebSocket &operator =(const WebSocket &) = delete ;
152+ virtual ~IWebSocket () = default ;
180153
181154 /* *
182155 * @brief Sends a close event.
@@ -185,36 +158,48 @@ template <class NetClient> class WebSocket {
185158 * NULL-terminated. Max length = 123 characters.
186159 * @param length The number of characters in the reason c-string.
187160 */
188- void close (
189- WebSocketCloseCode, bool instant, const char *reason = nullptr ,
190- uint16_t length = 0 ) ;
161+ virtual void close (
162+ WebSocketCloseCode, bool instant, const char *reason,
163+ uint16_t length) = 0;
191164 /* * @brief Immediately closes the connection. */
192- void terminate ();
165+ virtual void terminate () = 0 ;
193166
194167 /* * @return Endpoint connection status. */
195- WebSocketReadyState getReadyState () const ;
168+ virtual WebSocketReadyState getReadyState () const = 0 ;
196169 /* * @brief Verifies endpoint connection. */
197- bool isAlive () const ;
170+ virtual bool isAlive () const = 0 ;
198171
199172 /* *
200173 * @return Endpoint IP address.
201174 * @remark For some microcontrollers it might be empty.
202175 */
203- IPAddress getRemoteIP () const ;
204- const char *getProtocol () const ;
176+ virtual IPAddress getRemoteIP () const = 0 ;
177+ virtual const char *getProtocol () const = 0 ;
205178
206179 /* *
207180 * @brief Sends a message frame.
208181 * @param message Doesn't have to be NULL-terminated.
209182 */
210- void send (WebSocketDataType, const char *message, uint16_t length);
183+ virtual void
184+ send (WebSocketDataType, const char *message, uint16_t length) = 0 ;
211185 /* *
212186 * @brief Sends a ping message.
213187 * @param payload An additional message, doesn't have to be NULL-terminated.
214188 * Max length = 125.
215189 * @param length The number of characters in payload.
216190 */
217- void ping (const char *payload = nullptr , uint16_t length = 0 );
191+ virtual void ping (const char *payload, uint16_t length) = 0;
192+
193+ /* *
194+ * @param ws Closing endpoint.
195+ * @param code Close event code.
196+ * @param reason Contains a message for a close event, c-string, non
197+ * NULL-terminated. Might be empty.
198+ * @param length The number of characters in the reason c-string.
199+ */
200+ using onCloseCallback = void (*)(
201+ IWebSocket &ws, WebSocketCloseCode code, const char *reason,
202+ uint16_t length);
218203
219204 /* *
220205 * @brief Sets the close event handler.
@@ -225,7 +210,18 @@ template <class NetClient> class WebSocket {
225210 * });
226211 * @endcode
227212 */
228- void onClose (const onCloseCallback &);
213+ virtual void onClose (const onCloseCallback &) = 0;
214+
215+ /* *
216+ * @param ws Source of a message.
217+ * @param dataType Type of a message.
218+ * @param message Non NULL-terminated.
219+ * @param length Number of data bytes.
220+ */
221+ using onMessageCallback = void (*)(
222+ IWebSocket &ws, WebSocketDataType dataType, const char *message,
223+ uint16_t length);
224+
229225 /* *
230226 * @brief Sets the message handler function.
231227 * @code{.cpp}
@@ -235,6 +231,36 @@ template <class NetClient> class WebSocket {
235231 * });
236232 * @endcode
237233 */
234+ virtual void onMessage (const onMessageCallback &) = 0;
235+ };
236+
237+ /* * @class WebSocket */
238+ template <class NetClient > class WebSocket : public IWebSocket {
239+ template <class , class , uint8_t > friend class WebSocketServer ;
240+
241+ public:
242+ public:
243+ WebSocket (const WebSocket &) = delete ;
244+ ~WebSocket () override ;
245+
246+ WebSocket &operator =(const WebSocket &) = delete ;
247+
248+ void close (
249+ WebSocketCloseCode, bool instant, const char *reason = nullptr ,
250+ uint16_t length = 0 );
251+ void terminate ();
252+
253+ WebSocketReadyState getReadyState () const ;
254+ bool isAlive () const ;
255+
256+ IPAddress getRemoteIP () const ;
257+ const char *getProtocol () const ;
258+
259+ void send (WebSocketDataType, const char *message, uint16_t length);
260+
261+ void ping (const char *payload = nullptr , uint16_t length = 0 );
262+
263+ void onClose (const onCloseCallback &);
238264 void onMessage (const onMessageCallback &);
239265
240266protected:
@@ -293,9 +319,9 @@ void encodeSecKey(const char *key, char output[]);
293319void generateMask (char output[]);
294320
295321template <typename NetClient>
296- inline IPAddress fetchRemoteIp (NetClient & &client) {
322+ inline IPAddress fetchRemoteIp (const NetClient &client) {
297323 if constexpr (has_remoteIP<NetClient>::value)
298- return client.remoteIP ();
324+ return const_cast <NetClient &>( client) .remoteIP ();
299325 else
300326 return {};
301327}
0 commit comments