Simple websocket client

Dependents:   WebsocketClient Web_suck_et APS SO - ALARME CONTROLADO VIA SOCKETS F411-mbed-os-iot-project ... more

Fork of WebSocketClient by mbed official

Revision:
9:efa2c147bee1
Parent:
8:ccedee13be8d
--- a/Websocket.cpp	Wed Oct 23 09:58:39 2013 +0000
+++ b/Websocket.cpp	Thu Mar 16 21:10:27 2017 +0000
@@ -1,18 +1,3 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2013 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
 #include "Websocket.h"
 
 #define MAX_TRY_WRITE 20
@@ -31,9 +16,10 @@
 
 #define INFO(x, ...) printf("[WebSocket : INFO]"x"\r\n", ##__VA_ARGS__); 
 
-Websocket::Websocket(char * url) {
+Websocket::Websocket(char * url, NetworkInterface * iface) {
     fillFields(url);
-    socket.set_blocking(false, 400);
+    socket.open(iface);
+    socket.set_timeout(400);
 }
 
 void Websocket::fillFields(char * url) {
@@ -93,6 +79,11 @@
     *port=0;
   }
   char* pathPtr = strchr(hostPtr, '/');
+  if(pathPtr == NULL)
+  {
+    WARN("Path not specified. Please add /[path] to the end of the websocket address");
+    return -1;
+  }
   if( hostLen == 0 )
   {
     hostLen = pathPtr - hostPtr;
@@ -181,7 +172,6 @@
                 return false;
             }
             cmd[ret] = '\0';
-            printf("%s",cmd);
         } while (ret > 0);
         close();
         return false;
@@ -254,20 +244,14 @@
             DBG("timeout ws\r\n");
             return false;
         }
-        
-        if(!socket.is_connected())
-        {
-            WARN("Connection was closed by server");
+
+        socket.set_timeout(1);
+        if (socket.recv(&opcode, 1) != 1) {
+            socket.set_timeout(2000);
             return false;
         }
 
-        socket.set_blocking(false, 1);
-        if (socket.receive(&opcode, 1) != 1) {
-            socket.set_blocking(false, 2000);
-            return false;
-        }
-
-        socket.set_blocking(false, 2000);
+        socket.set_timeout(2000);
 
         if (opcode == 0x81)
             break;
@@ -296,9 +280,10 @@
     DBG("length: %d\r\n", len_msg);
     
     if (is_masked) {
-        for (i = 0; i < 4; i++)
-            readChar(&c);
-        mask[i] = c;
+        for (i = 0; i < 4; i++) {
+          readChar(&c);
+          mask[i] = c;
+        }
     }
 
     int nb = read(message, len_msg, len_msg);
@@ -315,8 +300,6 @@
 }
 
 bool Websocket::close() {
-    if (!is_connected())
-        return false;
 
     int ret = socket.close();
     if (ret < 0) {
@@ -326,10 +309,6 @@
     return true;
 }
 
-bool Websocket::is_connected() {
-    return socket.is_connected();
-}
-
 char* Websocket::getPath() {
     return path;
 }
@@ -338,14 +317,8 @@
     int res = 0, idx = 0;
     
     for (int j = 0; j < MAX_TRY_WRITE; j++) {
-    
-        if(!socket.is_connected())
-        {
-            WARN("Connection was closed by server");
-            break;
-        }
 
-        if ((res = socket.send_all(str + idx, len - idx)) == -1)
+        if ((res = socket.send(str + idx, len - idx)) < 0)
             continue;
 
         idx += res;
@@ -362,8 +335,8 @@
     
     for (int j = 0; j < MAX_TRY_WRITE; j++) {
 
-        if ((res = socket.receive_all(str + idx, len - idx)) == -1)
-            continue;
+        if ((res = socket.recv(str + idx, len - idx)) < 0)
+          continue;
 
         idx += res;