WebSocketClientStompOverSockJs.ino 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. WebSocketClientStompOverSockJs.ino
  3. Example for connecting and maintining a connection with a SockJS+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: 18.07.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. // base URL for SockJS (websocket) connection
  21. // The complete URL will look something like this(cf. http://sockjs.github.io/sockjs-protocol/sockjs-protocol-0.3.3.html#section-36):
  22. // ws://<ws_host>:<ws_port>/<ws_baseurl>/<3digits>/<randomstring>/websocket
  23. // For the default config of Spring's SockJS/STOMP support, the default base URL is "/socketentry/".
  24. const char* ws_baseurl = "/socketentry/"; // don't forget leading and trailing "/" !!!
  25. // VARIABLES
  26. WebSocketsClient webSocket;
  27. // FUNCTIONS
  28. void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
  29. switch (type) {
  30. case WStype_DISCONNECTED:
  31. USE_SERIAL.printf("[WSc] Disconnected!\n");
  32. break;
  33. case WStype_CONNECTED:
  34. {
  35. USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload);
  36. }
  37. break;
  38. case WStype_TEXT:
  39. {
  40. // #####################
  41. // handle SockJs+STOMP protocol
  42. // #####################
  43. String text = (char*) payload;
  44. USE_SERIAL.printf("[WSc] get text: %s\n", payload);
  45. if (payload[0] == 'h') {
  46. USE_SERIAL.println("Heartbeat!");
  47. } else if (payload[0] == 'o') {
  48. // on open connection
  49. char *msg = "[\"CONNECT\\naccept-version:1.1,1.0\\nheart-beat:10000,10000\\n\\n\\u0000\"]";
  50. webSocket.sendTXT(msg);
  51. } else if (text.startsWith("a[\"CONNECTED")) {
  52. // subscribe to some channels
  53. char *msg = "[\"SUBSCRIBE\\nid:sub-0\\ndestination:/user/queue/messages\\n\\n\\u0000\"]";
  54. webSocket.sendTXT(msg);
  55. delay(1000);
  56. // and send a message
  57. msg = "[\"SEND\\ndestination:/app/message\\n\\n{\\\"user\\\":\\\"esp\\\",\\\"message\\\":\\\"Hello!\\\"}\\u0000\"]";
  58. webSocket.sendTXT(msg);
  59. delay(1000);
  60. }
  61. break;
  62. }
  63. case WStype_BIN:
  64. USE_SERIAL.printf("[WSc] get binary length: %u\n", length);
  65. hexdump(payload, length);
  66. // send data to server
  67. // webSocket.sendBIN(payload, length);
  68. break;
  69. }
  70. }
  71. void setup() {
  72. // setup serial
  73. // USE_SERIAL.begin(921600);
  74. USE_SERIAL.begin(115200);
  75. // USE_SERIAL.setDebugOutput(true);
  76. USE_SERIAL.println();
  77. // connect to WiFi
  78. USE_SERIAL.print("Logging into WLAN: "); Serial.print(wlan_ssid); Serial.print(" ...");
  79. WiFi.mode(WIFI_STA);
  80. WiFi.begin(wlan_ssid, wlan_password);
  81. while (WiFi.status() != WL_CONNECTED) {
  82. delay(500);
  83. USE_SERIAL.print(".");
  84. }
  85. USE_SERIAL.println(" success.");
  86. USE_SERIAL.print("IP: "); USE_SERIAL.println(WiFi.localIP());
  87. // #####################
  88. // create socket url according to SockJS protocol (cf. http://sockjs.github.io/sockjs-protocol/sockjs-protocol-0.3.3.html#section-36)
  89. // #####################
  90. String socketUrl = ws_baseurl;
  91. socketUrl += random(0, 999);
  92. socketUrl += "/";
  93. socketUrl += random(0, 999999); // should be a random string, but this works (see )
  94. socketUrl += "/websocket";
  95. // connect to websocket
  96. webSocket.begin(ws_host, ws_port, socketUrl);
  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. }