1
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: [email protected]
9
- */
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: [email protected]
9
+ */
10
10
11
11
// PRE
12
12
@@ -28,7 +28,7 @@ const char* ws_host = "the.host.net";
28
28
const int ws_port = 80 ;
29
29
30
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):
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
32
// ws://<ws_host>:<ws_port>/<ws_baseurl>/<3digits>/<randomstring>/websocket
33
33
// For the default config of Spring's SockJS/STOMP support the default base URL is "/socketentry/".
34
34
const char * ws_baseurl = " /socketentry/" ; // don't forget leading and trailing "/" !!!
@@ -43,7 +43,7 @@ WebSocketsClient webSocket;
43
43
44
44
void webSocketEvent (WStype_t type, uint8_t * payload, size_t length) {
45
45
46
- switch (type) {
46
+ switch (type) {
47
47
case WStype_DISCONNECTED:
48
48
USE_SERIAL.printf (" [WSc] Disconnected!\n " );
49
49
break ;
@@ -52,43 +52,43 @@ void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
52
52
USE_SERIAL.printf (" [WSc] Connected to url: %s\n " , payload);
53
53
}
54
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
- }
55
+ case WStype_TEXT:
56
+ {
57
+ // #####################
58
+ // handle STOMP protocol
59
+ // #####################
89
60
90
- break ;
91
- }
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
92
case WStype_BIN:
93
93
USE_SERIAL.printf (" [WSc] get binary length: %u\n " , length);
94
94
hexdump (payload, length);
@@ -107,7 +107,7 @@ void setup() {
107
107
// USE_SERIAL.begin(921600);
108
108
USE_SERIAL.begin (115200 );
109
109
110
- // USE_SERIAL.setDebugOutput(true);
110
+ // USE_SERIAL.setDebugOutput(true);
111
111
112
112
USE_SERIAL.println ();
113
113
@@ -117,28 +117,28 @@ void setup() {
117
117
USE_SERIAL.print (" Logging into WLAN: " ); Serial.print (wlan_ssid); Serial.print (" ..." );
118
118
WiFi.mode (WIFI_STA);
119
119
WiFi.begin (wlan_ssid, wlan_password);
120
-
120
+
121
121
while (WiFi.status () != WL_CONNECTED) {
122
- delay (500 );
123
- USE_SERIAL.print (" ." );
122
+ delay (500 );
123
+ USE_SERIAL.print (" ." );
124
124
}
125
125
USE_SERIAL.println (" success." );
126
126
USE_SERIAL.print (" IP: " ); USE_SERIAL.println (WiFi.localIP ());
127
127
128
-
128
+
129
129
// #####################
130
130
// create socket url according to SockJS protocol (cf. http://sockjs.github.io/sockjs-protocol/sockjs-protocol-0.3.3.html#section-36)
131
131
// #####################
132
132
String socketUrl = ws_baseurl;
133
- socketUrl += random (0 ,999 );
133
+ socketUrl += random (0 , 999 );
134
134
socketUrl += " /" ;
135
- socketUrl += random (0 ,999999 ); // should be a random string, but this works (see )
135
+ socketUrl += random (0 , 999999 ); // should be a random string, but this works (see )
136
136
socketUrl += " /websocket" ;
137
137
138
138
// connect to websocket
139
139
webSocket.begin (ws_host, ws_port, socketUrl);
140
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
141
+ // webSocket.setExtraHeaders("foo: I am so funny\r\nbar: not"); // some headers, in case you feel funny
142
142
webSocket.onEvent (webSocketEvent);
143
143
}
144
144
0 commit comments