Simple websocket client based on the original with a few added features such as: - setBaud() - set the baud rate for the communication - Initialize() - mimics the constructor - chaged read() to readmsg() to avoid confusion with other functions

Dependents:   IoT_Ex BatteryModelTester BatteryModelTester

Fork of WebSocketClient by Samuel Mokrani

Revision:
12:0979caf96fa6
Parent:
11:85bff70bab45
Child:
13:152b70450615
--- a/Websocket.cpp	Wed Mar 30 10:40:05 2016 +0000
+++ b/Websocket.cpp	Thu Mar 31 12:43:30 2016 +0000
@@ -4,7 +4,7 @@
 #define MAX_TRY_READ 10
 
 //Debug is disabled by default
-#if 0
+#if 1
 #define DBG(x, ...) std::printf("[WebSocket : DBG]"x"\r\n", ##__VA_ARGS__); 
 #define WARN(x, ...) std::printf("[WebSocket : WARN]"x"\r\n", ##__VA_ARGS__); 
 #define ERR(x, ...) std::printf("[WebSocket : ERR]"x"\r\n", ##__VA_ARGS__); 
@@ -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) {
     fillFields(url);
@@ -131,7 +131,7 @@
         wait(0.2);
         return false;
     }
-    pc.printf("TCP/IP Connection established, upgrading protocol...\n\r");
+    INFO("TCP/IP Connection established, upgrading protocol...");
     // sent http header to upgrade to the ws protocol
     sprintf(cmd, "GET %s HTTP/1.1\r\n", path);
     write(cmd, strlen(cmd));
@@ -175,13 +175,13 @@
                 return false;
             }
             cmd[ret] = '\0';
-            printf("%s",cmd);
+            DBG("%s",cmd);
         } while (ret > 0);
         close();
         return false;
     }
 
-    INFO("\r\nhost: %s\r\npath: %s\r\nport: %d\r\n\r\n", host, path, port);
+    INFO("\r\n\thost: %s\r\n\tpath: %s\r\n\tport: %d\r\n\r\n", host, path, port);
     return true;
 }
 
@@ -266,7 +266,7 @@
         if (opcode == 0x81)
             break;
     }
-    DBG("opcode: 0x%X\r\n", opcode);
+    DBG("opcode: 0x%X", opcode);
 
     readChar(&c);
     len_msg = c & 0x7f;
@@ -287,15 +287,18 @@
     if (len_msg == 0) {
         return false;
     }
-    DBG("length: %d\r\n", len_msg);
+    DBG("length: %d", len_msg);
     
     if (is_masked) {
         for (i = 0; i < 4; i++)
             readChar(&c);
         mask[i] = c;
     }
+    
+    DBG("Done readChar.");
 
     int nb = read(message, len_msg, len_msg);
+    DBG("Done nb:%d = read(message:%s, len_msg:%d, len_msg:%d)", nb, message, len_msg, len_msg);
     if (nb != len_msg)
         return false;
 
@@ -304,7 +307,7 @@
     }
 
     message[len_msg] = '\0';
-
+    DBG("Websocket::read() returning true, message:%s", message);
     return true;
 }
 
@@ -358,9 +361,8 @@
 
         if ((res = socket.receive_all(str + idx, len - idx)) == -1)
             continue;
-
         idx += res;
-        
+        DBG("In read(* str, len, min_len), res = %d, idx = %d", res, idx);
         if (idx == len || (min_len != -1 && idx > min_len))
             return idx;
     }