fork of WebSocketClient with some fixes
Fork of WebSocketClient by
Websocket.h@0:10b6eaafc2da, 2012-08-10 (annotated)
- Committer:
- samux
- Date:
- Fri Aug 10 11:08:44 2012 +0000
- Revision:
- 0:10b6eaafc2da
- Child:
- 3:9589afa4712e
websocket client with new official mbed tcp/ip stack
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 0:10b6eaafc2da | 1 | /** |
samux | 0:10b6eaafc2da | 2 | * @author Samuel Mokrani |
samux | 0:10b6eaafc2da | 3 | * |
samux | 0:10b6eaafc2da | 4 | * @section LICENSE |
samux | 0:10b6eaafc2da | 5 | * |
samux | 0:10b6eaafc2da | 6 | * Copyright (c) 2011 mbed |
samux | 0:10b6eaafc2da | 7 | * |
samux | 0:10b6eaafc2da | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
samux | 0:10b6eaafc2da | 9 | * of this software and associated documentation files (the "Software"), to deal |
samux | 0:10b6eaafc2da | 10 | * in the Software without restriction, including without limitation the rights |
samux | 0:10b6eaafc2da | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
samux | 0:10b6eaafc2da | 12 | * copies of the Software, and to permit persons to whom the Software is |
samux | 0:10b6eaafc2da | 13 | * furnished to do so, subject to the following conditions: |
samux | 0:10b6eaafc2da | 14 | * |
samux | 0:10b6eaafc2da | 15 | * The above copyright notice and this permission notice shall be included in |
samux | 0:10b6eaafc2da | 16 | * all copies or substantial portions of the Software. |
samux | 0:10b6eaafc2da | 17 | * |
samux | 0:10b6eaafc2da | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
samux | 0:10b6eaafc2da | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
samux | 0:10b6eaafc2da | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
samux | 0:10b6eaafc2da | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
samux | 0:10b6eaafc2da | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
samux | 0:10b6eaafc2da | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
samux | 0:10b6eaafc2da | 24 | * THE SOFTWARE. |
samux | 0:10b6eaafc2da | 25 | * |
samux | 0:10b6eaafc2da | 26 | * @section DESCRIPTION |
samux | 0:10b6eaafc2da | 27 | * Simple websocket client |
samux | 0:10b6eaafc2da | 28 | * |
samux | 0:10b6eaafc2da | 29 | */ |
samux | 0:10b6eaafc2da | 30 | |
samux | 0:10b6eaafc2da | 31 | #ifndef WEBSOCKET_H |
samux | 0:10b6eaafc2da | 32 | #define WEBSOCKET_H |
samux | 0:10b6eaafc2da | 33 | |
samux | 0:10b6eaafc2da | 34 | #include "mbed.h" |
samux | 0:10b6eaafc2da | 35 | #include <string> |
samux | 0:10b6eaafc2da | 36 | |
samux | 0:10b6eaafc2da | 37 | #include "TCPSocketConnection.h" |
samux | 0:10b6eaafc2da | 38 | |
samux | 0:10b6eaafc2da | 39 | /** Websocket client Class. |
samux | 0:10b6eaafc2da | 40 | * |
samux | 0:10b6eaafc2da | 41 | * Example (ethernet network): |
samux | 0:10b6eaafc2da | 42 | * @code |
samux | 0:10b6eaafc2da | 43 | * #include "mbed.h" |
samux | 0:10b6eaafc2da | 44 | * #include "EthernetInterface.h" |
samux | 0:10b6eaafc2da | 45 | * #include "Websocket.h" |
samux | 0:10b6eaafc2da | 46 | * |
samux | 0:10b6eaafc2da | 47 | * int main() { |
samux | 0:10b6eaafc2da | 48 | * EthernetInterface eth; |
samux | 0:10b6eaafc2da | 49 | * eth.init(); //Use DHCP |
samux | 0:10b6eaafc2da | 50 | * eth.connect(); |
samux | 0:10b6eaafc2da | 51 | * printf("IP Address is %s\n\r", eth.getIPAddress()); |
samux | 0:10b6eaafc2da | 52 | * |
samux | 0:10b6eaafc2da | 53 | * Websocket ws("ws://sockets.mbed.org:443/ws/demo/rw"); |
samux | 0:10b6eaafc2da | 54 | * ws.connect(); |
samux | 0:10b6eaafc2da | 55 | * |
samux | 0:10b6eaafc2da | 56 | * while (1) { |
samux | 0:10b6eaafc2da | 57 | * int res = ws.send("WebSocket Hello World!"); |
samux | 0:10b6eaafc2da | 58 | * |
samux | 0:10b6eaafc2da | 59 | * if (ws.read(recv)) { |
samux | 0:10b6eaafc2da | 60 | * printf("rcv: %s\r\n", recv); |
samux | 0:10b6eaafc2da | 61 | * } |
samux | 0:10b6eaafc2da | 62 | * |
samux | 0:10b6eaafc2da | 63 | * wait(0.1); |
samux | 0:10b6eaafc2da | 64 | * } |
samux | 0:10b6eaafc2da | 65 | * } |
samux | 0:10b6eaafc2da | 66 | * @endcode |
samux | 0:10b6eaafc2da | 67 | */ |
samux | 0:10b6eaafc2da | 68 | |
samux | 0:10b6eaafc2da | 69 | class Websocket |
samux | 0:10b6eaafc2da | 70 | { |
samux | 0:10b6eaafc2da | 71 | public: |
samux | 0:10b6eaafc2da | 72 | /** |
samux | 0:10b6eaafc2da | 73 | * Constructor |
samux | 0:10b6eaafc2da | 74 | * |
samux | 0:10b6eaafc2da | 75 | * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80) |
samux | 0:10b6eaafc2da | 76 | */ |
samux | 0:10b6eaafc2da | 77 | Websocket(char * url); |
samux | 0:10b6eaafc2da | 78 | |
samux | 0:10b6eaafc2da | 79 | /** |
samux | 0:10b6eaafc2da | 80 | * Connect to the websocket url |
samux | 0:10b6eaafc2da | 81 | * |
samux | 0:10b6eaafc2da | 82 | *@return true if the connection is established, false otherwise |
samux | 0:10b6eaafc2da | 83 | */ |
samux | 0:10b6eaafc2da | 84 | bool connect(); |
samux | 0:10b6eaafc2da | 85 | |
samux | 0:10b6eaafc2da | 86 | /** |
samux | 0:10b6eaafc2da | 87 | * Send a string according to the websocket format (see rfc 6455) |
samux | 0:10b6eaafc2da | 88 | * |
samux | 0:10b6eaafc2da | 89 | * @param str string to be sent |
samux | 0:10b6eaafc2da | 90 | * |
samux | 0:10b6eaafc2da | 91 | * @returns the number of bytes sent |
samux | 0:10b6eaafc2da | 92 | */ |
samux | 0:10b6eaafc2da | 93 | int send(char * str); |
samux | 0:10b6eaafc2da | 94 | |
samux | 0:10b6eaafc2da | 95 | /** |
samux | 0:10b6eaafc2da | 96 | * Read a websocket message |
samux | 0:10b6eaafc2da | 97 | * |
samux | 0:10b6eaafc2da | 98 | * @param message pointer to the string to be read (null if drop frame) |
samux | 0:10b6eaafc2da | 99 | * |
samux | 0:10b6eaafc2da | 100 | * @return true if a websocket frame has been read |
samux | 0:10b6eaafc2da | 101 | */ |
samux | 0:10b6eaafc2da | 102 | bool read(char * message); |
samux | 0:10b6eaafc2da | 103 | |
samux | 0:10b6eaafc2da | 104 | /** |
samux | 0:10b6eaafc2da | 105 | * To see if there is a websocket connection active |
samux | 0:10b6eaafc2da | 106 | * |
samux | 0:10b6eaafc2da | 107 | * @return true if there is a connection active |
samux | 0:10b6eaafc2da | 108 | */ |
samux | 0:10b6eaafc2da | 109 | bool is_connected(); |
samux | 0:10b6eaafc2da | 110 | |
samux | 0:10b6eaafc2da | 111 | /** |
samux | 0:10b6eaafc2da | 112 | * Close the websocket connection |
samux | 0:10b6eaafc2da | 113 | * |
samux | 0:10b6eaafc2da | 114 | * @return true if the connection has been closed, false otherwise |
samux | 0:10b6eaafc2da | 115 | */ |
samux | 0:10b6eaafc2da | 116 | bool close(); |
samux | 0:10b6eaafc2da | 117 | |
samux | 0:10b6eaafc2da | 118 | /* |
samux | 0:10b6eaafc2da | 119 | * Accessor: get path from the websocket url |
samux | 0:10b6eaafc2da | 120 | * |
samux | 0:10b6eaafc2da | 121 | * @return path |
samux | 0:10b6eaafc2da | 122 | */ |
samux | 0:10b6eaafc2da | 123 | std::string getPath(); |
samux | 0:10b6eaafc2da | 124 | |
samux | 0:10b6eaafc2da | 125 | private: |
samux | 0:10b6eaafc2da | 126 | |
samux | 0:10b6eaafc2da | 127 | void fillFields(char * url); |
samux | 0:10b6eaafc2da | 128 | int sendOpcode(uint8_t opcode); |
samux | 0:10b6eaafc2da | 129 | int sendLength(uint32_t len); |
samux | 0:10b6eaafc2da | 130 | int sendMask(); |
samux | 0:10b6eaafc2da | 131 | int sendChar(char c); |
samux | 0:10b6eaafc2da | 132 | int readChar(char * pC, bool block = true); |
samux | 0:10b6eaafc2da | 133 | |
samux | 0:10b6eaafc2da | 134 | std::string ip_domain; |
samux | 0:10b6eaafc2da | 135 | std::string path; |
samux | 0:10b6eaafc2da | 136 | std::string port; |
samux | 0:10b6eaafc2da | 137 | |
samux | 0:10b6eaafc2da | 138 | TCPSocketConnection socket; |
samux | 0:10b6eaafc2da | 139 | |
samux | 0:10b6eaafc2da | 140 | int read(char * buf, int len, bool block = true); |
samux | 0:10b6eaafc2da | 141 | int write(char * buf, int len, uint32_t timeout=5000); |
samux | 0:10b6eaafc2da | 142 | }; |
samux | 0:10b6eaafc2da | 143 | |
samux | 0:10b6eaafc2da | 144 | #endif |