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