WebSocket client library
Websocket.h@17:1e339933c97a, 2011-09-22 (annotated)
- Committer:
- samux
- Date:
- Thu Sep 22 10:09:12 2011 +0000
- Revision:
- 17:1e339933c97a
- Parent:
- 16:d4518b50f653
- Child:
- 18:ef44ea603938
Who changed what in which revision?
User | Revision | Line number | New 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 | 6:01a1eb7c0145 | 38 | #include "EthernetNetIf.h" |
samux | 6:01a1eb7c0145 | 39 | #include "TCPSocket.h" |
samux | 6:01a1eb7c0145 | 40 | #include "dnsresolve.h" |
samux | 6:01a1eb7c0145 | 41 | |
samux | 0:21fe3b05249f | 42 | /** Websocket client Class. |
samux | 0:21fe3b05249f | 43 | * |
samux | 13:3b058372cad9 | 44 | * Warning: you must use a wifi module (Wifly RN131-C) or an ethernet network to use this class |
samux | 0:21fe3b05249f | 45 | * |
samux | 13:3b058372cad9 | 46 | * Example (wifi network): |
samux | 0:21fe3b05249f | 47 | * @code |
samux | 0:21fe3b05249f | 48 | * #include "mbed.h" |
samux | 0:21fe3b05249f | 49 | * #include "Wifly.h" |
samux | 0:21fe3b05249f | 50 | * #include "Websocket.h" |
samux | 0:21fe3b05249f | 51 | * |
samux | 0:21fe3b05249f | 52 | * Serial pc(USBTX, USBRX); |
samux | 0:21fe3b05249f | 53 | * Wifly * wifly; |
samux | 0:21fe3b05249f | 54 | * Websocket * ws; |
samux | 0:21fe3b05249f | 55 | * |
samux | 0:21fe3b05249f | 56 | * int main() |
samux | 0:21fe3b05249f | 57 | * { |
samux | 0:21fe3b05249f | 58 | * wifly = new Wifly(p9, p10, p20, "network", "password", true); |
samux | 13:3b058372cad9 | 59 | * ws = new Websocket("ws://ip_domain/path", wifly); |
samux | 0:21fe3b05249f | 60 | * |
samux | 0:21fe3b05249f | 61 | * if(wifly->Join()) |
samux | 0:21fe3b05249f | 62 | * { |
samux | 0:21fe3b05249f | 63 | * if(ws->connect()) |
samux | 0:21fe3b05249f | 64 | * { |
samux | 0:21fe3b05249f | 65 | * pc.printf("ws connected\r\n"); |
samux | 0:21fe3b05249f | 66 | * while(1) |
samux | 0:21fe3b05249f | 67 | * { |
samux | 0:21fe3b05249f | 68 | * wait(0.1); |
samux | 0:21fe3b05249f | 69 | * ws->Send("test"); |
samux | 0:21fe3b05249f | 70 | * } |
samux | 0:21fe3b05249f | 71 | * } |
samux | 0:21fe3b05249f | 72 | * else |
samux | 0:21fe3b05249f | 73 | * pc.printf("ws not connected\r\n"); |
samux | 0:21fe3b05249f | 74 | * } |
samux | 0:21fe3b05249f | 75 | * else |
samux | 0:21fe3b05249f | 76 | * pc.printf("join network failed\r\n"); |
samux | 0:21fe3b05249f | 77 | * |
samux | 0:21fe3b05249f | 78 | * } |
samux | 0:21fe3b05249f | 79 | * @endcode |
samux | 13:3b058372cad9 | 80 | * |
samux | 13:3b058372cad9 | 81 | * |
samux | 13:3b058372cad9 | 82 | * |
samux | 13:3b058372cad9 | 83 | * Example (ethernet network): |
samux | 13:3b058372cad9 | 84 | * @code |
samux | 13:3b058372cad9 | 85 | * #include "mbed.h" |
samux | 13:3b058372cad9 | 86 | * #include "Websocket.h" |
samux | 13:3b058372cad9 | 87 | * |
samux | 13:3b058372cad9 | 88 | * Serial pc(USBTX, USBRX); |
samux | 13:3b058372cad9 | 89 | * Websocket * ws; |
samux | 13:3b058372cad9 | 90 | * |
samux | 13:3b058372cad9 | 91 | * int main() |
samux | 13:3b058372cad9 | 92 | * { |
samux | 13:3b058372cad9 | 93 | * ws = new Websocket("ws://ip_domain/path"); |
samux | 13:3b058372cad9 | 94 | * |
samux | 13:3b058372cad9 | 95 | * if(ws->connect()) |
samux | 13:3b058372cad9 | 96 | * { |
samux | 13:3b058372cad9 | 97 | * pc.printf("ws connected\r\n"); |
samux | 13:3b058372cad9 | 98 | * while(1) |
samux | 13:3b058372cad9 | 99 | * { |
samux | 13:3b058372cad9 | 100 | * wait(0.1); |
samux | 13:3b058372cad9 | 101 | * ws->Send("test"); |
samux | 13:3b058372cad9 | 102 | * } |
samux | 13:3b058372cad9 | 103 | * } |
samux | 13:3b058372cad9 | 104 | * else |
samux | 13:3b058372cad9 | 105 | * pc.printf("ws not connected\r\n"); |
samux | 13:3b058372cad9 | 106 | * } |
samux | 13:3b058372cad9 | 107 | * @endcode |
samux | 0:21fe3b05249f | 108 | */ |
samux | 0:21fe3b05249f | 109 | class Websocket |
samux | 0:21fe3b05249f | 110 | { |
samux | 0:21fe3b05249f | 111 | public: |
samux | 0:21fe3b05249f | 112 | /** |
samux | 6:01a1eb7c0145 | 113 | * Constructor for a wifi module |
samux | 0:21fe3b05249f | 114 | * |
samux | 6:01a1eb7c0145 | 115 | * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80) |
samux | 0:21fe3b05249f | 116 | * @param wifi pointer to a wifi module (the communication will be establish by this module) |
samux | 0:21fe3b05249f | 117 | */ |
samux | 6:01a1eb7c0145 | 118 | Websocket(char * url, Wifly * wifi); |
samux | 6:01a1eb7c0145 | 119 | |
samux | 6:01a1eb7c0145 | 120 | /** |
samux | 6:01a1eb7c0145 | 121 | * Constructor for an ethernet communication |
samux | 6:01a1eb7c0145 | 122 | * |
samux | 6:01a1eb7c0145 | 123 | * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80) |
samux | 6:01a1eb7c0145 | 124 | */ |
samux | 6:01a1eb7c0145 | 125 | Websocket(char * url); |
samux | 0:21fe3b05249f | 126 | |
samux | 0:21fe3b05249f | 127 | /** |
samux | 9:9fa055ed54b4 | 128 | * Connect to the websocket url |
samux | 0:21fe3b05249f | 129 | * |
samux | 1:0bb1153de91e | 130 | *@return true if the connection is established, false otherwise |
samux | 0:21fe3b05249f | 131 | */ |
samux | 0:21fe3b05249f | 132 | bool connect(); |
samux | 0:21fe3b05249f | 133 | |
samux | 0:21fe3b05249f | 134 | /** |
samux | 5:0f3422b6ca44 | 135 | * Send a string according to the websocket format: \x00 str \xff |
samux | 0:21fe3b05249f | 136 | * |
samux | 5:0f3422b6ca44 | 137 | * @param str string to be sent |
samux | 0:21fe3b05249f | 138 | */ |
samux | 0:21fe3b05249f | 139 | void Send(char * str); |
samux | 0:21fe3b05249f | 140 | |
samux | 0:21fe3b05249f | 141 | /** |
samux | 0:21fe3b05249f | 142 | * Read a websocket message |
samux | 0:21fe3b05249f | 143 | * |
samux | 0:21fe3b05249f | 144 | * @param message pointer to the string to be read (null if drop frame) |
samux | 0:21fe3b05249f | 145 | * |
samux | 0:21fe3b05249f | 146 | * @return true if a string has been read, false otherwise |
samux | 0:21fe3b05249f | 147 | */ |
samux | 0:21fe3b05249f | 148 | bool read(char * message); |
samux | 0:21fe3b05249f | 149 | |
samux | 0:21fe3b05249f | 150 | /** |
samux | 12:1f6b9451a608 | 151 | * To see if there is a websocket connection active |
samux | 0:21fe3b05249f | 152 | * |
samux | 12:1f6b9451a608 | 153 | * @return true if there is a connection active |
samux | 0:21fe3b05249f | 154 | */ |
samux | 0:21fe3b05249f | 155 | bool connected(); |
samux | 0:21fe3b05249f | 156 | |
samux | 0:21fe3b05249f | 157 | /** |
samux | 0:21fe3b05249f | 158 | * Close the websocket connection |
samux | 0:21fe3b05249f | 159 | * |
samux | 1:0bb1153de91e | 160 | * @return true if the connection has been closed, false otherwise |
samux | 0:21fe3b05249f | 161 | */ |
samux | 0:21fe3b05249f | 162 | bool close(); |
samux | 6:01a1eb7c0145 | 163 | |
samux | 17:1e339933c97a | 164 | /** |
samux | 17:1e339933c97a | 165 | * Accessor: get path from the websocket url |
samux | 17:1e339933c97a | 166 | * |
samux | 17:1e339933c97a | 167 | * @return path |
samux | 17:1e339933c97a | 168 | */ |
samux | 17:1e339933c97a | 169 | std::string getPath(); |
samux | 17:1e339933c97a | 170 | |
samux | 16:d4518b50f653 | 171 | enum Type { |
samux | 16:d4518b50f653 | 172 | WIF, |
samux | 16:d4518b50f653 | 173 | ETH |
samux | 16:d4518b50f653 | 174 | }; |
samux | 6:01a1eb7c0145 | 175 | |
samux | 0:21fe3b05249f | 176 | |
samux | 0:21fe3b05249f | 177 | private: |
samux | 16:d4518b50f653 | 178 | |
samux | 6:01a1eb7c0145 | 179 | |
samux | 16:d4518b50f653 | 180 | void fillFields(char * url); |
samux | 6:01a1eb7c0145 | 181 | |
samux | 17:1e339933c97a | 182 | std::string ip_domain; |
samux | 17:1e339933c97a | 183 | std::string path; |
samux | 17:1e339933c97a | 184 | std::string port; |
samux | 6:01a1eb7c0145 | 185 | |
samux | 0:21fe3b05249f | 186 | Wifly * wifi; |
samux | 6:01a1eb7c0145 | 187 | |
samux | 16:d4518b50f653 | 188 | void onTCPSocketEvent(TCPSocketEvent e); |
samux | 6:01a1eb7c0145 | 189 | bool eth_connected; |
samux | 12:1f6b9451a608 | 190 | bool eth_readable; |
samux | 12:1f6b9451a608 | 191 | bool eth_writeable; |
samux | 9:9fa055ed54b4 | 192 | char eth_rx[512]; |
samux | 9:9fa055ed54b4 | 193 | bool response_server_eth; |
samux | 16:d4518b50f653 | 194 | bool new_msg; |
samux | 6:01a1eb7c0145 | 195 | |
samux | 16:d4518b50f653 | 196 | EthernetNetIf * eth; |
samux | 16:d4518b50f653 | 197 | TCPSocket * sock; |
samux | 16:d4518b50f653 | 198 | IpAddr * server_ip; |
samux | 16:d4518b50f653 | 199 | |
samux | 16:d4518b50f653 | 200 | Type netif; |
samux | 16:d4518b50f653 | 201 | |
samux | 0:21fe3b05249f | 202 | |
samux | 0:21fe3b05249f | 203 | }; |
samux | 0:21fe3b05249f | 204 | |
samux | 0:21fe3b05249f | 205 | #endif |