Skip to content

Commit d2043bf

Browse files
committed
start work for socket.IO
basic Engine.IO implementation
1 parent 7ddcdc2 commit d2043bf

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed

src/SocketIOclient.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* SocketIOclient.cpp
3+
*
4+
* Created on: May 12, 2018
5+
* Author: links
6+
*/
7+
8+
#include "WebSockets.h"
9+
#include "WebSocketsClient.h"
10+
#include "SocketIOclient.h"
11+
12+
SocketIOclient::SocketIOclient() {
13+
14+
}
15+
16+
SocketIOclient::~SocketIOclient() {
17+
18+
}
19+
20+
void SocketIOclient::begin(const char *host, uint16_t port, const char * url, const char * protocol) {
21+
WebSocketsClient::beginSocketIO(host, port, url, protocol);
22+
}
23+
24+
void SocketIOclient::begin(String host, uint16_t port, String url, String protocol) {
25+
WebSocketsClient::beginSocketIO(host, port, url, protocol);
26+
}
27+
28+
void SocketIOclient::loop(void) {
29+
WebSocketsClient::loop();
30+
unsigned long t = millis();
31+
if((t - _lastConnectionFail) > EIO_HEARTBEAT_INTERVAL) {
32+
_lastConnectionFail = t;
33+
sendTXT(eIOtype_PING);
34+
}
35+
}
36+
37+
void SocketIOclient::runCbEvent(WStype_t type, uint8_t * payload, size_t length) {
38+
switch(type) {
39+
case WStype_DISCONNECTED:
40+
DEBUG_WEBSOCKETS("[wsIOc] Disconnected!\n");
41+
break;
42+
case WStype_CONNECTED: {
43+
DEBUG_WEBSOCKETS("[wsIOc] Connected to url: %s\n", payload);
44+
// send message to server when Connected
45+
// socket.io upgrade confirmation message (required)
46+
sendTXT(eIOtype_UPGRADE);
47+
}
48+
break;
49+
case WStype_TEXT: {
50+
51+
if(length < 1) {
52+
break;
53+
}
54+
55+
engineIOmessageType_t eType = (engineIOmessageType_t) payload[0];
56+
switch(eType) {
57+
case eIOtype_PING:
58+
payload[0] = eIOtype_PONG;
59+
DEBUG_WEBSOCKETS("[wsIOc] get ping send pong (%s)\n", payload);
60+
sendTXT(payload, length);
61+
break;
62+
case eIOtype_PONG:
63+
DEBUG_WEBSOCKETS("[wsIOc] get pong\n");
64+
break;
65+
case eIOtype_OPEN:
66+
case eIOtype_CLOSE:
67+
case eIOtype_MESSAGE:
68+
case eIOtype_UPGRADE:
69+
case eIOtype_NOOP:
70+
default:
71+
DEBUG_WEBSOCKETS("[wsIOc] Engine.IO Message Type %c (%02X) is not implemented\n", eType, eType);
72+
DEBUG_WEBSOCKETS("[wsIOc] get text: %s\n", payload);
73+
break;
74+
}
75+
76+
// send message to server
77+
// webSocket.sendTXT("message here");
78+
}
79+
break;
80+
case WStype_BIN:
81+
DEBUG_WEBSOCKETS("[wsIOc] get binary length: %u\n", length);
82+
// hexdump(payload, length);
83+
84+
// send data to server
85+
// webSocket.sendBIN(payload, length);
86+
break;
87+
}
88+
}

src/SocketIOclient.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* SocketIOclient.h
3+
*
4+
* Created on: May 12, 2018
5+
* Author: links
6+
*/
7+
8+
#ifndef SOCKETIOCLIENT_H_
9+
#define SOCKETIOCLIENT_H_
10+
11+
#include "WebSockets.h"
12+
13+
#define EIO_HEARTBEAT_INTERVAL 10000
14+
15+
typedef enum {
16+
eIOtype_OPEN = '0', ///< Sent from the server when a new transport is opened (recheck)
17+
eIOtype_CLOSE = '1', ///< Request the close of this transport but does not shutdown the connection itself.
18+
eIOtype_PING = '2', ///< Sent by the client. Server should answer with a pong packet containing the same data
19+
eIOtype_PONG = '3', ///< Sent by the server to respond to ping packets.
20+
eIOtype_MESSAGE = '4', ///< actual message, client and server should call their callbacks with the data
21+
eIOtype_UPGRADE = '5', ///< Before engine.io switches a transport, it tests, if server and client can communicate over this transport. If this test succeed, the client sends an upgrade packets which requests the server to flush its cache on the old transport and switch to the new transport.
22+
eIOtype_NOOP = '6', ///< A noop packet. Used primarily to force a poll cycle when an incoming websocket connection is received.
23+
} engineIOmessageType_t;
24+
25+
26+
typedef enum {
27+
sIOtype_CONNECT = '0',
28+
sIOtype_DISCONNECT = '1',
29+
sIOtype_EVENT = '2',
30+
sIOtype_ACK = '3',
31+
sIOtype_ERROR = '4',
32+
sIOtype_BINARY_EVENT = '5',
33+
sIOtype_BINARY_ACK = '6',
34+
} socketIOmessageType_t;
35+
36+
class SocketIOclient: private WebSocketsClient {
37+
38+
public:
39+
#ifdef __AVR__
40+
typedef void (*SocketIOclientEvent)(WStype_t type, uint8_t * payload, size_t length);
41+
#else
42+
typedef std::function<void (WStype_t type, uint8_t * payload, size_t length)> SocketIOclientEvent;
43+
#endif
44+
45+
SocketIOclient(void);
46+
virtual ~SocketIOclient(void);
47+
48+
void begin(const char *host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
49+
void begin(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
50+
51+
52+
void loop(void);
53+
private:
54+
void runCbEvent(WStype_t type, uint8_t * payload, size_t length);
55+
uint64_t _lastHeartbeat = 0;
56+
};
57+
58+
#endif /* SOCKETIOCLIENT_H_ */

src/WebSocketsClient.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ bool WebSocketsClient::sendTXT(String & payload) {
215215
return sendTXT((uint8_t *) payload.c_str(), payload.length());
216216
}
217217

218+
bool WebSocketsClient::sendTXT(char payload) {
219+
return sendTXT((uint8_t *) &payload, 1);
220+
}
221+
218222
/**
219223
* send binary data to client
220224
* @param num uint8_t client id

src/WebSocketsClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class WebSocketsClient: private WebSockets {
7070
bool sendTXT(char * payload, size_t length = 0, bool headerToPayload = false);
7171
bool sendTXT(const char * payload, size_t length = 0);
7272
bool sendTXT(String & payload);
73+
bool sendTXT(char payload);
7374

7475
bool sendBIN(uint8_t * payload, size_t length, bool headerToPayload = false);
7576
bool sendBIN(const uint8_t * payload, size_t length);

0 commit comments

Comments
 (0)