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