Websocket client. You can only use this class with a Wifly module. If you want to use websockets over ethernet, take a look at the Websocket class.

Committer:
samux
Date:
Thu Oct 20 09:41:56 2011 +0000
Revision:
2:06909bc328c0
Parent:
1:5dda5cd3caf2

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 0:08afdb5fb5ca 1 /**
samux 0:08afdb5fb5ca 2 * @author Samuel Mokrani
samux 0:08afdb5fb5ca 3 *
samux 0:08afdb5fb5ca 4 * @section LICENSE
samux 0:08afdb5fb5ca 5 *
samux 0:08afdb5fb5ca 6 * Copyright (c) 2011 mbed
samux 0:08afdb5fb5ca 7 *
samux 0:08afdb5fb5ca 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
samux 0:08afdb5fb5ca 9 * of this software and associated documentation files (the "Software"), to deal
samux 0:08afdb5fb5ca 10 * in the Software without restriction, including without limitation the rights
samux 0:08afdb5fb5ca 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
samux 0:08afdb5fb5ca 12 * copies of the Software, and to permit persons to whom the Software is
samux 0:08afdb5fb5ca 13 * furnished to do so, subject to the following conditions:
samux 0:08afdb5fb5ca 14 *
samux 0:08afdb5fb5ca 15 * The above copyright notice and this permission notice shall be included in
samux 0:08afdb5fb5ca 16 * all copies or substantial portions of the Software.
samux 0:08afdb5fb5ca 17 *
samux 0:08afdb5fb5ca 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
samux 0:08afdb5fb5ca 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
samux 0:08afdb5fb5ca 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
samux 0:08afdb5fb5ca 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
samux 0:08afdb5fb5ca 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 0:08afdb5fb5ca 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
samux 0:08afdb5fb5ca 24 * THE SOFTWARE.
samux 0:08afdb5fb5ca 25 *
samux 0:08afdb5fb5ca 26 * @section DESCRIPTION
samux 0:08afdb5fb5ca 27 * Simple websocket client
samux 0:08afdb5fb5ca 28 *
samux 0:08afdb5fb5ca 29 */
samux 0:08afdb5fb5ca 30
samux 0:08afdb5fb5ca 31 #ifndef WEBSOCKET_H
samux 0:08afdb5fb5ca 32 #define WEBSOCKET_H
samux 0:08afdb5fb5ca 33
samux 0:08afdb5fb5ca 34 #include "mbed.h"
samux 0:08afdb5fb5ca 35 #include "Wifly.h"
samux 0:08afdb5fb5ca 36 #include <string>
samux 0:08afdb5fb5ca 37
samux 0:08afdb5fb5ca 38
samux 0:08afdb5fb5ca 39 /** Websocket client Class.
samux 0:08afdb5fb5ca 40 *
samux 0:08afdb5fb5ca 41 * Warning: you must use a wifi module (Wifly RN131-C)
samux 0:08afdb5fb5ca 42 *
samux 0:08afdb5fb5ca 43 * Example (wifi network):
samux 0:08afdb5fb5ca 44 * @code
samux 0:08afdb5fb5ca 45 * #include "mbed.h"
samux 0:08afdb5fb5ca 46 * #include "Wifly.h"
samux 0:08afdb5fb5ca 47 * #include "Websocket.h"
samux 0:08afdb5fb5ca 48 *
samux 0:08afdb5fb5ca 49 * Serial pc(USBTX, USBRX);
samux 0:08afdb5fb5ca 50 * Wifly * wifly;
samux 0:08afdb5fb5ca 51 * Websocket * ws;
samux 0:08afdb5fb5ca 52 *
samux 0:08afdb5fb5ca 53 * int main()
samux 0:08afdb5fb5ca 54 * {
samux 0:08afdb5fb5ca 55 * wifly = new Wifly(p9, p10, p20, "network", "password", true);
samux 0:08afdb5fb5ca 56 * ws = new Websocket("ws://ip_domain/path", wifly);
samux 0:08afdb5fb5ca 57 *
samux 2:06909bc328c0 58 * if(wifly->join())
samux 0:08afdb5fb5ca 59 * {
samux 0:08afdb5fb5ca 60 * if(ws->connect())
samux 0:08afdb5fb5ca 61 * {
samux 0:08afdb5fb5ca 62 * pc.printf("ws connected\r\n");
samux 0:08afdb5fb5ca 63 * while(1)
samux 0:08afdb5fb5ca 64 * {
samux 0:08afdb5fb5ca 65 * wait(0.1);
samux 2:06909bc328c0 66 * ws->send("test");
samux 0:08afdb5fb5ca 67 * }
samux 0:08afdb5fb5ca 68 * }
samux 0:08afdb5fb5ca 69 * else
samux 0:08afdb5fb5ca 70 * pc.printf("ws not connected\r\n");
samux 0:08afdb5fb5ca 71 * }
samux 0:08afdb5fb5ca 72 * else
samux 0:08afdb5fb5ca 73 * pc.printf("join network failed\r\n");
samux 0:08afdb5fb5ca 74 *
samux 0:08afdb5fb5ca 75 * }
samux 0:08afdb5fb5ca 76 * @endcode
samux 0:08afdb5fb5ca 77 *
samux 0:08afdb5fb5ca 78 *
samux 0:08afdb5fb5ca 79 */
samux 0:08afdb5fb5ca 80 class Websocket
samux 0:08afdb5fb5ca 81 {
samux 0:08afdb5fb5ca 82 public:
samux 0:08afdb5fb5ca 83 /**
samux 1:5dda5cd3caf2 84 * Constructor
samux 0:08afdb5fb5ca 85 *
samux 0:08afdb5fb5ca 86 * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80)
samux 0:08afdb5fb5ca 87 * @param wifi pointer to a wifi module (the communication will be establish by this module)
samux 0:08afdb5fb5ca 88 */
samux 0:08afdb5fb5ca 89 Websocket(char * url, Wifly * wifi);
samux 0:08afdb5fb5ca 90
samux 0:08afdb5fb5ca 91
samux 0:08afdb5fb5ca 92 /**
samux 0:08afdb5fb5ca 93 * Connect to the websocket url
samux 0:08afdb5fb5ca 94 *
samux 0:08afdb5fb5ca 95 *@return true if the connection is established, false otherwise
samux 0:08afdb5fb5ca 96 */
samux 0:08afdb5fb5ca 97 bool connect();
samux 0:08afdb5fb5ca 98
samux 0:08afdb5fb5ca 99 /**
samux 1:5dda5cd3caf2 100 * Send "str" according to the websocket format: 00 str ff
samux 0:08afdb5fb5ca 101 *
samux 0:08afdb5fb5ca 102 * @param str string to be sent
samux 0:08afdb5fb5ca 103 */
samux 0:08afdb5fb5ca 104 void send(char * str);
samux 0:08afdb5fb5ca 105
samux 0:08afdb5fb5ca 106 /**
samux 0:08afdb5fb5ca 107 * Read a websocket message
samux 0:08afdb5fb5ca 108 *
samux 0:08afdb5fb5ca 109 * @param message pointer to the string to be read (null if drop frame)
samux 0:08afdb5fb5ca 110 *
samux 0:08afdb5fb5ca 111 * @return true if a string has been read, false otherwise
samux 0:08afdb5fb5ca 112 */
samux 0:08afdb5fb5ca 113 bool read(char * message);
samux 0:08afdb5fb5ca 114
samux 0:08afdb5fb5ca 115 /**
samux 0:08afdb5fb5ca 116 * To see if there is a websocket connection active
samux 0:08afdb5fb5ca 117 *
samux 0:08afdb5fb5ca 118 * @return true if there is a connection active
samux 0:08afdb5fb5ca 119 */
samux 0:08afdb5fb5ca 120 bool connected();
samux 0:08afdb5fb5ca 121
samux 0:08afdb5fb5ca 122 /**
samux 0:08afdb5fb5ca 123 * Close the websocket connection
samux 0:08afdb5fb5ca 124 *
samux 0:08afdb5fb5ca 125 * @return true if the connection has been closed, false otherwise
samux 0:08afdb5fb5ca 126 */
samux 0:08afdb5fb5ca 127 bool close();
samux 0:08afdb5fb5ca 128
samux 0:08afdb5fb5ca 129 /**
samux 0:08afdb5fb5ca 130 * Accessor: get path from the websocket url
samux 0:08afdb5fb5ca 131 *
samux 0:08afdb5fb5ca 132 * @return path
samux 0:08afdb5fb5ca 133 */
samux 0:08afdb5fb5ca 134 std::string getPath();
samux 0:08afdb5fb5ca 135
samux 0:08afdb5fb5ca 136 enum Type {
samux 0:08afdb5fb5ca 137 WIF,
samux 0:08afdb5fb5ca 138 ETH
samux 0:08afdb5fb5ca 139 };
samux 0:08afdb5fb5ca 140
samux 0:08afdb5fb5ca 141
samux 0:08afdb5fb5ca 142 private:
samux 0:08afdb5fb5ca 143
samux 0:08afdb5fb5ca 144
samux 0:08afdb5fb5ca 145 void fillFields(char * url);
samux 0:08afdb5fb5ca 146
samux 0:08afdb5fb5ca 147 std::string ip_domain;
samux 0:08afdb5fb5ca 148 std::string path;
samux 0:08afdb5fb5ca 149 std::string port;
samux 0:08afdb5fb5ca 150
samux 0:08afdb5fb5ca 151 Wifly * wifi;
samux 0:08afdb5fb5ca 152 Type netif;
samux 0:08afdb5fb5ca 153
samux 0:08afdb5fb5ca 154
samux 0:08afdb5fb5ca 155 };
samux 0:08afdb5fb5ca 156
samux 0:08afdb5fb5ca 157 #endif