WebSockets4WebServer.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @file WebSocketsServer.cpp
  3. * @date 28.10.2020
  4. * @author Markus Sattler & esp8266/arduino community
  5. *
  6. * Copyright (c) 2020 Markus Sattler. All rights reserved.
  7. * This file is part of the WebSockets for Arduino.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. */
  24. #ifndef __WEBSOCKETS4WEBSERVER_H
  25. #define __WEBSOCKETS4WEBSERVER_H
  26. #include <WebSocketsServer.h>
  27. #include <ESP8266WebServer.h>
  28. #if WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266 && WEBSERVER_HAS_HOOK
  29. class WebSockets4WebServer : public WebSocketsServerCore {
  30. public:
  31. WebSockets4WebServer(const String & origin = "", const String & protocol = "arduino")
  32. : WebSocketsServerCore(origin, protocol) {
  33. begin();
  34. }
  35. ESP8266WebServer::HookFunction hookForWebserver(const String & wsRootDir, WebSocketServerEvent event) {
  36. onEvent(event);
  37. return [&, wsRootDir](const String & method, const String & url, WiFiClient * tcpClient, ESP8266WebServer::ContentTypeFunction contentType) {
  38. (void)contentType;
  39. if(!(method == "GET" && url.indexOf(wsRootDir) == 0)) {
  40. return ESP8266WebServer::CLIENT_REQUEST_CAN_CONTINUE;
  41. }
  42. // allocate a WiFiClient copy (like in WebSocketsServer::handleNewClients())
  43. WEBSOCKETS_NETWORK_CLASS * newTcpClient = new WEBSOCKETS_NETWORK_CLASS(*tcpClient);
  44. // Then initialize a new WSclient_t (like in WebSocketsServer::handleNewClient())
  45. WSclient_t * client = handleNewClient(newTcpClient);
  46. if(client) {
  47. // give "GET <url>"
  48. String headerLine;
  49. headerLine.reserve(url.length() + 5);
  50. headerLine = "GET ";
  51. headerLine += url;
  52. handleHeader(client, &headerLine);
  53. }
  54. // tell webserver to not close but forget about this client
  55. return ESP8266WebServer::CLIENT_IS_GIVEN;
  56. };
  57. }
  58. };
  59. #else // WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266 && WEBSERVER_HAS_HOOK
  60. #ifndef WEBSERVER_HAS_HOOK
  61. #error Your current Framework / Arduino core version does not support Webserver Hook Functions
  62. #else
  63. #error Your Hardware Platform does not support Webserver Hook Functions
  64. #endif
  65. #endif // WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266 && WEBSERVER_HAS_HOOK
  66. #endif // __WEBSOCKETS4WEBSERVER_H