Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Socket io with namespace #730

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 58 additions & 2 deletions src/SocketIOclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ void SocketIOclient::handleCbEvent(WStype_t type, uint8_t * payload, size_t leng
// Engine.io upgrade confirmation message (required)
WebSocketsClient::sendTXT("2probe");
WebSocketsClient::sendTXT(eIOtype_UPGRADE);
setNamespace();
runIOCbEvent(sIOtype_CONNECT, payload, length);
} break;
case WStype_TEXT: {
Expand All @@ -214,9 +215,21 @@ void SocketIOclient::handleCbEvent(WStype_t type, uint8_t * payload, size_t leng
if(length < 2) {
break;
}
int i = 2;
if(payload[2] == '/') {
for(i = 2; i < length; i++) {
if(payload[i] == ',')
break;
}
i++;
}

uint8_t * data = &payload[i];
size_t lData = length - i;
socketIOmessageType_t ioType = (socketIOmessageType_t)payload[1];
uint8_t * data = &payload[2];
size_t lData = length - 2;
// uint8_t * data = &payload[2];
// size_t lData = length - 2;

switch(ioType) {
case sIOtype_EVENT:
DEBUG_WEBSOCKETS("[wsIOc] get event (%d): %s\n", lData, data);
Expand Down Expand Up @@ -258,3 +271,46 @@ void SocketIOclient::handleCbEvent(WStype_t type, uint8_t * payload, size_t leng
break;
}
}

// Socket io with namespace
void SocketIOclient::connect(String host, uint16_t port, String name_space, String url, String protocol) {
WebSocketsClient::beginSocketIO(host, port, url, protocol);
WebSocketsClient::enableHeartbeat(60 * 1000, 90 * 1000, 5);
nsp = name_space;
initClient();
}
bool SocketIOclient::setNamespace() {
String msg = "40/" + nsp;
uint8_t * payload = (uint8_t *)msg.c_str();
size_t length = msg.length();

bool ret = false;
if(clientIsConnected(&_client) && _client.status == WSC_CONNECTED) {
ret = WebSocketsClient::sendFrameHeader(&_client, WSop_text, length, true);
if(ret && payload && length > 0) {
ret = WebSocketsClient::write(&_client, payload, length);
}
return ret;
}
return false;
}
bool SocketIOclient::emit(String event_name, String _payload) {
String msg = "42/" + nsp + ",[\"" + event_name + "\"" + "," + _payload + "]";
Serial.println(msg);

uint8_t * payload = (uint8_t *)msg.c_str();

size_t length = msg.length();

bool ret = false;
if(clientIsConnected(&_client) && _client.status == WSC_CONNECTED) {
ret = WebSocketsClient::sendFrameHeader(&_client, WSop_text, length, true);

// send data
if(ret && payload && length > 0) {
ret = WebSocketsClient::write(&_client, payload, length);
}
return ret;
}
return false;
}
9 changes: 9 additions & 0 deletions src/SocketIOclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ class SocketIOclient : protected WebSocketsClient {
handleCbEvent(type, payload, length);
}
void handleCbEvent(WStype_t type, uint8_t * payload, size_t length);

// Socket io with namespace
public:
void connect(String host, uint16_t port, String name_space = "", String url = "/socket.io/?EIO=4", String protocol = "arduino");
bool emit(String event_name, String payload);

protected:
String nsp;
bool setNamespace();
};

#endif /* SOCKETIOCLIENT_H_ */