Web Socket connection (MBED is client side)
Dependencies: EthernetNetIf MbedJSONValue mbed
Fork of WebSocket_test by
Websocket/Websocket.h@0:236a084b1d6b, 2011-11-02 (annotated)
- Committer:
- okini3939
- Date:
- Wed Nov 02 02:56:07 2011 +0000
- Revision:
- 0:236a084b1d6b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 0:236a084b1d6b | 1 | /** |
okini3939 | 0:236a084b1d6b | 2 | * @author Samuel Mokrani |
okini3939 | 0:236a084b1d6b | 3 | * |
okini3939 | 0:236a084b1d6b | 4 | * @section LICENSE |
okini3939 | 0:236a084b1d6b | 5 | * |
okini3939 | 0:236a084b1d6b | 6 | * Copyright (c) 2011 mbed |
okini3939 | 0:236a084b1d6b | 7 | * |
okini3939 | 0:236a084b1d6b | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
okini3939 | 0:236a084b1d6b | 9 | * of this software and associated documentation files (the "Software"), to deal |
okini3939 | 0:236a084b1d6b | 10 | * in the Software without restriction, including without limitation the rights |
okini3939 | 0:236a084b1d6b | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
okini3939 | 0:236a084b1d6b | 12 | * copies of the Software, and to permit persons to whom the Software is |
okini3939 | 0:236a084b1d6b | 13 | * furnished to do so, subject to the following conditions: |
okini3939 | 0:236a084b1d6b | 14 | * |
okini3939 | 0:236a084b1d6b | 15 | * The above copyright notice and this permission notice shall be included in |
okini3939 | 0:236a084b1d6b | 16 | * all copies or substantial portions of the Software. |
okini3939 | 0:236a084b1d6b | 17 | * |
okini3939 | 0:236a084b1d6b | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
okini3939 | 0:236a084b1d6b | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
okini3939 | 0:236a084b1d6b | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
okini3939 | 0:236a084b1d6b | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
okini3939 | 0:236a084b1d6b | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
okini3939 | 0:236a084b1d6b | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
okini3939 | 0:236a084b1d6b | 24 | * THE SOFTWARE. |
okini3939 | 0:236a084b1d6b | 25 | * |
okini3939 | 0:236a084b1d6b | 26 | * @section DESCRIPTION |
okini3939 | 0:236a084b1d6b | 27 | * Simple websocket client |
okini3939 | 0:236a084b1d6b | 28 | * |
okini3939 | 0:236a084b1d6b | 29 | */ |
okini3939 | 0:236a084b1d6b | 30 | /* |
okini3939 | 0:236a084b1d6b | 31 | * modify by Suga |
okini3939 | 0:236a084b1d6b | 32 | */ |
okini3939 | 0:236a084b1d6b | 33 | |
okini3939 | 0:236a084b1d6b | 34 | #ifndef WEBSOCKET_H |
okini3939 | 0:236a084b1d6b | 35 | #define WEBSOCKET_H |
okini3939 | 0:236a084b1d6b | 36 | |
okini3939 | 0:236a084b1d6b | 37 | //#define WIFLY |
okini3939 | 0:236a084b1d6b | 38 | //#define ETH_SETUP |
okini3939 | 0:236a084b1d6b | 39 | |
okini3939 | 0:236a084b1d6b | 40 | #include "mbed.h" |
okini3939 | 0:236a084b1d6b | 41 | #ifdef WIFLY |
okini3939 | 0:236a084b1d6b | 42 | #include "Wifly.h" |
okini3939 | 0:236a084b1d6b | 43 | #endif |
okini3939 | 0:236a084b1d6b | 44 | #include <string> |
okini3939 | 0:236a084b1d6b | 45 | |
okini3939 | 0:236a084b1d6b | 46 | #include "EthernetNetIf.h" |
okini3939 | 0:236a084b1d6b | 47 | #include "TCPSocket.h" |
okini3939 | 0:236a084b1d6b | 48 | //#include "dnsresolve.h" |
okini3939 | 0:236a084b1d6b | 49 | #include "DNSRequest.h" |
okini3939 | 0:236a084b1d6b | 50 | |
okini3939 | 0:236a084b1d6b | 51 | /** Websocket client Class. |
okini3939 | 0:236a084b1d6b | 52 | * |
okini3939 | 0:236a084b1d6b | 53 | * Warning: you must use a wifi module (Wifly RN131-C) or an ethernet network to use this class |
okini3939 | 0:236a084b1d6b | 54 | * |
okini3939 | 0:236a084b1d6b | 55 | * Example (wifi network): |
okini3939 | 0:236a084b1d6b | 56 | * @code |
okini3939 | 0:236a084b1d6b | 57 | * #include "mbed.h" |
okini3939 | 0:236a084b1d6b | 58 | * #include "Wifly.h" |
okini3939 | 0:236a084b1d6b | 59 | * #include "Websocket.h" |
okini3939 | 0:236a084b1d6b | 60 | * |
okini3939 | 0:236a084b1d6b | 61 | * Serial pc(USBTX, USBRX); |
okini3939 | 0:236a084b1d6b | 62 | * Wifly * wifly; |
okini3939 | 0:236a084b1d6b | 63 | * Websocket * ws; |
okini3939 | 0:236a084b1d6b | 64 | * |
okini3939 | 0:236a084b1d6b | 65 | * int main() |
okini3939 | 0:236a084b1d6b | 66 | * { |
okini3939 | 0:236a084b1d6b | 67 | * wifly = new Wifly(p9, p10, p20, "network", "password", true); |
okini3939 | 0:236a084b1d6b | 68 | * ws = new Websocket("ws://ip_domain/path", wifly); |
okini3939 | 0:236a084b1d6b | 69 | * |
okini3939 | 0:236a084b1d6b | 70 | * if(wifly->join()) |
okini3939 | 0:236a084b1d6b | 71 | * { |
okini3939 | 0:236a084b1d6b | 72 | * if(ws->connect()) |
okini3939 | 0:236a084b1d6b | 73 | * { |
okini3939 | 0:236a084b1d6b | 74 | * pc.printf("ws connected\r\n"); |
okini3939 | 0:236a084b1d6b | 75 | * while(1) |
okini3939 | 0:236a084b1d6b | 76 | * { |
okini3939 | 0:236a084b1d6b | 77 | * wait(0.1); |
okini3939 | 0:236a084b1d6b | 78 | * ws->send("test"); |
okini3939 | 0:236a084b1d6b | 79 | * } |
okini3939 | 0:236a084b1d6b | 80 | * } |
okini3939 | 0:236a084b1d6b | 81 | * else |
okini3939 | 0:236a084b1d6b | 82 | * pc.printf("ws not connected\r\n"); |
okini3939 | 0:236a084b1d6b | 83 | * } |
okini3939 | 0:236a084b1d6b | 84 | * else |
okini3939 | 0:236a084b1d6b | 85 | * pc.printf("join network failed\r\n"); |
okini3939 | 0:236a084b1d6b | 86 | * |
okini3939 | 0:236a084b1d6b | 87 | * } |
okini3939 | 0:236a084b1d6b | 88 | * @endcode |
okini3939 | 0:236a084b1d6b | 89 | * |
okini3939 | 0:236a084b1d6b | 90 | * |
okini3939 | 0:236a084b1d6b | 91 | * |
okini3939 | 0:236a084b1d6b | 92 | * Example (ethernet network): |
okini3939 | 0:236a084b1d6b | 93 | * @code |
okini3939 | 0:236a084b1d6b | 94 | * #include "mbed.h" |
okini3939 | 0:236a084b1d6b | 95 | * #include "Websocket.h" |
okini3939 | 0:236a084b1d6b | 96 | * |
okini3939 | 0:236a084b1d6b | 97 | * Serial pc(USBTX, USBRX); |
okini3939 | 0:236a084b1d6b | 98 | * Websocket * ws; |
okini3939 | 0:236a084b1d6b | 99 | * |
okini3939 | 0:236a084b1d6b | 100 | * int main() |
okini3939 | 0:236a084b1d6b | 101 | * { |
okini3939 | 0:236a084b1d6b | 102 | * ws = new Websocket("ws://ip_domain/path"); |
okini3939 | 0:236a084b1d6b | 103 | * |
okini3939 | 0:236a084b1d6b | 104 | * if(ws->connect()) |
okini3939 | 0:236a084b1d6b | 105 | * { |
okini3939 | 0:236a084b1d6b | 106 | * pc.printf("ws connected\r\n"); |
okini3939 | 0:236a084b1d6b | 107 | * while(1) |
okini3939 | 0:236a084b1d6b | 108 | * { |
okini3939 | 0:236a084b1d6b | 109 | * wait(0.1); |
okini3939 | 0:236a084b1d6b | 110 | * ws->send("test"); |
okini3939 | 0:236a084b1d6b | 111 | * } |
okini3939 | 0:236a084b1d6b | 112 | * } |
okini3939 | 0:236a084b1d6b | 113 | * else |
okini3939 | 0:236a084b1d6b | 114 | * pc.printf("ws not connected\r\n"); |
okini3939 | 0:236a084b1d6b | 115 | * } |
okini3939 | 0:236a084b1d6b | 116 | * @endcode |
okini3939 | 0:236a084b1d6b | 117 | */ |
okini3939 | 0:236a084b1d6b | 118 | class Websocket |
okini3939 | 0:236a084b1d6b | 119 | { |
okini3939 | 0:236a084b1d6b | 120 | public: |
okini3939 | 0:236a084b1d6b | 121 | /** |
okini3939 | 0:236a084b1d6b | 122 | * Constructor |
okini3939 | 0:236a084b1d6b | 123 | * |
okini3939 | 0:236a084b1d6b | 124 | * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80) |
okini3939 | 0:236a084b1d6b | 125 | * @param wifi pointer to a wifi module (the communication will be establish by this module) |
okini3939 | 0:236a084b1d6b | 126 | */ |
okini3939 | 0:236a084b1d6b | 127 | #ifdef WIFLY |
okini3939 | 0:236a084b1d6b | 128 | Websocket(char * url, Wifly * wifi); |
okini3939 | 0:236a084b1d6b | 129 | #endif |
okini3939 | 0:236a084b1d6b | 130 | |
okini3939 | 0:236a084b1d6b | 131 | /** |
okini3939 | 0:236a084b1d6b | 132 | * Constructor for an ethernet communication |
okini3939 | 0:236a084b1d6b | 133 | * |
okini3939 | 0:236a084b1d6b | 134 | * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80) |
okini3939 | 0:236a084b1d6b | 135 | */ |
okini3939 | 0:236a084b1d6b | 136 | Websocket(char * url, EthernetNetIf *e); |
okini3939 | 0:236a084b1d6b | 137 | |
okini3939 | 0:236a084b1d6b | 138 | /** |
okini3939 | 0:236a084b1d6b | 139 | * Connect to the websocket url |
okini3939 | 0:236a084b1d6b | 140 | * |
okini3939 | 0:236a084b1d6b | 141 | *@return true if the connection is established, false otherwise |
okini3939 | 0:236a084b1d6b | 142 | */ |
okini3939 | 0:236a084b1d6b | 143 | bool connect(); |
okini3939 | 0:236a084b1d6b | 144 | |
okini3939 | 0:236a084b1d6b | 145 | /** |
okini3939 | 0:236a084b1d6b | 146 | * Send a string according to the websocket format: 00 str ff |
okini3939 | 0:236a084b1d6b | 147 | * |
okini3939 | 0:236a084b1d6b | 148 | * @param str string to be sent |
okini3939 | 0:236a084b1d6b | 149 | */ |
okini3939 | 0:236a084b1d6b | 150 | void send(char * str); |
okini3939 | 0:236a084b1d6b | 151 | |
okini3939 | 0:236a084b1d6b | 152 | /** |
okini3939 | 0:236a084b1d6b | 153 | * Read a websocket message |
okini3939 | 0:236a084b1d6b | 154 | * |
okini3939 | 0:236a084b1d6b | 155 | * @param message pointer to the string to be read (null if drop frame) |
okini3939 | 0:236a084b1d6b | 156 | * |
okini3939 | 0:236a084b1d6b | 157 | * @return true if a string has been read, false otherwise |
okini3939 | 0:236a084b1d6b | 158 | */ |
okini3939 | 0:236a084b1d6b | 159 | bool read(char * message); |
okini3939 | 0:236a084b1d6b | 160 | |
okini3939 | 0:236a084b1d6b | 161 | /** |
okini3939 | 0:236a084b1d6b | 162 | * To see if there is a websocket connection active |
okini3939 | 0:236a084b1d6b | 163 | * |
okini3939 | 0:236a084b1d6b | 164 | * @return true if there is a connection active |
okini3939 | 0:236a084b1d6b | 165 | */ |
okini3939 | 0:236a084b1d6b | 166 | bool connected(); |
okini3939 | 0:236a084b1d6b | 167 | |
okini3939 | 0:236a084b1d6b | 168 | /** |
okini3939 | 0:236a084b1d6b | 169 | * Close the websocket connection |
okini3939 | 0:236a084b1d6b | 170 | * |
okini3939 | 0:236a084b1d6b | 171 | * @return true if the connection has been closed, false otherwise |
okini3939 | 0:236a084b1d6b | 172 | */ |
okini3939 | 0:236a084b1d6b | 173 | bool close(); |
okini3939 | 0:236a084b1d6b | 174 | |
okini3939 | 0:236a084b1d6b | 175 | /** |
okini3939 | 0:236a084b1d6b | 176 | * Accessor: get path from the websocket url |
okini3939 | 0:236a084b1d6b | 177 | * |
okini3939 | 0:236a084b1d6b | 178 | * @return path |
okini3939 | 0:236a084b1d6b | 179 | */ |
okini3939 | 0:236a084b1d6b | 180 | std::string getPath(); |
okini3939 | 0:236a084b1d6b | 181 | |
okini3939 | 0:236a084b1d6b | 182 | enum Type { |
okini3939 | 0:236a084b1d6b | 183 | WIF, |
okini3939 | 0:236a084b1d6b | 184 | ETH |
okini3939 | 0:236a084b1d6b | 185 | }; |
okini3939 | 0:236a084b1d6b | 186 | |
okini3939 | 0:236a084b1d6b | 187 | |
okini3939 | 0:236a084b1d6b | 188 | private: |
okini3939 | 0:236a084b1d6b | 189 | |
okini3939 | 0:236a084b1d6b | 190 | |
okini3939 | 0:236a084b1d6b | 191 | void fillFields(char * url); |
okini3939 | 0:236a084b1d6b | 192 | void isr_dns (DNSReply r); |
okini3939 | 0:236a084b1d6b | 193 | |
okini3939 | 0:236a084b1d6b | 194 | std::string ip_domain; |
okini3939 | 0:236a084b1d6b | 195 | std::string path; |
okini3939 | 0:236a084b1d6b | 196 | std::string port; |
okini3939 | 0:236a084b1d6b | 197 | |
okini3939 | 0:236a084b1d6b | 198 | #ifdef WIFLY |
okini3939 | 0:236a084b1d6b | 199 | Wifly * wifi; |
okini3939 | 0:236a084b1d6b | 200 | #endif |
okini3939 | 0:236a084b1d6b | 201 | |
okini3939 | 0:236a084b1d6b | 202 | void onTCPSocketEvent(TCPSocketEvent e); |
okini3939 | 0:236a084b1d6b | 203 | bool eth_connected; |
okini3939 | 0:236a084b1d6b | 204 | bool eth_readable; |
okini3939 | 0:236a084b1d6b | 205 | bool eth_writeable; |
okini3939 | 0:236a084b1d6b | 206 | char eth_rx[512]; |
okini3939 | 0:236a084b1d6b | 207 | bool response_server_eth; |
okini3939 | 0:236a084b1d6b | 208 | bool new_msg; |
okini3939 | 0:236a084b1d6b | 209 | int dns_status; |
okini3939 | 0:236a084b1d6b | 210 | |
okini3939 | 0:236a084b1d6b | 211 | EthernetNetIf * eth; |
okini3939 | 0:236a084b1d6b | 212 | TCPSocket * sock; |
okini3939 | 0:236a084b1d6b | 213 | IpAddr * server_ip; |
okini3939 | 0:236a084b1d6b | 214 | |
okini3939 | 0:236a084b1d6b | 215 | Type netif; |
okini3939 | 0:236a084b1d6b | 216 | |
okini3939 | 0:236a084b1d6b | 217 | |
okini3939 | 0:236a084b1d6b | 218 | }; |
okini3939 | 0:236a084b1d6b | 219 | |
okini3939 | 0:236a084b1d6b | 220 | #endif |