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

Files at this revision

API Documentation at this revision

Comitter:
defrost
Date:
Tue Oct 04 16:46:59 2016 +0000
Parent:
22:d2c00e47527c
Parent:
24:6f30d0c4ff7b
Commit message:
- Merged with changes from the internets

Changed in this revision

Websocket.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r d2c00e47527c -r 4a98a3b83b87 Websocket.cpp
--- a/Websocket.cpp	Wed Aug 31 10:03:43 2016 +0000
+++ b/Websocket.cpp	Tue Oct 04 16:46:59 2016 +0000
@@ -254,7 +254,7 @@
 }
 
 
-bool Websocket::readmsg(char * message) {
+int Websocket::readmsg(char * message) {
     int i = 0;
     uint32_t len_msg;
     char opcode = 0;
@@ -268,19 +268,19 @@
     while (true) {
         if (tmr.read() > 3) {
             DBG("timeout ws\r\n");
-            return false;
+            return -1;
         }
         
         if(!socket.is_connected())
         {
             WARN("Connection was closed by server");
-            return false;
+            return -1;
         }
 
         socket.set_blocking(false, 1);
         if (socket.receive(&opcode, 1) != 1) {
             socket.set_blocking(false, 2000);
-            return false;
+            return 0;
         }
 
         socket.set_blocking(false, 2000);
@@ -307,7 +307,7 @@
     }
 
     if (len_msg == 0) {
-        return false;
+        return 0;
     }
     DBG("length: %d", len_msg);
     
@@ -322,7 +322,7 @@
     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;
+        return 0;
 
     for (i = 0; i < len_msg; i++) {
         message[i] = message[i] ^ mask[i % 4];
@@ -330,7 +330,7 @@
 
     message[len_msg] = '\0';
     DBG("Websocket::read() returning true, message:%s", message);
-    return true;
+    return 1;
 }
 
 bool Websocket::close() {
diff -r d2c00e47527c -r 4a98a3b83b87 Websocket.h
--- a/Websocket.h	Wed Aug 31 10:03:43 2016 +0000
+++ b/Websocket.h	Tue Oct 04 16:46:59 2016 +0000
@@ -104,9 +104,11 @@
         *
         * @param message pointer to the string to be read (null if drop frame)
         *
-        * @return true if a websocket frame has been read
+        * @return 1 if a websocket frame has been read
+        * @return 0 if a websocket frame could not be read
+        * @return -1 if the server connection is closed
         */
-        bool readmsg(char * message);
+        int readmsg(char * message);
 
         /**
         * To see if there is a websocket connection active