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 13:57:18 2016 +0000
Parent:
21:420b83755885
Child:
24:6f30d0c4ff7b
Commit message:
- Change the readmsg() funciton to return an int instead of bool.; - (-1) is returned by readmsg() if the server disconnects

Changed in this revision

Websocket.cpp Show annotated file Show diff for this revision Revisions of this file
Websocket.h Show annotated file Show diff for this revision Revisions of this file
--- a/Websocket.cpp	Fri Aug 05 13:31:48 2016 +0000
+++ b/Websocket.cpp	Tue Oct 04 13:57:18 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() {
--- a/Websocket.h	Fri Aug 05 13:31:48 2016 +0000
+++ b/Websocket.h	Tue Oct 04 13:57:18 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