mirror of
https://github.com/Links2004/arduinoWebSockets.git
synced 2025-07-15 16:30:31 +02:00
start work for socket.IO
basic Engine.IO implementation
This commit is contained in:
88
src/SocketIOclient.cpp
Normal file
88
src/SocketIOclient.cpp
Normal file
@ -0,0 +1,88 @@
|
||||
/*
|
||||
* SocketIOclient.cpp
|
||||
*
|
||||
* Created on: May 12, 2018
|
||||
* Author: links
|
||||
*/
|
||||
|
||||
#include "WebSockets.h"
|
||||
#include "WebSocketsClient.h"
|
||||
#include "SocketIOclient.h"
|
||||
|
||||
SocketIOclient::SocketIOclient() {
|
||||
|
||||
}
|
||||
|
||||
SocketIOclient::~SocketIOclient() {
|
||||
|
||||
}
|
||||
|
||||
void SocketIOclient::begin(const char *host, uint16_t port, const char * url, const char * protocol) {
|
||||
WebSocketsClient::beginSocketIO(host, port, url, protocol);
|
||||
}
|
||||
|
||||
void SocketIOclient::begin(String host, uint16_t port, String url, String protocol) {
|
||||
WebSocketsClient::beginSocketIO(host, port, url, protocol);
|
||||
}
|
||||
|
||||
void SocketIOclient::loop(void) {
|
||||
WebSocketsClient::loop();
|
||||
unsigned long t = millis();
|
||||
if((t - _lastConnectionFail) > EIO_HEARTBEAT_INTERVAL) {
|
||||
_lastConnectionFail = t;
|
||||
sendTXT(eIOtype_PING);
|
||||
}
|
||||
}
|
||||
|
||||
void SocketIOclient::runCbEvent(WStype_t type, uint8_t * payload, size_t length) {
|
||||
switch(type) {
|
||||
case WStype_DISCONNECTED:
|
||||
DEBUG_WEBSOCKETS("[wsIOc] Disconnected!\n");
|
||||
break;
|
||||
case WStype_CONNECTED: {
|
||||
DEBUG_WEBSOCKETS("[wsIOc] Connected to url: %s\n", payload);
|
||||
// send message to server when Connected
|
||||
// socket.io upgrade confirmation message (required)
|
||||
sendTXT(eIOtype_UPGRADE);
|
||||
}
|
||||
break;
|
||||
case WStype_TEXT: {
|
||||
|
||||
if(length < 1) {
|
||||
break;
|
||||
}
|
||||
|
||||
engineIOmessageType_t eType = (engineIOmessageType_t) payload[0];
|
||||
switch(eType) {
|
||||
case eIOtype_PING:
|
||||
payload[0] = eIOtype_PONG;
|
||||
DEBUG_WEBSOCKETS("[wsIOc] get ping send pong (%s)\n", payload);
|
||||
sendTXT(payload, length);
|
||||
break;
|
||||
case eIOtype_PONG:
|
||||
DEBUG_WEBSOCKETS("[wsIOc] get pong\n");
|
||||
break;
|
||||
case eIOtype_OPEN:
|
||||
case eIOtype_CLOSE:
|
||||
case eIOtype_MESSAGE:
|
||||
case eIOtype_UPGRADE:
|
||||
case eIOtype_NOOP:
|
||||
default:
|
||||
DEBUG_WEBSOCKETS("[wsIOc] Engine.IO Message Type %c (%02X) is not implemented\n", eType, eType);
|
||||
DEBUG_WEBSOCKETS("[wsIOc] get text: %s\n", payload);
|
||||
break;
|
||||
}
|
||||
|
||||
// send message to server
|
||||
// webSocket.sendTXT("message here");
|
||||
}
|
||||
break;
|
||||
case WStype_BIN:
|
||||
DEBUG_WEBSOCKETS("[wsIOc] get binary length: %u\n", length);
|
||||
// hexdump(payload, length);
|
||||
|
||||
// send data to server
|
||||
// webSocket.sendBIN(payload, length);
|
||||
break;
|
||||
}
|
||||
}
|
58
src/SocketIOclient.h
Normal file
58
src/SocketIOclient.h
Normal file
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* SocketIOclient.h
|
||||
*
|
||||
* Created on: May 12, 2018
|
||||
* Author: links
|
||||
*/
|
||||
|
||||
#ifndef SOCKETIOCLIENT_H_
|
||||
#define SOCKETIOCLIENT_H_
|
||||
|
||||
#include "WebSockets.h"
|
||||
|
||||
#define EIO_HEARTBEAT_INTERVAL 10000
|
||||
|
||||
typedef enum {
|
||||
eIOtype_OPEN = '0', ///< Sent from the server when a new transport is opened (recheck)
|
||||
eIOtype_CLOSE = '1', ///< Request the close of this transport but does not shutdown the connection itself.
|
||||
eIOtype_PING = '2', ///< Sent by the client. Server should answer with a pong packet containing the same data
|
||||
eIOtype_PONG = '3', ///< Sent by the server to respond to ping packets.
|
||||
eIOtype_MESSAGE = '4', ///< actual message, client and server should call their callbacks with the data
|
||||
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.
|
||||
eIOtype_NOOP = '6', ///< A noop packet. Used primarily to force a poll cycle when an incoming websocket connection is received.
|
||||
} engineIOmessageType_t;
|
||||
|
||||
|
||||
typedef enum {
|
||||
sIOtype_CONNECT = '0',
|
||||
sIOtype_DISCONNECT = '1',
|
||||
sIOtype_EVENT = '2',
|
||||
sIOtype_ACK = '3',
|
||||
sIOtype_ERROR = '4',
|
||||
sIOtype_BINARY_EVENT = '5',
|
||||
sIOtype_BINARY_ACK = '6',
|
||||
} socketIOmessageType_t;
|
||||
|
||||
class SocketIOclient: private WebSocketsClient {
|
||||
|
||||
public:
|
||||
#ifdef __AVR__
|
||||
typedef void (*SocketIOclientEvent)(WStype_t type, uint8_t * payload, size_t length);
|
||||
#else
|
||||
typedef std::function<void (WStype_t type, uint8_t * payload, size_t length)> SocketIOclientEvent;
|
||||
#endif
|
||||
|
||||
SocketIOclient(void);
|
||||
virtual ~SocketIOclient(void);
|
||||
|
||||
void begin(const char *host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
|
||||
void begin(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
|
||||
|
||||
|
||||
void loop(void);
|
||||
private:
|
||||
void runCbEvent(WStype_t type, uint8_t * payload, size_t length);
|
||||
uint64_t _lastHeartbeat = 0;
|
||||
};
|
||||
|
||||
#endif /* SOCKETIOCLIENT_H_ */
|
@ -215,6 +215,10 @@ bool WebSocketsClient::sendTXT(String & payload) {
|
||||
return sendTXT((uint8_t *) payload.c_str(), payload.length());
|
||||
}
|
||||
|
||||
bool WebSocketsClient::sendTXT(char payload) {
|
||||
return sendTXT((uint8_t *) &payload, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* send binary data to client
|
||||
* @param num uint8_t client id
|
||||
|
@ -70,6 +70,7 @@ class WebSocketsClient: private WebSockets {
|
||||
bool sendTXT(char * payload, size_t length = 0, bool headerToPayload = false);
|
||||
bool sendTXT(const char * payload, size_t length = 0);
|
||||
bool sendTXT(String & payload);
|
||||
bool sendTXT(char payload);
|
||||
|
||||
bool sendBIN(uint8_t * payload, size_t length, bool headerToPayload = false);
|
||||
bool sendBIN(const uint8_t * payload, size_t length);
|
||||
|
Reference in New Issue
Block a user