Simple websocket client

Dependents:   Websocket_Ethernet_HelloWorld Websocket_Wifly_HelloWorld RPC_Wifly_HelloWorld RPC_Ethernet_HelloWorld ... more

Committer:
samux
Date:
Fri Feb 08 12:33:04 2013 +0000
Revision:
7:4567996414a5
Parent:
4:466f90b7849a
reduce timeout

Who changed what in which revision?

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