WebSocket client library

Committer:
samux
Date:
Wed May 09 11:45:54 2012 +0000
Revision:
25:2214f1e5d4a3
Parent:
22:f4aac491ea26

        

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 17:1e339933c97a 36 #include <string>
samux 16:d4518b50f653 37
samux 22:f4aac491ea26 38
samux 22:f4aac491ea26 39 #ifdef TARGET_LPC1768
samux 6:01a1eb7c0145 40 #include "EthernetNetIf.h"
samux 6:01a1eb7c0145 41 #include "TCPSocket.h"
samux 6:01a1eb7c0145 42 #include "dnsresolve.h"
samux 22:f4aac491ea26 43 #endif //target
samux 22:f4aac491ea26 44
samux 6:01a1eb7c0145 45
samux 0:21fe3b05249f 46 /** Websocket client Class.
samux 0:21fe3b05249f 47 *
samux 13:3b058372cad9 48 * Warning: you must use a wifi module (Wifly RN131-C) or an ethernet network to use this class
samux 0:21fe3b05249f 49 *
samux 13:3b058372cad9 50 * Example (wifi network):
samux 0:21fe3b05249f 51 * @code
samux 0:21fe3b05249f 52 * #include "mbed.h"
samux 0:21fe3b05249f 53 * #include "Wifly.h"
samux 0:21fe3b05249f 54 * #include "Websocket.h"
samux 0:21fe3b05249f 55 *
samux 22:f4aac491ea26 56 * DigitalOut l1(LED1);
samux 22:f4aac491ea26 57 *
samux 22:f4aac491ea26 58 * //Here, we create an instance, with pins 9 and 10 connecting to the
samux 22:f4aac491ea26 59 * //WiFly's TX and RX pins, and pin 21 to RESET. We are connecting to the
samux 22:f4aac491ea26 60 * //"mbed" network, password "password", and we are using WPA.
samux 22:f4aac491ea26 61 * Wifly wifly(p9, p10, p21, "mbed", "password", true);
samux 22:f4aac491ea26 62 *
samux 22:f4aac491ea26 63 * //Here, we create a Websocket instance in 'wo' (write-only) mode
samux 22:f4aac491ea26 64 * //on the 'samux' channel
samux 22:f4aac491ea26 65 * Websocket ws("ws://sockets.mbed.org/ws/samux/wo", &wifly);
samux 22:f4aac491ea26 66 *
samux 22:f4aac491ea26 67 *
samux 22:f4aac491ea26 68 * int main() {
samux 22:f4aac491ea26 69 * while (1) {
samux 22:f4aac491ea26 70 *
samux 22:f4aac491ea26 71 * //we connect the network
samux 22:f4aac491ea26 72 * while (!wifly.join()) {
samux 22:f4aac491ea26 73 * wifly.reset();
samux 22:f4aac491ea26 74 * }
samux 22:f4aac491ea26 75 *
samux 22:f4aac491ea26 76 * //we connect to the websocket server
samux 22:f4aac491ea26 77 * while (!ws.connect());
samux 22:f4aac491ea26 78 *
samux 22:f4aac491ea26 79 * while (1) {
samux 22:f4aac491ea26 80 * wait(0.5);
samux 22:f4aac491ea26 81 *
samux 22:f4aac491ea26 82 * //Send Hello world
samux 22:f4aac491ea26 83 * ws.send("Hello World! over Wifi");
samux 22:f4aac491ea26 84 *
samux 22:f4aac491ea26 85 * // show that we are alive
samux 22:f4aac491ea26 86 * l1 = !l1;
samux 22:f4aac491ea26 87 * }
samux 22:f4aac491ea26 88 * }
samux 0:21fe3b05249f 89 * }
samux 0:21fe3b05249f 90 * @endcode
samux 13:3b058372cad9 91 *
samux 13:3b058372cad9 92 *
samux 13:3b058372cad9 93 *
samux 13:3b058372cad9 94 * Example (ethernet network):
samux 13:3b058372cad9 95 * @code
samux 13:3b058372cad9 96 * #include "mbed.h"
samux 13:3b058372cad9 97 * #include "Websocket.h"
samux 13:3b058372cad9 98 *
samux 22:f4aac491ea26 99 * Timer tmr;
samux 22:f4aac491ea26 100 *
samux 22:f4aac491ea26 101 * //Here, we create a Websocket instance in 'wo' (write-only) mode
samux 22:f4aac491ea26 102 * //on the 'samux' channel
samux 22:f4aac491ea26 103 * Websocket ws("ws://sockets.mbed.org/ws/samux/wo");
samux 22:f4aac491ea26 104 *
samux 22:f4aac491ea26 105 * int main() {
samux 22:f4aac491ea26 106 * while (1) {
samux 22:f4aac491ea26 107 *
samux 22:f4aac491ea26 108 * while (!ws.connect());
samux 22:f4aac491ea26 109 *
samux 22:f4aac491ea26 110 * tmr.start();
samux 22:f4aac491ea26 111 * while (1) {
samux 22:f4aac491ea26 112 * if (tmr.read() > 0.5) {
samux 22:f4aac491ea26 113 * ws.send("Hello World! over Ethernet");
samux 22:f4aac491ea26 114 * tmr.start();
samux 22:f4aac491ea26 115 * }
samux 22:f4aac491ea26 116 * Net::poll();
samux 22:f4aac491ea26 117 * }
samux 22:f4aac491ea26 118 * }
samux 13:3b058372cad9 119 * }
samux 13:3b058372cad9 120 * @endcode
samux 0:21fe3b05249f 121 */
samux 0:21fe3b05249f 122 class Websocket
samux 0:21fe3b05249f 123 {
samux 0:21fe3b05249f 124 public:
samux 0:21fe3b05249f 125 /**
samux 18:ef44ea603938 126 * Constructor
samux 0:21fe3b05249f 127 *
samux 6:01a1eb7c0145 128 * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80)
samux 0:21fe3b05249f 129 * @param wifi pointer to a wifi module (the communication will be establish by this module)
samux 0:21fe3b05249f 130 */
samux 6:01a1eb7c0145 131 Websocket(char * url, Wifly * wifi);
samux 6:01a1eb7c0145 132
samux 22:f4aac491ea26 133 #ifdef TARGET_LPC1768
samux 6:01a1eb7c0145 134 /**
samux 6:01a1eb7c0145 135 * Constructor for an ethernet communication
samux 6:01a1eb7c0145 136 *
samux 6:01a1eb7c0145 137 * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80)
samux 6:01a1eb7c0145 138 */
samux 6:01a1eb7c0145 139 Websocket(char * url);
samux 22:f4aac491ea26 140 #endif //target
samux 0:21fe3b05249f 141
samux 0:21fe3b05249f 142 /**
samux 9:9fa055ed54b4 143 * Connect to the websocket url
samux 0:21fe3b05249f 144 *
samux 1:0bb1153de91e 145 *@return true if the connection is established, false otherwise
samux 0:21fe3b05249f 146 */
samux 0:21fe3b05249f 147 bool connect();
samux 0:21fe3b05249f 148
samux 0:21fe3b05249f 149 /**
samux 22:f4aac491ea26 150 * Send a string according to the websocket format
samux 0:21fe3b05249f 151 *
samux 5:0f3422b6ca44 152 * @param str string to be sent
samux 0:21fe3b05249f 153 */
samux 18:ef44ea603938 154 void send(char * str);
samux 0:21fe3b05249f 155
samux 0:21fe3b05249f 156 /**
samux 0:21fe3b05249f 157 * Read a websocket message
samux 0:21fe3b05249f 158 *
samux 0:21fe3b05249f 159 * @param message pointer to the string to be read (null if drop frame)
samux 0:21fe3b05249f 160 *
samux 0:21fe3b05249f 161 * @return true if a string has been read, false otherwise
samux 0:21fe3b05249f 162 */
samux 0:21fe3b05249f 163 bool read(char * message);
samux 0:21fe3b05249f 164
samux 0:21fe3b05249f 165 /**
samux 12:1f6b9451a608 166 * To see if there is a websocket connection active
samux 0:21fe3b05249f 167 *
samux 12:1f6b9451a608 168 * @return true if there is a connection active
samux 0:21fe3b05249f 169 */
samux 0:21fe3b05249f 170 bool connected();
samux 0:21fe3b05249f 171
samux 0:21fe3b05249f 172 /**
samux 0:21fe3b05249f 173 * Close the websocket connection
samux 0:21fe3b05249f 174 *
samux 1:0bb1153de91e 175 * @return true if the connection has been closed, false otherwise
samux 0:21fe3b05249f 176 */
samux 0:21fe3b05249f 177 bool close();
samux 6:01a1eb7c0145 178
samux 17:1e339933c97a 179 /**
samux 17:1e339933c97a 180 * Accessor: get path from the websocket url
samux 17:1e339933c97a 181 *
samux 17:1e339933c97a 182 * @return path
samux 17:1e339933c97a 183 */
samux 17:1e339933c97a 184 std::string getPath();
samux 17:1e339933c97a 185
samux 16:d4518b50f653 186 enum Type {
samux 16:d4518b50f653 187 WIF,
samux 16:d4518b50f653 188 ETH
samux 16:d4518b50f653 189 };
samux 6:01a1eb7c0145 190
samux 0:21fe3b05249f 191
samux 0:21fe3b05249f 192 private:
samux 16:d4518b50f653 193
samux 6:01a1eb7c0145 194
samux 16:d4518b50f653 195 void fillFields(char * url);
samux 22:f4aac491ea26 196 void sendOpcode(uint8_t opcode);
samux 22:f4aac491ea26 197 void sendLength(uint32_t len);
samux 22:f4aac491ea26 198 void sendMask();
samux 22:f4aac491ea26 199 void sendChar(uint8_t c);
samux 6:01a1eb7c0145 200
samux 17:1e339933c97a 201 std::string ip_domain;
samux 17:1e339933c97a 202 std::string path;
samux 17:1e339933c97a 203 std::string port;
samux 6:01a1eb7c0145 204
samux 0:21fe3b05249f 205 Wifly * wifi;
samux 6:01a1eb7c0145 206
samux 22:f4aac491ea26 207 #ifdef TARGET_LPC1768
samux 16:d4518b50f653 208 void onTCPSocketEvent(TCPSocketEvent e);
samux 6:01a1eb7c0145 209 bool eth_connected;
samux 12:1f6b9451a608 210 bool eth_readable;
samux 12:1f6b9451a608 211 bool eth_writeable;
samux 9:9fa055ed54b4 212 char eth_rx[512];
samux 9:9fa055ed54b4 213 bool response_server_eth;
samux 16:d4518b50f653 214 bool new_msg;
samux 6:01a1eb7c0145 215
samux 16:d4518b50f653 216 EthernetNetIf * eth;
samux 16:d4518b50f653 217 TCPSocket * sock;
samux 16:d4518b50f653 218 IpAddr * server_ip;
samux 22:f4aac491ea26 219 #endif //target
samux 16:d4518b50f653 220
samux 16:d4518b50f653 221 Type netif;
samux 0:21fe3b05249f 222
samux 0:21fe3b05249f 223 };
samux 0:21fe3b05249f 224
samux 0:21fe3b05249f 225 #endif