8000 [SocketIO] add example for use of ACK / callback handling · pascalodek/arduinoWebSockets@0444a78 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0444a78

Browse files
committed
[SocketIO] add example for use of ACK / callback handling
1 parent 8cc20b6 commit 0444a78

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* WebSocketClientSocketIOack.ino
3+
*
4+
* Created on: 20.07.2019
5+
*
6+
*/
7+
8+
#include <Arduino.h>
9+
10+
#include <ESP8266WiFi.h>
11+
#include <ESP8266WiFiMulti.h>
12+
13+
#include <ArduinoJson.h>
14+
15+
#include <WebSocketsClient.h>
16+
#include <SocketIOclient.h>
17+
18+
#include <Hash.h>
19+
20+
ESP8266WiFiMulti WiFiMulti;
21+
SocketIOclient socketIO;
22+
23+
#define USE_SERIAL Serial
24+
25+
26+
void socketIOEvent(socketIOmessageType_t type, uint8_t * payload, size_t length) {
27+
switch(type) {
28+
case sIOtype_DISCONNECT:
29+
USE_SERIAL.printf("[IOc] Disconnected!\n");
30+
break;
31+
case sIOtype_CONNECT:
32+
USE_SERIAL.printf("[IOc] Connected to url: %s\n", payload);
33+
break;
34+
case sIOtype_EVENT:
35+
{
36+
char * sptr = NULL;
37+
int id = strtol((char *)payload, &sptr, 10);
38+
USE_SERIAL.printf("[IOc] get event: %s id: %d\n", payload, id);
39+
if(id) {
40+
payload = (uint8_t *)sptr;
41+
}
42+
DynamicJsonDocument doc(1024);
43+
DeserializationError error = deserializeJson(doc, payload, length);
44+
if(error) {
45+
USE_SERIAL.print(F("deserializeJson() failed: "));
46+
USE_SERIAL.println(error.c_str());
47+
return;
48+
}
49+
50+
String eventName = doc[0];
51+
USE_SERIAL.printf("[IOc] event name: %s\n", eventName.c_str());
52+
53+
// Message Includes a ID for a ACK (callback)
54+
if(id) {
55+
// creat JSON message for Socket.IO (ack)
56+
DynamicJsonDocument docOut(1024);
57+
JsonArray array = docOut.to<JsonArray>();
58+
59+
// add payload (parameters) for the ack (callback function)
60+
JsonObject param1 = array.createNestedObject();
61+
param1["now"] = millis();
62+
63+
// JSON to String (serializion)
64+
String output;
65+
output += id;
66+
serializeJson(docOut, output);
67+
68+
// Send event
69+
socketIO.send(sIOtype_ACK, output);
70+
}
71+
}
72+
break;
73+
case sIOtype_ACK:
74+
USE_SERIAL.printf("[IOc] get ack: %u\n", length);
75+
hexdump(payload, length);
76+
break;
77+
case sIOtype_ERROR:
78+
USE_SERIAL.printf("[IOc] get error: %u\n", length);
79+
hexdump(payload, length);
80+
break;
81+
case sIOtype_BINARY_EVENT:
82+
USE_SERIAL.printf("[IOc] get binary: %u\n", length);
83+
hexdump(payload, length);
84+
break;
85+
case sIOtype_BINARY_ACK:
86+
USE_SERIAL.printf("[IOc] get binary ack: %u\n", length);
87+
hexdump(payload, length);
88+
break;
89+
}
90+
}
91+
92+
void setup() {
93+
//USE_SERIAL.begin(921600);
94+
USE_SERIAL.begin(115200);
95+
96+
//Serial.setDebugOutput(true);
97+
USE_SERIAL.setDebugOutput(true);
98+
99+
USE_SERIAL.println();
100+
USE_SERIAL.println();
101+
USE_SERIAL.println();
102+
103+
for(uint8_t t = 4; t > 0; t--) {
104+
USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
105+
USE_SERIAL.flush();
106+
delay(1000);
107+
}
108+
109+
// disable AP
110+
if(WiFi.getMode() & WIFI_AP) {
111+
WiFi.softAPdisconnect(true);
112+
}
113+
114+
WiFiMulti.addAP("SSID", "passpasspass");
115+
116+
//WiFi.disconnect();
117+
while(WiFiMulti.run() != WL_CONNECTED) {
118+
delay(100);
119+
}
120+
121+
String ip = WiFi.localIP().toString();
122+
USE_SERIAL.printf("[SETUP] WiFi Connected %s\n", ip.c_str());
123+
124+
// server address, port and URL
125+
socketIO.begin("10.11.100.100", 8880);
126+
127+
// event handler
128+
socketIO.onEvent(socketIOEvent);
129+
}
130+
131+
unsigned long messageTimestamp = 0;
132+
void loop() {
133+
socketIO.loop();
134+
135+
uint64_t now = millis();
136+
137+
if(now - messageTimestamp > 2000) {
138+
messageTimestamp = now;
139+
140+
// creat JSON message for Socket.IO (event)
141+
DynamicJsonDocument doc(1024);
142+
JsonArray array = doc.to<JsonArray>();
143+
144+
// add evnet name
145+
// Hint: socket.on('event_name', ....
146+
array.add("event_name");
147+
148+
// add payload (parameters) for the event
149+
JsonObject param1 = array.createNestedObject();
150+
param1["now"] = now;
151+
152+
// JSON to String (serializion)
153+
String output;
154+
serializeJson(doc, output);
155+
156+
// Send event
157+
socketIO.sendEVENT(output);
158+
159+
// Print JSON for debugging
160+
USE_SERIAL.println(output);
161+
}
162+
}

0 commit comments

Comments
 (0)
0