fork of WebSocketClient with some fixes

Fork of WebSocketClient by mbed_example

Files at this revision

API Documentation at this revision

Comitter:
arminfelder
Date:
Sun Jan 28 14:45:05 2018 +0000
Parent:
9:efa2c147bee1
Commit message:
send handshake at once, to prevent compatibility issues, with some WebsocketServer implementations as for example QT5 QWebSocketServer

Changed in this revision

Websocket.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r efa2c147bee1 -r a2686b9d28d5 Websocket.cpp
--- a/Websocket.cpp	Thu Mar 16 21:10:27 2017 +0000
+++ b/Websocket.cpp	Sun Jan 28 14:45:05 2018 +0000
@@ -14,7 +14,7 @@
 #define ERR(x, ...) 
 #endif
 
-#define INFO(x, ...) printf("[WebSocket : INFO]"x"\r\n", ##__VA_ARGS__); 
+#define INFO(x, ...) std::printf("[WebSocket : INFO]"x"\r\n", ##__VA_ARGS__); 
 
 Websocket::Websocket(char * url, NetworkInterface * iface) {
     fillFields(url);
@@ -122,7 +122,7 @@
 
 bool Websocket::connect() {
     char cmd[200];
-
+    int cmdPos = 0;
     while (socket.connect(host, port) < 0) {
         ERR("Unable to connect to (%s) on port (%d)", host, port);
         wait(0.2);
@@ -130,24 +130,19 @@
     }
 
     // sent http header to upgrade to the ws protocol
-    sprintf(cmd, "GET %s HTTP/1.1\r\n", path);
-    write(cmd, strlen(cmd));
+    cmdPos += sprintf(cmd+cmdPos, "GET %s HTTP/1.1\r\n", path);
     
-    sprintf(cmd, "Host: %s:%d\r\n", host, port);
-    write(cmd, strlen(cmd));
+    cmdPos += sprintf(cmd+cmdPos, "Host: %s:%d\r\n", host, port);
 
-    sprintf(cmd, "Upgrade: WebSocket\r\n");
-    write(cmd, strlen(cmd));
+    cmdPos += sprintf(cmd+cmdPos, "Upgrade: WebSocket\r\n");
 
-    sprintf(cmd, "Connection: Upgrade\r\n");
-    write(cmd, strlen(cmd));
+    cmdPos += sprintf(cmd+cmdPos, "Connection: Upgrade\r\n");
 
-    sprintf(cmd, "Sec-WebSocket-Key: L159VM0TWUzyDxwJEIEzjw==\r\n");
-    write(cmd, strlen(cmd));
-
-    sprintf(cmd, "Sec-WebSocket-Version: 13\r\n\r\n");
+    cmdPos += sprintf(cmd+cmdPos, "Sec-WebSocket-Key: L159VM0TWUzyDxwJEIEzjw==\r\n");
+    
+    sprintf(cmd+cmdPos, "Sec-WebSocket-Version: 13\r\n\r\n");
     int ret = write(cmd, strlen(cmd));
-    if (ret != strlen(cmd)) {
+    if (ret != (int)strlen(cmd)) {
         close();
         ERR("Could not send request");
         return false;