WebSocket client library

Committer:
samux
Date:
Fri Aug 12 11:21:45 2011 +0000
Revision:
0:21fe3b05249f
Child:
1:0bb1153de91e

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 0:21fe3b05249f 1 /**
samux 0:21fe3b05249f 2 * @author Samuel Mokrani
samux 0:21fe3b05249f 3 *
samux 0:21fe3b05249f 4 * @section LICENSE
samux 0:21fe3b05249f 5 *
samux 0:21fe3b05249f 6 * Copyright (c) 2011 mbed
samux 0:21fe3b05249f 7 *
samux 0:21fe3b05249f 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
samux 0:21fe3b05249f 9 * of this software and associated documentation files (the "Software"), to deal
samux 0:21fe3b05249f 10 * in the Software without restriction, including without limitation the rights
samux 0:21fe3b05249f 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
samux 0:21fe3b05249f 12 * copies of the Software, and to permit persons to whom the Software is
samux 0:21fe3b05249f 13 * furnished to do so, subject to the following conditions:
samux 0:21fe3b05249f 14 *
samux 0:21fe3b05249f 15 * The above copyright notice and this permission notice shall be included in
samux 0:21fe3b05249f 16 * all copies or substantial portions of the Software.
samux 0:21fe3b05249f 17 *
samux 0:21fe3b05249f 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
samux 0:21fe3b05249f 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
samux 0:21fe3b05249f 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
samux 0:21fe3b05249f 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
samux 0:21fe3b05249f 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 0:21fe3b05249f 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
samux 0:21fe3b05249f 24 * THE SOFTWARE.
samux 0:21fe3b05249f 25 *
samux 0:21fe3b05249f 26 * @section DESCRIPTION
samux 0:21fe3b05249f 27 * Simple websocket client
samux 0:21fe3b05249f 28 *
samux 0:21fe3b05249f 29 */
samux 0:21fe3b05249f 30
samux 0:21fe3b05249f 31 #ifndef WEBSOCKET_H
samux 0:21fe3b05249f 32 #define WEBSOCKET_H
samux 0:21fe3b05249f 33
samux 0:21fe3b05249f 34 #include "mbed.h"
samux 0:21fe3b05249f 35 #include "Wifly.h"
samux 0:21fe3b05249f 36 /** Websocket client Class.
samux 0:21fe3b05249f 37 *
samux 0:21fe3b05249f 38 * Warning: you must use a wifi module (Wifly RN131-C) to use this class
samux 0:21fe3b05249f 39 *
samux 0:21fe3b05249f 40 * Example:
samux 0:21fe3b05249f 41 * @code
samux 0:21fe3b05249f 42 * #include "mbed.h"
samux 0:21fe3b05249f 43 * #include "Wifly.h"
samux 0:21fe3b05249f 44 * #include "Websocket.h"
samux 0:21fe3b05249f 45 *
samux 0:21fe3b05249f 46 * Serial pc(USBTX, USBRX);
samux 0:21fe3b05249f 47 * Wifly * wifly;
samux 0:21fe3b05249f 48 * Websocket * ws;
samux 0:21fe3b05249f 49 *
samux 0:21fe3b05249f 50 * int main()
samux 0:21fe3b05249f 51 * {
samux 0:21fe3b05249f 52 * wifly = new Wifly(p9, p10, p20, "network", "password", true);
samux 0:21fe3b05249f 53 * ws = new Websocket("ws://ip_domain/path", 80, wifly);
samux 0:21fe3b05249f 54 *
samux 0:21fe3b05249f 55 * if(wifly->Join())
samux 0:21fe3b05249f 56 * {
samux 0:21fe3b05249f 57 * if(ws->connect())
samux 0:21fe3b05249f 58 * {
samux 0:21fe3b05249f 59 * pc.printf("ws connected\r\n");
samux 0:21fe3b05249f 60 * while(1)
samux 0:21fe3b05249f 61 * {
samux 0:21fe3b05249f 62 * wait(0.1);
samux 0:21fe3b05249f 63 * ws->Send("test");
samux 0:21fe3b05249f 64 * }
samux 0:21fe3b05249f 65 * }
samux 0:21fe3b05249f 66 * else
samux 0:21fe3b05249f 67 * pc.printf("ws not connected\r\n");
samux 0:21fe3b05249f 68 * }
samux 0:21fe3b05249f 69 * else
samux 0:21fe3b05249f 70 * pc.printf("join network failed\r\n");
samux 0:21fe3b05249f 71 *
samux 0:21fe3b05249f 72 * }
samux 0:21fe3b05249f 73 * @endcode
samux 0:21fe3b05249f 74 */
samux 0:21fe3b05249f 75 class Websocket
samux 0:21fe3b05249f 76 {
samux 0:21fe3b05249f 77 public:
samux 0:21fe3b05249f 78 /**
samux 0:21fe3b05249f 79 * Constructor
samux 0:21fe3b05249f 80 *
samux 0:21fe3b05249f 81 * @param url The Websocket url in the form "ws://ip_domain/path"
samux 0:21fe3b05249f 82 * @param port port
samux 0:21fe3b05249f 83 * @param wifi pointer to a wifi module (the communication will be establish by this module)
samux 0:21fe3b05249f 84 */
samux 0:21fe3b05249f 85 Websocket(char * url, int port, Wifly * wifi);
samux 0:21fe3b05249f 86
samux 0:21fe3b05249f 87 /**
samux 0:21fe3b05249f 88 * Connect to the websocket
samux 0:21fe3b05249f 89 *
samux 0:21fe3b05249f 90 *@ return true if the connection is established, false otherwise
samux 0:21fe3b05249f 91 */
samux 0:21fe3b05249f 92 bool connect();
samux 0:21fe3b05249f 93
samux 0:21fe3b05249f 94 /**
samux 0:21fe3b05249f 95 * To see if characters are available on the serial port
samux 0:21fe3b05249f 96 *
samux 0:21fe3b05249f 97 * @return true if characters are available, false otherwise
samux 0:21fe3b05249f 98 */
samux 0:21fe3b05249f 99 void Send(char * str);
samux 0:21fe3b05249f 100
samux 0:21fe3b05249f 101 /**
samux 0:21fe3b05249f 102 * Read a websocket message
samux 0:21fe3b05249f 103 *
samux 0:21fe3b05249f 104 * @param message pointer to the string to be read (null if drop frame)
samux 0:21fe3b05249f 105 *
samux 0:21fe3b05249f 106 * @return true if a string has been read, false otherwise
samux 0:21fe3b05249f 107 */
samux 0:21fe3b05249f 108 bool read(char * message);
samux 0:21fe3b05249f 109
samux 0:21fe3b05249f 110 /**
samux 0:21fe3b05249f 111 * To see if there is a websocket connection
samux 0:21fe3b05249f 112 *
samux 0:21fe3b05249f 113 * @return true if there is a connection
samux 0:21fe3b05249f 114 */
samux 0:21fe3b05249f 115 bool connected();
samux 0:21fe3b05249f 116
samux 0:21fe3b05249f 117 /**
samux 0:21fe3b05249f 118 * Close the websocket connection
samux 0:21fe3b05249f 119 *
samux 0:21fe3b05249f 120 * @ return true if the connection has been closed, false otherwise
samux 0:21fe3b05249f 121 */
samux 0:21fe3b05249f 122 bool close();
samux 0:21fe3b05249f 123
samux 0:21fe3b05249f 124 private:
samux 0:21fe3b05249f 125 char * ip_domain;
samux 0:21fe3b05249f 126 char * path;
samux 0:21fe3b05249f 127 int port;
samux 0:21fe3b05249f 128 Wifly * wifi;
samux 0:21fe3b05249f 129
samux 0:21fe3b05249f 130 };
samux 0:21fe3b05249f 131
samux 0:21fe3b05249f 132 #endif