WebSocketClientStomp.ino 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. WebSocketClientStomp.ino
  3. Example for connecting and maintining a connection with a STOMP websocket connection.
  4. In this example, we connect to a Spring application (see https://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html).
  5. Created on: 25.09.2017
  6. Author: Martin Becker <mgbckr>, Contact: becker@informatik.uni-wuerzburg.de
  7. */
  8. // PRE
  9. #define USE_SERIAL Serial
  10. // LIBRARIES
  11. #include <Arduino.h>
  12. #include <Hash.h>
  13. #include <ESP8266WiFi.h>
  14. #include <WebSocketsClient.h>
  15. // SETTINGS
  16. const char* wlan_ssid = "yourssid";
  17. const char* wlan_password = "somepassword";
  18. const char* ws_host = "the.host.net";
  19. const int ws_port = 80;
  20. // URL for STOMP endpoint.
  21. // For the default config of Spring's STOMP support, the default URL is "/socketentry/websocket".
  22. const char* stompUrl = "/socketentry/websocket"; // don't forget the leading "/" !!!
  23. // VARIABLES
  24. WebSocketsClient webSocket;
  25. // FUNCTIONS
  26. /**
  27. * STOMP messages need to be NULL-terminated (i.e., \0 or \u0000).
  28. * However, when we send a String or a char[] array without specifying
  29. * a length, the size of the message payload is derived by strlen() internally,
  30. * thus dropping any NULL values appended to the "msg"-String.
  31. *
  32. * To solve this, we first convert the String to a NULL terminated char[] array
  33. * via "c_str" and set the length of the payload to include the NULL value.
  34. */
  35. void sendMessage(String & msg) {
  36. webSocket.sendTXT(msg.c_str(), msg.length() + 1);
  37. }
  38. void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
  39. switch (type) {
  40. case WStype_DISCONNECTED:
  41. USE_SERIAL.printf("[WSc] Disconnected!\n");
  42. break;
  43. case WStype_CONNECTED:
  44. {
  45. USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
  46. String msg = "CONNECT\r\naccept-version:1.1,1.0\r\nheart-beat:10000,10000\r\n\r\n";
  47. sendMessage(msg);
  48. }
  49. break;
  50. case WStype_TEXT:
  51. {
  52. // #####################
  53. // handle STOMP protocol
  54. // #####################
  55. String text = (char*) payload;
  56. USE_SERIAL.printf("[WSc] get text: %s\n", payload);
  57. if (text.startsWith("CONNECTED")) {
  58. // subscribe to some channels
  59. String msg = "SUBSCRIBE\nid:sub-0\ndestination:/user/queue/messages\n\n";
  60. sendMessage(msg);
  61. delay(1000);
  62. // and send a message
  63. msg = "SEND\ndestination:/app/message\n\n{\"user\":\"esp\",\"message\":\"Hello!\"}";
  64. sendMessage(msg);
  65. delay(1000);
  66. } else {
  67. // do something with messages
  68. }
  69. break;
  70. }
  71. case WStype_BIN:
  72. USE_SERIAL.printf("[WSc] get binary length: %u\n", length);
  73. hexdump(payload, length);
  74. // send data to server
  75. // webSocket.sendBIN(payload, length);
  76. break;
  77. }
  78. }
  79. void setup() {
  80. // setup serial
  81. // USE_SERIAL.begin(921600);
  82. USE_SERIAL.begin(115200);
  83. // USE_SERIAL.setDebugOutput(true);
  84. USE_SERIAL.println();
  85. // connect to WiFi
  86. USE_SERIAL.print("Logging into WLAN: "); Serial.print(wlan_ssid); Serial.print(" ...");
  87. WiFi.mode(WIFI_STA);
  88. WiFi.begin(wlan_ssid, wlan_password);
  89. while (WiFi.status() != WL_CONNECTED) {
  90. delay(500);
  91. USE_SERIAL.print(".");
  92. }
  93. USE_SERIAL.println(" success.");
  94. USE_SERIAL.print("IP: "); USE_SERIAL.println(WiFi.localIP());
  95. // connect to websocket
  96. webSocket.begin(ws_host, ws_port, stompUrl);
  97. webSocket.setExtraHeaders(); // remove "Origin: file://" header because it breaks the connection with Spring's default websocket config
  98. // webSocket.setExtraHeaders("foo: I am so funny\r\nbar: not"); // some headers, in case you feel funny
  99. webSocket.onEvent(webSocketEvent);
  100. }
  101. void loop() {
  102. webSocket.loop();
  103. }