WebSocket client library

Files at this revision

API Documentation at this revision

Comitter:
samux
Date:
Fri Aug 12 11:21:45 2011 +0000
Child:
1:0bb1153de91e
Commit message:

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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Websocket.cpp	Fri Aug 12 11:21:45 2011 +0000
@@ -0,0 +1,156 @@
+#include "Websocket.h"
+
+Websocket::Websocket(char * url, int port, Wifly * wifi)
+{
+    char *res = NULL;
+    char buf[30];
+    strcpy(buf, url);
+     
+    res = strtok(buf, ":");
+    if (strcmp(res, "ws"))
+    {
+        this->ip_domain = NULL;
+        this->path = NULL;
+        printf("\r\nFormat error: please use: \"ws://ip-or-domain/path\"\r\n\r\n");
+    }
+    else
+    {
+        res = strtok(NULL, ":");
+        res = strtok(res, "/");
+        if(res != NULL)
+        {
+            ip_domain = (char *) malloc (sizeof(char) * strlen(res));
+            strcpy(this->ip_domain, res);
+        }
+        res = strtok(NULL, " ");
+        if(res != NULL)
+        {
+            path = (char *) malloc (sizeof(char) * strlen(res));
+            strcpy(this->path, res);
+        }
+        
+        this->port = port;
+        this->wifi = wifi;
+        
+    }
+}
+
+bool Websocket::connect()
+{
+    char cmd[50];
+    wifi->Send("exit\r", "NO");
+    //enter in cmd mode
+    while(!wifi->Send("$$$", "CMD"))
+    {
+        printf("cannot enter in CMD mode\r\n");
+        wifi->exit();
+    }
+    
+
+    //open the connection
+    sprintf(cmd, "open %s %d\r\n", ip_domain, port);
+    if(!wifi->Send(cmd, "OPEN"))
+    {   
+        printf("Websocket::connect cannot open\r\n");
+        return false;
+    }
+    
+    
+    //send websocket HTTP header
+    sprintf(cmd, "GET /%s HTTP/1.1\r\n", path);
+    wifi->Send(cmd, "NO");
+    
+    sprintf(cmd, "Host: %s:%d\r\n", ip_domain, port);
+    wifi->Send(cmd, "NO");
+    
+    wifi->Send("Upgrade: WebSocket\r\n", "NO");
+    
+    sprintf(cmd, "Origin: http:%s:%d\r\n", ip_domain, port);
+    wifi->Send(cmd, "NO");
+    
+    
+    wifi->Send("Connection: Upgrade\r\n", "NO");
+    wifi->Send("Sec-WebSocket-Key1: 4 @1  46546xW%0l 1 5\r\n", "NO");
+    wifi->Send("Sec-WebSocket-key2: 12998 5 Y3 1  .P00\r\n\r\n", "NO");
+    if(!wifi->Send("^n:ds[4U", "8jKS'y:G*Co,Wxa-"))
+        return false;
+        
+    printf("\r\nip_domain: %s\r\npath: /%s\r\nport: %d\r\n\r\n",this->ip_domain, this->path, this->port);
+    return true;
+}
+
+void Websocket::Send(char * str)
+{
+    char cmd[100];
+    wifi->putc('\x00');
+    sprintf(cmd, "%s%c", "test", '\xff');
+    wifi->Send(cmd, "NO");
+}
+
+bool Websocket::read(char * message)
+{
+    int i = 0;
+    char char_read;
+    
+    if (!wifi->readable())
+    {
+        message = NULL;
+        return false;
+    }
+        
+    if (wifi->getc() != 0x00)
+    {
+        message = NULL;
+        return false;
+    }
+        
+    while ( (char_read = wifi->getc()) != 0xff)
+        message[i++] = char_read;
+        
+    return true;
+}
+
+bool Websocket::close()
+{
+    if(!wifi->CmdMode())
+    {
+        printf("Websocket::close: cannot enter in cmd mode\r\n");
+        return false;
+    }
+    
+    wifi->Send("close\r", "NO");
+    
+    if(!wifi->exit())
+        return false;
+        
+    return true;
+}
+
+
+
+bool Websocket::connected()
+{
+    char cmd[30];
+    
+    if(!wifi->CmdMode())
+    {
+        printf("Websocket::connected: cannot enter in cmd mode\r\n");
+        return false;
+    }
+    
+    //try to (re)open the connection
+    sprintf(cmd, "open %s %d\r\n", ip_domain, port);
+    if(wifi->Send(cmd, "Connected"))
+    {   
+        if(!wifi->exit())
+            return false;
+        return true;
+    }
+    else
+    {
+        wifi->exit();
+        return false;
+    }
+        
+    
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Websocket.h	Fri Aug 12 11:21:45 2011 +0000
@@ -0,0 +1,132 @@
+/**
+* @author Samuel Mokrani
+*
+* @section LICENSE
+*
+* Copyright (c) 2011 mbed
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy
+* of this software and associated documentation files (the "Software"), to deal
+* in the Software without restriction, including without limitation the rights
+* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+* copies of the Software, and to permit persons to whom the Software is
+* furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*
+* @section DESCRIPTION
+*    Simple websocket client
+*
+*/ 
+
+#ifndef WEBSOCKET_H
+#define WEBSOCKET_H
+
+#include "mbed.h"
+#include "Wifly.h"
+/** Websocket client Class. 
+ *
+ * Warning: you must use a wifi module (Wifly RN131-C) to use this class
+ *
+ * Example:
+ * @code
+ * #include "mbed.h"
+ * #include "Wifly.h"
+ * #include "Websocket.h"
+ * 
+ * Serial pc(USBTX, USBRX);
+ * Wifly * wifly;
+ * Websocket * ws;
+ *
+ * int main()
+ * {
+ *   wifly = new Wifly(p9, p10, p20, "network", "password", true);
+ *   ws = new Websocket("ws://ip_domain/path", 80, wifly);
+ *   
+ *   if(wifly->Join())
+ *   {
+ *       if(ws->connect())
+ *       {
+ *           pc.printf("ws connected\r\n");
+ *           while(1)
+ *           {
+ *               wait(0.1);
+ *               ws->Send("test");
+ *           }
+ *       }
+ *       else
+ *           pc.printf("ws not connected\r\n");
+ *   }
+ *   else
+ *       pc.printf("join network failed\r\n");
+ *       
+ * }
+ * @endcode
+ */
+class Websocket
+{
+    public:
+        /**
+        * Constructor
+        *
+        * @param url The Websocket url in the form "ws://ip_domain/path"
+        * @param port port
+        * @param wifi pointer to a wifi module (the communication will be establish by this module)
+        */
+        Websocket(char * url, int port, Wifly * wifi);
+        
+        /**
+        * Connect to the websocket
+        *
+        *@ return true if the connection is established, false otherwise
+        */
+        bool connect();
+        
+        /**
+        * To see if characters are available on the serial port
+        *
+        * @return true if characters are available, false otherwise
+        */
+        void Send(char * str);
+        
+        /**
+        * Read a websocket message
+        *
+        * @param message pointer to the string to be read (null if drop frame)
+        *
+        * @return true if a string has been read, false otherwise
+        */
+        bool read(char * message);
+        
+        /**
+        * To see if there is a websocket connection
+        *
+        * @return true if there is a connection
+        */
+        bool connected();
+        
+        /**
+        * Close the websocket connection
+        *
+        * @ return true if the connection has been closed, false otherwise
+        */
+        bool close();
+    
+    private:
+        char * ip_domain;
+        char * path;
+        int port;
+        Wifly * wifi;
+
+};
+
+#endif
\ No newline at end of file