websocketclient.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Websocket client implementation
  2. *
  3. * Copyright (c) 2016 Luís Fonseca <miguelluisfonseca@gmail.com>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #ifndef _WEBSOCKET_H_
  25. #define _WEBSOCKET_H_
  26. #include "osapi.h"
  27. #include "user_interface.h"
  28. #include "espconn.h"
  29. #include "mem.h"
  30. #include <limits.h>
  31. #include <stdlib.h>
  32. #if defined(USES_SDK_BEFORE_V140)
  33. #define espconn_send espconn_sent
  34. #define espconn_secure_send espconn_secure_sent
  35. #endif
  36. struct ws_info;
  37. typedef void (*ws_onConnectionCallback)(struct ws_info *wsInfo);
  38. typedef void (*ws_onReceiveCallback)(struct ws_info *wsInfo, int len, char *message, int opCode);
  39. typedef void (*ws_onFailureCallback)(struct ws_info *wsInfo, int errorCode);
  40. typedef struct {
  41. char *key;
  42. char *value;
  43. } header_t;
  44. typedef struct ws_info {
  45. int connectionState;
  46. bool isSecure;
  47. char *hostname;
  48. int port;
  49. char *path;
  50. char *expectedSecKey;
  51. header_t *extraHeaders;
  52. struct espconn *conn;
  53. void *reservedData;
  54. int knownFailureCode;
  55. char *frameBuffer;
  56. int frameBufferLen;
  57. char *payloadBuffer;
  58. int payloadBufferLen;
  59. int payloadOriginalOpCode;
  60. os_timer_t timeoutTimer;
  61. int unhealthyPoints;
  62. ws_onConnectionCallback onConnection;
  63. ws_onReceiveCallback onReceive;
  64. ws_onFailureCallback onFailure;
  65. } ws_info;
  66. /*
  67. * Attempts to estabilish a websocket connection to the given url.
  68. */
  69. void ws_connect(ws_info *wsInfo, const char *url);
  70. /*
  71. * Sends a message with a given opcode.
  72. */
  73. void ws_send(ws_info *wsInfo, int opCode, const char *message, unsigned short length);
  74. /*
  75. * Disconnects existing conection and frees memory.
  76. */
  77. void ws_close(ws_info *wsInfo);
  78. #endif // _WEBSOCKET_H_