Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Websocket.h Source File

Websocket.h

00001 /**
00002 * @author Samuel Mokrani
00003 *
00004 * @section LICENSE
00005 *
00006 * Copyright (c) 2011 mbed
00007 *
00008 * Permission is hereby granted, free of charge, to any person obtaining a copy
00009 * of this software and associated documentation files (the "Software"), to deal
00010 * in the Software without restriction, including without limitation the rights
00011 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012 * copies of the Software, and to permit persons to whom the Software is
00013 * furnished to do so, subject to the following conditions:
00014 *
00015 * The above copyright notice and this permission notice shall be included in
00016 * all copies or substantial portions of the Software.
00017 *
00018 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024 * THE SOFTWARE.
00025 *
00026 * @section DESCRIPTION
00027 *    Simple websocket client
00028 *
00029 */
00030 
00031 #ifndef WEBSOCKET_H
00032 #define WEBSOCKET_H
00033 
00034 #include "mbed.h"
00035 #include <string>
00036 
00037 #include "api/socket.h"
00038 
00039 /** Websocket client Class.
00040  *
00041  * Warning: you must use a wifi module (Wifly RN131-C) or an ethernet network to use this class
00042  *
00043  * Example (wifi network):
00044  * @code
00045  * #include "mbed.h"
00046  * #include "Wifly.h"
00047  * #include "Websocket.h"
00048  *
00049  * Serial pc(USBTX, USBRX);
00050  * Wifly * wifly;
00051  * Websocket * ws;
00052  *
00053  * int main()
00054  * {
00055  *   wifly = new Wifly(p9, p10, p20, "network", "password", true);
00056  *   ws = new Websocket("ws://ip_domain/path", wifly);
00057  *
00058  *   if(wifly->join())
00059  *   {
00060  *       if(ws->connect())
00061  *       {
00062  *           pc.printf("ws connected\r\n");
00063  *           while(1)
00064  *           {
00065  *               wait(0.1);
00066  *               ws->send("test");
00067  *           }
00068  *       }
00069  *       else
00070  *           pc.printf("ws not connected\r\n");
00071  *   }
00072  *   else
00073  *       pc.printf("join network failed\r\n");
00074  *
00075  * }
00076  * @endcode
00077  *
00078  *
00079  *
00080  * Example (ethernet network):
00081  * @code
00082  * #include "mbed.h"
00083  * #include "Websocket.h"
00084  *
00085  * Serial pc(USBTX, USBRX);
00086  * Websocket * ws;
00087  *
00088  * int main()
00089  * {
00090  *   ws = new Websocket("ws://ip_domain/path");
00091  *
00092  *   if(ws->connect())
00093  *   {
00094  *      pc.printf("ws connected\r\n");
00095  *      while(1)
00096  *      {
00097  *         wait(0.1);
00098  *         ws->send("test");
00099  *      }
00100  *   }
00101  *   else
00102  *      pc.printf("ws not connected\r\n");
00103  * }
00104  * @endcode
00105  */
00106 class Websocket
00107 {
00108     public:
00109         /**
00110         * Constructor for 3g communication
00111         *
00112         * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80)
00113         */
00114         Websocket(char * url);
00115 
00116         /**
00117         * Connect to the websocket url
00118         *
00119         *@return true if the connection is established, false otherwise
00120         */
00121         bool connect();
00122 
00123         /**
00124         * Send a string according to the websocket format: 00 str ff
00125         *
00126         * @param str string to be sent
00127         */
00128         void send(char * str);
00129 
00130         /**
00131         * Read a websocket message
00132         *
00133         * @param message pointer to the string to be read (null if drop frame)
00134         *
00135         * @return true if a string has been read, false otherwise
00136         */
00137         bool read(char * message);
00138 
00139         /**
00140         * To see if there is a websocket connection active
00141         *
00142         * @return true if there is a connection active
00143         */
00144         bool connected();
00145 
00146         /**
00147         * Close the websocket connection
00148         *
00149         * @return true if the connection has been closed, false otherwise
00150         */
00151         bool close();
00152 
00153         /**
00154         * Accessor: get path from the websocket url
00155         *
00156         * @return path
00157         */
00158         std::string getPath();
00159 
00160     private:
00161 
00162         void fillFields(char * url);
00163         void sendOpcode(uint8_t opcode);
00164         void sendLength(uint32_t len);
00165         void sendMask();
00166         void sendChar(uint8_t c);
00167         bool readChar(uint8_t* pC, bool block = true);
00168 
00169         std::string ip_domain;
00170         std::string path;
00171         std::string port;
00172 
00173         int m_sockHandle;
00174 
00175         struct sockaddr_in m_sockAddr;
00176 
00177         bool m_connected;
00178 
00179         int waitReadable(uint32_t timeout);
00180         int waitWriteable(uint32_t timeout);
00181         int read(uint8_t* buf, int minLen, int maxLen, uint32_t timeout=5000);
00182         int write(uint8_t* buf, int len, uint32_t timeout=5000);
00183         void timeoutHandler();
00184 };
00185 
00186 #endif