|
| 1 | +/* |
| 2 | + WebSocketClientSockJsAndStomp.ino |
| 3 | +
|
| 4 | + Example for connecting and maintining a connection with a SockJS+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: 18.07.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 <ESP8266WiFi.h> |
| 19 | +#include <WebSocketsClient.h> |
| 20 | + |
| 21 | + |
| 22 | +// SETTINGS |
| 23 | + |
| 24 | +const char* wlan_ssid = "yourssid"; |
| 25 | +const char* wlan_password = "somepassword"; |
| 26 | + |
| 27 | +const char* ws_host = "the.host.net"; |
| 28 | +const int ws_port = 80; |
| 29 | + |
| 30 | +// base URL for SockJS (websocket) connection |
| 31 | +// The complete URL will look something like this(cf. http://sockjs.github.io/sockjs-protocol/sockjs-protocol-0.3.3.html#section-36): |
| 32 | +// ws://<ws_host>:<ws_port>/<ws_baseurl>/<3digits>/<randomstring>/websocket |
| 33 | +// For the default config of Spring's SockJS/STOMP support the default base URL is "/socketentry/". |
| 34 | +const char* ws_baseurl = "/socketentry/"; // don't forget leading and trailing "/" !!! |
| 35 | + |
| 36 | + |
| 37 | +// VARIABLES |
| 38 | + |
| 39 | +WebSocketsClient webSocket; |
| 40 | + |
| 41 | + |
| 42 | +// FUNCTIONS |
| 43 | + |
| 44 | +void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { |
| 45 | + |
| 46 | + switch (type) { |
| 47 | + case WStype_DISCONNECTED: |
| 48 | + USE_SERIAL.printf("[WSc] Disconnected!\n"); |
| 49 | + break; |
| 50 | + case WStype_CONNECTED: |
| 51 | + { |
| 52 | + USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload); |
| 53 | + } |
| 54 | + break; |
| 55 | + case WStype_TEXT: |
| 56 | + { |
| 57 | + // ##################### |
| 58 | + // handle STOMP protocol |
| 59 | + // ##################### |
| 60 | + |
| 61 | + String text = (char*) payload; |
| 62 | + |
| 63 | + USE_SERIAL.printf("[WSc] get text: %s\n", payload); |
| 64 | + |
| 65 | + if (payload[0] == 'h') { |
| 66 | + |
| 67 | + USE_SERIAL.println("Heartbeat!"); |
| 68 | + |
| 69 | + } else if (payload[0] == 'o') { |
| 70 | + |
| 71 | + // on open connection |
| 72 | + char *msg = "[\"CONNECT\\naccept-version:1.1,1.0\\nheart-beat:10000,10000\\n\\n\\u0000\"]"; |
| 73 | + webSocket.sendTXT(msg); |
| 74 | + |
| 75 | + } else if (text.startsWith("a[\"CONNECTED")) { |
| 76 | + |
| 77 | + // subscribe to some channels |
| 78 | + |
| 79 | + char *msg = "[\"SUBSCRIBE\\nid:sub-0\\ndestination:/user/queue/messages\\n\\n\\u0000\"]"; |
| 80 | + webSocket.sendTXT(msg); |
| 81 | + delay(1000); |
| 82 | + |
| 83 | + // and send a message |
| 84 | + |
| 85 | + msg = "[\"SEND\\ndestination:/app/message\\n\\n{\\\"user\\\":\\\"esp\\\",\\\"message\\\":\\\"Hello!\\\"}\\u0000\"]"; |
| 86 | + webSocket.sendTXT(msg); |
| 87 | + delay(1000); |
| 88 | + } |
| 89 | + |
| 90 | + break; |
| 91 | + } |
| 92 | + case WStype_BIN: |
| 93 | + USE_SERIAL.printf("[WSc] get binary length: %u\n", length); |
| 94 | + hexdump(payload, length); |
| 95 | + |
| 96 | + // send data to server |
| 97 | + // webSocket.sendBIN(payload, length); |
| 98 | + break; |
| 99 | + } |
| 100 | + |
| 101 | +} |
| 102 | + |
| 103 | +void setup() { |
| 104 | + |
| 105 | + // setup serial |
| 106 | + |
| 107 | + // USE_SERIAL.begin(921600); |
| 108 | + USE_SERIAL.begin(115200); |
| 109 | + |
| 110 | + // USE_SERIAL.setDebugOutput(true); |
| 111 | + |
| 112 | + USE_SERIAL.println(); |
| 113 | + |
| 114 | + |
| 115 | + // connect to WiFi |
| 116 | + |
| 117 | + USE_SERIAL.print("Logging into WLAN: "); Serial.print(wlan_ssid); Serial.print(" ..."); |
| 118 | + WiFi.mode(WIFI_STA); |
| 119 | + WiFi.begin(wlan_ssid, wlan_password); |
| 120 | + |
| 121 | + while (WiFi.status() != WL_CONNECTED) { |
| 122 | + delay(500); |
| 123 | + USE_SERIAL.print("."); |
| 124 | + } |
| 125 | + USE_SERIAL.println(" success."); |
| 126 | + USE_SERIAL.print("IP: "); USE_SERIAL.println(WiFi.localIP()); |
| 127 | + |
| 128 | + |
| 129 | + // ##################### |
| 130 | + // create socket url according to SockJS protocol (cf. http://sockjs.github.io/sockjs-protocol/sockjs-protocol-0.3.3.html#section-36) |
| 131 | + // ##################### |
| 132 | + String socketUrl = ws_baseurl; |
| 133 | + socketUrl += random(0, 999); |
| 134 | + socketUrl += "/"; |
| 135 | + socketUrl += random(0, 999999); // should be a random string, but this works (see ) |
| 136 | + socketUrl += "/websocket"; |
| 137 | + |
| 138 | + // connect to websocket |
| 139 | + webSocket.begin(ws_host, ws_port, socketUrl); |
| 140 | + webSocket.setExtraHeaders(); // remove "Origin: file://" header because it breaks the connection with Spring's default websocket config |
| 141 | + // webSocket.setExtraHeaders("foo: I am so funny\r\nbar: not"); // some headers, in case you feel funny |
| 142 | + webSocket.onEvent(webSocketEvent); |
| 143 | +} |
| 144 | + |
| 145 | +void loop() { |
| 146 | + webSocket.loop(); |
| 147 | +} |
0 commit comments