fork of WebSocketClient with some fixes
Fork of WebSocketClient by
Websocket.h@8:ccedee13be8d, 2013-10-23 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Oct 23 09:58:39 2013 +0000
- Revision:
- 8:ccedee13be8d
- Parent:
- 6:86e89a0369b9
- Child:
- 9:efa2c147bee1
Apache V2 License
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 8:ccedee13be8d | 1 | /* mbed Microcontroller Library |
mbed_official | 8:ccedee13be8d | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 8:ccedee13be8d | 3 | * |
mbed_official | 8:ccedee13be8d | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 8:ccedee13be8d | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 8:ccedee13be8d | 6 | * You may obtain a copy of the License at |
mbed_official | 8:ccedee13be8d | 7 | * |
mbed_official | 8:ccedee13be8d | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 8:ccedee13be8d | 9 | * |
mbed_official | 8:ccedee13be8d | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 8:ccedee13be8d | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 8:ccedee13be8d | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 8:ccedee13be8d | 13 | * See the License for the specific language governing permissions and |
mbed_official | 8:ccedee13be8d | 14 | * limitations under the License. |
mbed_official | 8:ccedee13be8d | 15 | */ |
samux | 4:466f90b7849a | 16 | #ifndef WEBSOCKET_H |
samux | 4:466f90b7849a | 17 | #define WEBSOCKET_H |
samux | 4:466f90b7849a | 18 | |
samux | 4:466f90b7849a | 19 | #include "mbed.h" |
samux | 4:466f90b7849a | 20 | |
samux | 4:466f90b7849a | 21 | #include "TCPSocketConnection.h" |
samux | 4:466f90b7849a | 22 | |
samux | 4:466f90b7849a | 23 | /** Websocket client Class. |
samux | 4:466f90b7849a | 24 | * |
samux | 4:466f90b7849a | 25 | * Example (ethernet network): |
samux | 4:466f90b7849a | 26 | * @code |
samux | 4:466f90b7849a | 27 | * #include "mbed.h" |
samux | 4:466f90b7849a | 28 | * #include "EthernetInterface.h" |
samux | 4:466f90b7849a | 29 | * #include "Websocket.h" |
samux | 4:466f90b7849a | 30 | * |
samux | 4:466f90b7849a | 31 | * int main() { |
samux | 4:466f90b7849a | 32 | * EthernetInterface eth; |
samux | 4:466f90b7849a | 33 | * eth.init(); //Use DHCP |
samux | 4:466f90b7849a | 34 | * eth.connect(); |
samux | 4:466f90b7849a | 35 | * printf("IP Address is %s\n\r", eth.getIPAddress()); |
samux | 4:466f90b7849a | 36 | * |
samux | 4:466f90b7849a | 37 | * Websocket ws("ws://sockets.mbed.org:443/ws/demo/rw"); |
samux | 4:466f90b7849a | 38 | * ws.connect(); |
samux | 4:466f90b7849a | 39 | * |
samux | 4:466f90b7849a | 40 | * while (1) { |
samux | 4:466f90b7849a | 41 | * int res = ws.send("WebSocket Hello World!"); |
samux | 4:466f90b7849a | 42 | * |
samux | 4:466f90b7849a | 43 | * if (ws.read(recv)) { |
samux | 4:466f90b7849a | 44 | * printf("rcv: %s\r\n", recv); |
samux | 4:466f90b7849a | 45 | * } |
samux | 4:466f90b7849a | 46 | * |
samux | 4:466f90b7849a | 47 | * wait(0.1); |
samux | 4:466f90b7849a | 48 | * } |
samux | 4:466f90b7849a | 49 | * } |
samux | 4:466f90b7849a | 50 | * @endcode |
samux | 4:466f90b7849a | 51 | */ |
samux | 4:466f90b7849a | 52 | |
samux | 4:466f90b7849a | 53 | class Websocket |
samux | 4:466f90b7849a | 54 | { |
samux | 4:466f90b7849a | 55 | public: |
samux | 4:466f90b7849a | 56 | /** |
samux | 4:466f90b7849a | 57 | * Constructor |
samux | 4:466f90b7849a | 58 | * |
samux | 4:466f90b7849a | 59 | * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80) |
samux | 4:466f90b7849a | 60 | */ |
samux | 4:466f90b7849a | 61 | Websocket(char * url); |
samux | 4:466f90b7849a | 62 | |
samux | 4:466f90b7849a | 63 | /** |
samux | 4:466f90b7849a | 64 | * Connect to the websocket url |
samux | 4:466f90b7849a | 65 | * |
samux | 4:466f90b7849a | 66 | *@return true if the connection is established, false otherwise |
samux | 4:466f90b7849a | 67 | */ |
samux | 4:466f90b7849a | 68 | bool connect(); |
samux | 4:466f90b7849a | 69 | |
samux | 4:466f90b7849a | 70 | /** |
samux | 4:466f90b7849a | 71 | * Send a string according to the websocket format (see rfc 6455) |
samux | 4:466f90b7849a | 72 | * |
samux | 4:466f90b7849a | 73 | * @param str string to be sent |
samux | 4:466f90b7849a | 74 | * |
samux | 4:466f90b7849a | 75 | * @returns the number of bytes sent |
samux | 4:466f90b7849a | 76 | */ |
samux | 4:466f90b7849a | 77 | int send(char * str); |
samux | 4:466f90b7849a | 78 | |
samux | 4:466f90b7849a | 79 | /** |
samux | 4:466f90b7849a | 80 | * Read a websocket message |
samux | 4:466f90b7849a | 81 | * |
samux | 4:466f90b7849a | 82 | * @param message pointer to the string to be read (null if drop frame) |
samux | 4:466f90b7849a | 83 | * |
samux | 4:466f90b7849a | 84 | * @return true if a websocket frame has been read |
samux | 4:466f90b7849a | 85 | */ |
samux | 4:466f90b7849a | 86 | bool read(char * message); |
samux | 4:466f90b7849a | 87 | |
samux | 4:466f90b7849a | 88 | /** |
samux | 4:466f90b7849a | 89 | * To see if there is a websocket connection active |
samux | 4:466f90b7849a | 90 | * |
samux | 4:466f90b7849a | 91 | * @return true if there is a connection active |
samux | 4:466f90b7849a | 92 | */ |
samux | 4:466f90b7849a | 93 | bool is_connected(); |
samux | 4:466f90b7849a | 94 | |
samux | 4:466f90b7849a | 95 | /** |
samux | 4:466f90b7849a | 96 | * Close the websocket connection |
samux | 4:466f90b7849a | 97 | * |
samux | 4:466f90b7849a | 98 | * @return true if the connection has been closed, false otherwise |
samux | 4:466f90b7849a | 99 | */ |
samux | 4:466f90b7849a | 100 | bool close(); |
samux | 4:466f90b7849a | 101 | |
samux | 4:466f90b7849a | 102 | /* |
samux | 4:466f90b7849a | 103 | * Accessor: get path from the websocket url |
samux | 4:466f90b7849a | 104 | * |
samux | 4:466f90b7849a | 105 | * @return path |
samux | 4:466f90b7849a | 106 | */ |
donatien | 6:86e89a0369b9 | 107 | char* getPath(); |
samux | 4:466f90b7849a | 108 | |
samux | 4:466f90b7849a | 109 | private: |
samux | 4:466f90b7849a | 110 | |
samux | 4:466f90b7849a | 111 | void fillFields(char * url); |
donatien | 6:86e89a0369b9 | 112 | int parseURL(const char* url, char* scheme, size_t maxSchemeLen, char* host, size_t maxHostLen, uint16_t* port, char* path, size_t maxPathLen); //Parse URL |
samux | 4:466f90b7849a | 113 | int sendOpcode(uint8_t opcode, char * msg); |
samux | 4:466f90b7849a | 114 | int sendLength(uint32_t len, char * msg); |
samux | 4:466f90b7849a | 115 | int sendMask(char * msg); |
samux | 4:466f90b7849a | 116 | int readChar(char * pC, bool block = true); |
donatien | 6:86e89a0369b9 | 117 | |
donatien | 6:86e89a0369b9 | 118 | char scheme[8]; |
donatien | 6:86e89a0369b9 | 119 | uint16_t port; |
donatien | 6:86e89a0369b9 | 120 | char host[32]; |
donatien | 6:86e89a0369b9 | 121 | char path[64]; |
samux | 4:466f90b7849a | 122 | |
samux | 4:466f90b7849a | 123 | TCPSocketConnection socket; |
samux | 4:466f90b7849a | 124 | |
samux | 4:466f90b7849a | 125 | int read(char * buf, int len, int min_len = -1); |
samux | 4:466f90b7849a | 126 | int write(char * buf, int len); |
samux | 4:466f90b7849a | 127 | }; |
samux | 4:466f90b7849a | 128 | |
samux | 4:466f90b7849a | 129 | #endif |