Skip to content

Commit

Permalink
add ws path
Browse files Browse the repository at this point in the history
  • Loading branch information
tuanpmt committed Sep 13, 2017
1 parent 906ab6b commit 252c4ca
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void setup() {
mqtt.subscribe("/qos2", 2);
});

mqtt.begin("ws://test.mosquitto.org:8080");
mqtt.begin("ws://broker.mqttdashboard.com:8000/mqtt");
//mqtt.begin("ws://test.mosquitto.org:8080", {.lwtTopic = "hello", .lwtMsg = "offline", .lwtQos = 0, .lwtRetain = 0});
//mqtt.begin("ws://user:[email protected]:8080");
//mqtt.begin("ws://user:[email protected]:8080#clientId");
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ESP8266MQTTClient
version=1.0.1
version=1.0.3
author=Tuan PM
maintainer=Tuan PM
sentence=MQTT Client for ESP8266
Expand Down
3 changes: 1 addition & 2 deletions src/ESP8266MQTTClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ bool MQTTClient::connect(void)
return false;
}
_tcp->setNoDelay(true);
//if(!_tcp->connect(_host.c_str(), _port)) { //=>works
if(!_transportTraits->connect(_tcp.get(), _host.c_str(), _port)) { //=not works
if(!_transportTraits->connect(_tcp.get(), _host.c_str(), _port, _path.c_str())) {
LOG("[MQTT-Client] failed connect to %s:%u\n", _host.c_str(), _port);
return false;
}
Expand Down
20 changes: 13 additions & 7 deletions src/MQTTTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ std::unique_ptr<WiFiClient> MQTTTransportTraits::create()
return std::unique_ptr<WiFiClient>(new WiFiClientSecure());
return std::unique_ptr<WiFiClient>(new WiFiClient());
}
bool MQTTTransportTraits::connect(WiFiClient* client, const char* host, int port)
bool MQTTTransportTraits::connect(WiFiClient* client, const char* host, int port, const char *path)
{
if(_isSecure) {
WiFiClientSecure *client = (WiFiClientSecure*) client;
Expand Down Expand Up @@ -77,7 +77,7 @@ MQTTWSTraits::MQTTWSTraits(bool secure): _isSecure(secure)
}


bool MQTTWSTraits::connect(WiFiClient* client, const char* host, int port)
bool MQTTWSTraits::connect(WiFiClient* client, const char* host, int port, const char *path)
{
uint8_t randomKey[16] = { 0 }, timeout = 0;
int bite;
Expand All @@ -89,34 +89,40 @@ bool MQTTWSTraits::connect(WiFiClient* client, const char* host, int port)
}
_key = base64::encode(randomKey, 16);
LOG("Key: %s\r\n", _key.c_str());
String handshake = "GET / HTTP/1.1\r\n"
String handshake = "GET "+ String(path) +" HTTP/1.1\r\n"
"Connection: Upgrade\r\n"
"Upgrade: websocket\r\n"
"Host: " + String(host) + ":" + String(port) + "\r\n"
"Sec-WebSocket-Version: 13\r\n"
"Origin: file://\r\n"
"Sec-WebSocket-Protocol: mqttv3.1\r\n"
"User-Agent: ESP8266MQTTClient\r\n"
"Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n"
"Sec-WebSocket-Key: " + _key + "\r\n\r\n";
if(!client->connect(host, port))
if(!client->connect(host, port)) {
LOG("ERROR: Can't connect \r\n");
return false;
}
client->write(handshake.c_str(), handshake.length());

while(client->connected() && !client->available()) {
delay(100);
if(timeout++ > 10)
if(timeout++ > 10) {
LOG("ERROR Read timeout\r\n");
return false;
}
}
while((bite = client->read()) != -1) {

temp += (char)bite;

if((char)bite == '\n') {
if(!foundupgrade && temp.startsWith("Upgrade: websocket")) {
if(!foundupgrade && (temp.startsWith("Upgrade: websocket") || temp.startsWith("upgrade: websocket"))) {
foundupgrade = true;
} else if(temp.startsWith("Sec-WebSocket-Accept: ")) {
} else if(temp.startsWith("Sec-WebSocket-Accept: ") || temp.startsWith("sec-websocket-accept: ")) {
serverKey = temp.substring(22, temp.length() - 2); // Don't save last CR+LF
}
LOG("Data=%s", temp.c_str());
temp = "";
}

Expand Down
4 changes: 2 additions & 2 deletions src/MQTTTransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MQTTTransportTraits
MQTTTransportTraits();
MQTTTransportTraits(bool secure);
virtual std::unique_ptr<WiFiClient> create();
virtual bool connect(WiFiClient* client, const char* host, int port);
virtual bool connect(WiFiClient* client, const char* host, int port, const char *path);
virtual int write(WiFiClient* client, unsigned char *data, int size);
virtual int read(WiFiClient* client, unsigned char *data, int size);
protected:
Expand All @@ -33,7 +33,7 @@ class MQTTWSTraits : public MQTTTransportTraits
public:
MQTTWSTraits();
MQTTWSTraits(bool secure);
bool connect(WiFiClient* client, const char* host, int port) override;
bool connect(WiFiClient* client, const char* host, int port, const char *path) override;
int write(WiFiClient* client, unsigned char *data, int size) override;
int read(WiFiClient* client, unsigned char *data, int size) override;
protected:
Expand Down

0 comments on commit 252c4ca

Please sign in to comment.