8000 Merge pull request #216 from mgbckr/master · prodec/arduinoWebSockets@adb255b · GitHub
[go: up one dir, main page]

Skip to content

Commit adb255b

Browse files
authored
Merge pull request Links2004#216 from mgbckr/master
Extra headers (including removal of "Origin") and SockJS+STOMP example
2 parents a4533a0 + d0ab6c4 commit adb255b

File tree

4 files changed

+167
-2
lines changed

4 files changed

+167
-2
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
}

src/WebSockets.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ typedef struct {
226226
String base64Authorization; ///< Base64 encoded Auth request
227227
String plainAuthorization; ///< Base64 encoded Auth request
228228

229+
String extraHeaders;
230+
229231
bool cHttpHeadersValid; ///< non-websocket http header validity indicator
230232
size_t cMandatoryHeadersCount; ///< non-websocket mandatory http headers present count
231233

src/WebSocketsClient.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
WebSocketsClient::WebSocketsClient() {
3030
_cbEvent = NULL;
3131
_client.num = 0;
32+
_client.extraHeaders = WEBSOCKETS_STRING("Origin: file://");
3233
}
3334

3435
WebSocketsClient::~WebSocketsClient() {
@@ -274,6 +275,15 @@ void WebSocketsClient::setAuthorization(const char * auth) {
274275
}
275276
}
276277

278+
/**
279+
* set extra headers for the http request;
280+
* separate headers by "\r\n"
281+
* @param extraHeaders const char * extraHeaders
282+
*/
283+
void WebSocketsClient::setExtraHeaders(const char * extraHeaders) {
284+
_client.extraHeaders = extraHeaders;
285+
}
286+
277287
//#################################################################################
278288
//#################################################################################
279289
//#################################################################################
@@ -479,8 +489,12 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
479489
handshake += WEBSOCKETS_STRING("Connection: keep-alive\r\n");
480490
}
481491

482-
handshake += WEBSOCKETS_STRING("Origin: file://\r\n"
483-
"User-Agent: arduino-WebSocket-Client\r\n");
492+
// add extra headers; by default this includes "Origin: file://"
493+
if (client->extraHeaders) {
494+
handshake += client->extraHeaders + NEW_LINE;
495+
}
496+
497+
handshake += WEBSOCKETS_STRING("User-Agent: arduino-WebSocket-Client\r\n");
484498

485499
if(client->base64Authorization.length() > 0) {
486500
handshake += WEBSOCKETS_STRING("Authorization: Basic ");

src/WebSocketsClient.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ class WebSocketsClient: private WebSockets {
8080

8181
void setAuthorization(const char * user, const char * password);
8282
void setAuthorization(const char * auth);
83+
84+
void setExtraHeaders(const char * extraHeaders = NULL);
8385

8486
protected:
8587
String _host;

0 commit comments

Comments
 (0)
0