Skip to content

Commit

Permalink
Merge pull request #3 from yoav97h/master
Browse files Browse the repository at this point in the history
Disconnected event, SSL fingerprint support
  • Loading branch information
timum-viw authored Feb 13, 2017
2 parents 617c82a + 5e1391c commit b376ad3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ open connection to socket.io server.
socket.begin("my.socket-io.server", 443, "/socket.io/?transport=websocket");
```

### SocketIoClient::beginSSL(host[, port, path, fingerprint])
open SSL connection to socket.io server.
##### Parameter
```host``` url to socket.io server
```port``` port to connect on. Defaults to 80 or 443 (SSL)
```path``` path to connect to on server. Defaults to "/socket.io-client/?transport=websocket"
```fingerprint``` the SSL fingerprint. Defaults to ""
##### Example
```c
socket.begin("my.socket-io.server", 443, "/socket.io/?transport=websocket", "26 96 1C 2A 51 07 FD 15 80 96 93 AE F7 32 CE B9 0D 01 55 C4");
```

### SocketIoClient::on(event, callback)
binds a function to an event.
##### Parameter
Expand All @@ -46,6 +58,9 @@ void event(const char * payload, size_t length) {
}
socket.on("event", event);
```
##### Supported default events:
* `connect` - when user is connected to server
* `disconnected` - when user is disconnected from the server
### SocketIoClient::emit(event, payload)
emits an event to the server.
Expand Down
16 changes: 11 additions & 5 deletions SocketIoClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void SocketIoClient::webSocketEvent(WStype_t type, uint8_t * payload, size_t len
switch(type) {
case WStype_DISCONNECTED:
SOCKETIOCLIENT_DEBUG("[SIoC] Disconnected!\n");
trigger("disconnected", NULL, 0);
break;
case WStype_CONNECTED:
SOCKETIOCLIENT_DEBUG("[SIoC] Connected to url: %s\n", payload);
Expand All @@ -41,13 +42,18 @@ void SocketIoClient::webSocketEvent(WStype_t type, uint8_t * payload, size_t len
}
}

void SocketIoClient::beginSSL(const char* host, const int port, const char* url, const char* fingerprint) {
_webSocket.beginSSL(host, port, url, fingerprint);
initialize();
}

void SocketIoClient::begin(const char* host, const int port, const char* url) {
#ifdef SOCKETIOCLIENT_USE_SSL
_webSocket.beginSSL(host, port, url);
#else
_webSocket.begin(host, port, url);
#endif
_webSocket.onEvent(std::bind(&SocketIoClient::webSocketEvent, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
initialize();
}

void SocketIoClient::initialize() {
_webSocket.onEvent(std::bind(&SocketIoClient::webSocketEvent, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
_lastPing = millis();
}

Expand Down
4 changes: 4 additions & 0 deletions SocketIoClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#define DEFAULT_PORT 80
#endif
#define DEFAULT_URL "/socket.io/?transport=websocket"
#define DEFAULT_FINGERPRINT ""


class SocketIoClient {
private:
Expand All @@ -28,7 +30,9 @@ class SocketIoClient {

void trigger(const char* event, const char * payload, size_t length);
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length);
void initialize();
public:
void beginSSL(const char* host, const int port = DEFAULT_PORT, const char* url = DEFAULT_URL, const char* fingerprint = DEFAULT_FINGERPRINT);
void begin(const char* host, const int port = DEFAULT_PORT, const char* url = DEFAULT_URL);
void loop();
void on(const char* event, std::function<void (const char * payload, size_t length)>);
Expand Down

0 comments on commit b376ad3

Please sign in to comment.