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