update
Websocket.h
- Committer:
- mbed_official
- Date:
- 2013-10-23
- Revision:
- 8:ccedee13be8d
- Parent:
- 6:86e89a0369b9
- Child:
- 9:efa2c147bee1
File content as of revision 8:ccedee13be8d:
/* mbed Microcontroller Library * Copyright (c) 2006-2013 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef WEBSOCKET_H #define WEBSOCKET_H #include "mbed.h" #include "TCPSocketConnection.h" /** Websocket client Class. * * Example (ethernet network): * @code * #include "mbed.h" * #include "EthernetInterface.h" * #include "Websocket.h" * * int main() { * EthernetInterface eth; * eth.init(); //Use DHCP * eth.connect(); * printf("IP Address is %s\n\r", eth.getIPAddress()); * * Websocket ws("ws://sockets.mbed.org:443/ws/demo/rw"); * ws.connect(); * * while (1) { * int res = ws.send("WebSocket Hello World!"); * * if (ws.read(recv)) { * printf("rcv: %s\r\n", recv); * } * * wait(0.1); * } * } * @endcode */ class Websocket { public: /** * Constructor * * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80) */ Websocket(char * url); /** * Connect to the websocket url * *@return true if the connection is established, false otherwise */ bool connect(); /** * Send a string according to the websocket format (see rfc 6455) * * @param str string to be sent * * @returns the number of bytes sent */ int send(char * str); /** * Read a websocket message * * @param message pointer to the string to be read (null if drop frame) * * @return true if a websocket frame has been read */ bool read(char * message); /** * To see if there is a websocket connection active * * @return true if there is a connection active */ bool is_connected(); /** * Close the websocket connection * * @return true if the connection has been closed, false otherwise */ bool close(); /* * Accessor: get path from the websocket url * * @return path */ char* getPath(); private: void fillFields(char * url); 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 int sendOpcode(uint8_t opcode, char * msg); int sendLength(uint32_t len, char * msg); int sendMask(char * msg); int readChar(char * pC, bool block = true); char scheme[8]; uint16_t port; char host[32]; char path[64]; TCPSocketConnection socket; int read(char * buf, int len, int min_len = -1); int write(char * buf, int len); }; #endif