Skip to content

Commit 9608c2d

Browse files
authored
Merge pull request #240 from mgbckr/master
Add example for plain STOMP connection
2 parents 1bd4638 + 4baf489 commit 9608c2d

File tree

2 files changed

+153
-4
lines changed

2 files changed

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

examples/WebSocketClientSockJsAndStomp/WebSocketClientSockJsAndStomp.ino renamed to examples/WebSocketClientStompOverSockJs/WebSocketClientStompOverSockJs.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2-
WebSocketClientSockJsAndStomp.ino
2+
WebSocketClientStompOverSockJs.ino
33
44
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).
5+
In this example, we connect to a Spring application (see https://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html).
66
77
Created on: 18.07.2017
88
Author: Martin Becker <mgbckr>, Contact: [email protected]
@@ -33,7 +33,7 @@ const int ws_port = 80;
3333
// base URL for SockJS (websocket) connection
3434
// The complete URL will look something like this(cf. http://sockjs.github.io/sockjs-protocol/sockjs-protocol-0.3.3.html#section-36):
3535
// ws://<ws_host>:<ws_port>/<ws_baseurl>/<3digits>/<randomstring>/websocket
36-
// For the default config of Spring's SockJS/STOMP support the default base URL is "/socketentry/".
36+
// For the default config of Spring's SockJS/STOMP support, the default base URL is "/socketentry/".
3737
const char* ws_baseurl = "/socketentry/"; // don't forget leading and trailing "/" !!!
3838

3939

@@ -58,7 +58,7 @@ void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
5858
case WStype_TEXT:
5959
{
6060
// #####################
61-
// handle STOMP protocol
61+
// handle SockJs+STOMP protocol
6262
// #####################
6363

6464
String text = (char*) payload;

0 commit comments

Comments
 (0)