SocketIO is a compatibility library on top of the MBED Websockets API. Currently the broadcast paradigm is supported. There is much more to the SocketIO spec that could be built into this API. Have fun!

Dependents:   df-2013-thermostat-handson df-2013-minihack-thermostat-complete df-2013-minihack-thermostat df-2013-thermostat-remotes ... more

Committer:
ansond
Date:
Mon Jul 08 02:43:43 2013 +0000
Revision:
2:2979735cb379
Parent:
0:5e68215d973a
Child:
10:997b8c5d2d46
header update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:5e68215d973a 1 /**
ansond 0:5e68215d973a 2 * @author Doug Anson
ansond 0:5e68215d973a 3 *
ansond 0:5e68215d973a 4 * @section LICENSE
ansond 0:5e68215d973a 5 *
ansond 0:5e68215d973a 6 * Copyright (c) 2013 mbed
ansond 0:5e68215d973a 7 *
ansond 0:5e68215d973a 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
ansond 0:5e68215d973a 9 * of this software and associated documentation files (the "Software"), to deal
ansond 0:5e68215d973a 10 * in the Software without restriction, including without limitation the rights
ansond 0:5e68215d973a 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
ansond 0:5e68215d973a 12 * copies of the Software, and to permit persons to whom the Software is
ansond 0:5e68215d973a 13 * furnished to do so, subject to the following conditions:
ansond 0:5e68215d973a 14 *
ansond 0:5e68215d973a 15 * The above copyright notice and this permission notice shall be included in
ansond 0:5e68215d973a 16 * all copies or substantial portions of the Software.
ansond 0:5e68215d973a 17 *
ansond 0:5e68215d973a 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
ansond 0:5e68215d973a 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
ansond 0:5e68215d973a 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
ansond 0:5e68215d973a 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
ansond 0:5e68215d973a 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ansond 0:5e68215d973a 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
ansond 0:5e68215d973a 24 * THE SOFTWARE.
ansond 0:5e68215d973a 25 *
ansond 0:5e68215d973a 26 * @section DESCRIPTION
ansond 0:5e68215d973a 27 * Simple SocketIO client library
ansond 0:5e68215d973a 28 *
ansond 0:5e68215d973a 29 */
ansond 0:5e68215d973a 30
ansond 0:5e68215d973a 31 #ifndef SOCKETIO_H
ansond 0:5e68215d973a 32 #define SOCKETIO_H
ansond 0:5e68215d973a 33
ansond 0:5e68215d973a 34 #include "mbed.h"
ansond 0:5e68215d973a 35
ansond 0:5e68215d973a 36 // Default SocketIO version
ansond 0:5e68215d973a 37 #define DEFAULT_VERSION 1
ansond 0:5e68215d973a 38
ansond 0:5e68215d973a 39 // HTTP support for session key retrieval
ansond 0:5e68215d973a 40 #include "HTTPClient.h"
ansond 0:5e68215d973a 41
ansond 0:5e68215d973a 42 // WebSocket layer support
ansond 0:5e68215d973a 43 #include "Websocket.h"
ansond 0:5e68215d973a 44
ansond 0:5e68215d973a 45 /** SocketIO client Class.
ansond 0:5e68215d973a 46 *
ansond 0:5e68215d973a 47 * Example (ethernet network):
ansond 0:5e68215d973a 48 * @code
ansond 0:5e68215d973a 49 * #include "mbed.h"
ansond 0:5e68215d973a 50 * #include "WiflyInterface.h"
ansond 0:5e68215d973a 51 * #include "SocketIO.h"
ansond 0:5e68215d973a 52 *
ansond 0:5e68215d973a 53 * // our wifi interface
ansond 0:5e68215d973a 54 * WiflyInterface wifly(p9, p10, p30, p29, "mysid", "mypw", WPA);
ansond 0:5e68215d973a 55 *
ansond 0:5e68215d973a 56 * int main() {
ansond 0:5e68215d973a 57 * wifly.init(); //Use DHCP
ansond 0:5e68215d973a 58 * wifly.connect();
ansond 0:5e68215d973a 59 * printf("IP Address is %s\n\r", wifly.getIPAddress());
ansond 0:5e68215d973a 60 *
ansond 0:5e68215d973a 61 * SocketIO socketio("myapp.herokuapp.com");
ansond 0:5e68215d973a 62 * socketio.connect();
ansond 0:5e68215d973a 63 *
ansond 0:5e68215d973a 64 * char recv[256];
ansond 0:5e68215d973a 65 * while (1) {
ansond 0:5e68215d973a 66 * int res = socketio.emit("mesage_name","[\"SocketIO Hello World!\"]");
ansond 0:5e68215d973a 67 * if (socketio.read(recv)) {
ansond 0:5e68215d973a 68 * printf("rcv: %s\r\n", recv);
ansond 0:5e68215d973a 69 * }
ansond 0:5e68215d973a 70 * wait(0.1);
ansond 0:5e68215d973a 71 * }
ansond 0:5e68215d973a 72 * }
ansond 0:5e68215d973a 73 * @endcode
ansond 0:5e68215d973a 74 */
ansond 0:5e68215d973a 75
ansond 0:5e68215d973a 76 class SocketIO
ansond 0:5e68215d973a 77 {
ansond 0:5e68215d973a 78 public:
ansond 0:5e68215d973a 79 /**
ansond 0:5e68215d973a 80 * Constructor
ansond 0:5e68215d973a 81 *
ansond 0:5e68215d973a 82 * @param url The SocketIO url in the form "www.example.com:[port]" (by default: port = 80) - i.e. just the endpoint name
ansond 0:5e68215d973a 83 */
ansond 0:5e68215d973a 84 SocketIO(char * url);
ansond 0:5e68215d973a 85
ansond 0:5e68215d973a 86 /**
ansond 0:5e68215d973a 87 * Constructor
ansond 0:5e68215d973a 88 *
ansond 0:5e68215d973a 89 * @param url The SocketIO url in the form "www.example.com:[port]" (by default: port = 80) - i.e. just the endpoint name
ansond 0:5e68215d973a 90 * @param version The SocketIO version for the session URL (by default version = 1)
ansond 0:5e68215d973a 91 */
ansond 0:5e68215d973a 92 SocketIO(char * url, int version);
ansond 0:5e68215d973a 93
ansond 0:5e68215d973a 94 /**
ansond 0:5e68215d973a 95 * Connect to the SocketIO url
ansond 0:5e68215d973a 96 *
ansond 0:5e68215d973a 97 *@return true if the connection is established, false otherwise
ansond 0:5e68215d973a 98 */
ansond 0:5e68215d973a 99 bool connect();
ansond 0:5e68215d973a 100
ansond 0:5e68215d973a 101 /**
ansond 0:5e68215d973a 102 * Emit (Broadcast) a socket.io message to the SocketIO server
ansond 0:5e68215d973a 103 *
ansond 0:5e68215d973a 104 * Socket.IO message Format (JSON): { "name": <name>, "args":<args> }
ansond 0:5e68215d973a 105 * name: the "name" of the socket.io message
ansond 0:5e68215d973a 106 * args: the argument(s) (must always be a JSON array) of the message. Example: "[ \"foo\", {\"bar\": \"none\"}]"
ansond 0:5e68215d973a 107 *
ansond 0:5e68215d973a 108 * @param name "name" of the socket.io message to broadcast
ansond 0:5e68215d973a 109 * @param args argument string to be sent ( must be in a JSON array format. Example: "[ \"foo\", {\"bar\": \"none\"}]" )
ansond 0:5e68215d973a 110 *
ansond 0:5e68215d973a 111 * @returns the number of bytes sent
ansond 0:5e68215d973a 112 */
ansond 0:5e68215d973a 113 int emit(char * name, char * args);
ansond 0:5e68215d973a 114
ansond 0:5e68215d973a 115 /**
ansond 0:5e68215d973a 116 * Read a SocketIO message
ansond 0:5e68215d973a 117 *
ansond 0:5e68215d973a 118 * @param message pointer to the string to be read (null if drop frame)
ansond 0:5e68215d973a 119 *
ansond 0:5e68215d973a 120 * @return true if a SocketIO frame has been read
ansond 0:5e68215d973a 121 */
ansond 0:5e68215d973a 122 bool read(char * message);
ansond 0:5e68215d973a 123
ansond 0:5e68215d973a 124 /**
ansond 0:5e68215d973a 125 * To see if there is a SocketIO connection active
ansond 0:5e68215d973a 126 *
ansond 0:5e68215d973a 127 * @return true if there is a connection active
ansond 0:5e68215d973a 128 */
ansond 0:5e68215d973a 129 bool is_connected();
ansond 0:5e68215d973a 130
ansond 0:5e68215d973a 131 /**
ansond 0:5e68215d973a 132 * Close the SocketIO connection
ansond 0:5e68215d973a 133 *
ansond 0:5e68215d973a 134 * @return true if the connection has been closed, false otherwise
ansond 0:5e68215d973a 135 */
ansond 0:5e68215d973a 136 bool close();
ansond 0:5e68215d973a 137
ansond 2:2979735cb379 138 protected:
ansond 2:2979735cb379 139 Websocket *ws; // websocket endpoint
ansond 2:2979735cb379 140
ansond 0:5e68215d973a 141 private:
ansond 0:5e68215d973a 142 // Variables
ansond 0:5e68215d973a 143 int version; // default socket.io version
ansond 0:5e68215d973a 144 char *url; // base URL endpoint to connect to. Example: "myapp.herokuapp.com"
ansond 0:5e68215d973a 145 char *url_session_key; // generated session key URL - used to extract the session key
ansond 2:2979735cb379 146 char *url_session; // session URL - specific socket.io session to bind to
ansond 0:5e68215d973a 147 char *session_key; // our session key
ansond 0:5e68215d973a 148 char *ws_channel; // our websocket channel for the session
ansond 0:5e68215d973a 149
ansond 0:5e68215d973a 150 // Methods
ansond 0:5e68215d973a 151 char *prepareSocketIOJSONMessage(char *name, char *args, char *buffer);
ansond 0:5e68215d973a 152 void prepareSessionURL();
ansond 0:5e68215d973a 153 bool attemptWebSocketConnect();
ansond 0:5e68215d973a 154 void parseSessionKey(char *response, char *sessionkey, char *ws_channel);
ansond 0:5e68215d973a 155 bool acquireSessionKey();
ansond 0:5e68215d973a 156 void prepareSessionKeyURL(char *myurl, int myversion);
ansond 0:5e68215d973a 157
ansond 0:5e68215d973a 158
ansond 0:5e68215d973a 159 };
ansond 0:5e68215d973a 160
ansond 0:5e68215d973a 161 #endif