SocketIO updated for K64F usage

Fork of SocketIO by Doug Anson

Committer:
ansond
Date:
Sun Nov 10 02:17:45 2013 +0000
Revision:
12:8fe60d9ca3bf
Parent:
10:997b8c5d2d46
Child:
13:1b8ff174aea6
updates

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 10:997b8c5d2d46 37 #define DEFAULT_VERSION 1
ansond 10:997b8c5d2d46 38
ansond 10:997b8c5d2d46 39 // Default SocketIO message length (suggestion only...)
ansond 12:8fe60d9ca3bf 40 #define SOCKETIO_MESSAGE_LENGTH 512
ansond 0:5e68215d973a 41
ansond 0:5e68215d973a 42 // HTTP support for session key retrieval
ansond 0:5e68215d973a 43 #include "HTTPClient.h"
ansond 0:5e68215d973a 44
ansond 0:5e68215d973a 45 // WebSocket layer support
ansond 0:5e68215d973a 46 #include "Websocket.h"
ansond 0:5e68215d973a 47
ansond 0:5e68215d973a 48 /** SocketIO client Class.
ansond 0:5e68215d973a 49 *
ansond 0:5e68215d973a 50 * Example (ethernet network):
ansond 0:5e68215d973a 51 * @code
ansond 0:5e68215d973a 52 * #include "mbed.h"
ansond 0:5e68215d973a 53 * #include "WiflyInterface.h"
ansond 0:5e68215d973a 54 * #include "SocketIO.h"
ansond 0:5e68215d973a 55 *
ansond 0:5e68215d973a 56 * // our wifi interface
ansond 0:5e68215d973a 57 * WiflyInterface wifly(p9, p10, p30, p29, "mysid", "mypw", WPA);
ansond 0:5e68215d973a 58 *
ansond 0:5e68215d973a 59 * int main() {
ansond 0:5e68215d973a 60 * wifly.init(); //Use DHCP
ansond 0:5e68215d973a 61 * wifly.connect();
ansond 0:5e68215d973a 62 * printf("IP Address is %s\n\r", wifly.getIPAddress());
ansond 0:5e68215d973a 63 *
ansond 0:5e68215d973a 64 * SocketIO socketio("myapp.herokuapp.com");
ansond 0:5e68215d973a 65 * socketio.connect();
ansond 0:5e68215d973a 66 *
ansond 0:5e68215d973a 67 * char recv[256];
ansond 0:5e68215d973a 68 * while (1) {
ansond 0:5e68215d973a 69 * int res = socketio.emit("mesage_name","[\"SocketIO Hello World!\"]");
ansond 0:5e68215d973a 70 * if (socketio.read(recv)) {
ansond 0:5e68215d973a 71 * printf("rcv: %s\r\n", recv);
ansond 0:5e68215d973a 72 * }
ansond 0:5e68215d973a 73 * wait(0.1);
ansond 0:5e68215d973a 74 * }
ansond 0:5e68215d973a 75 * }
ansond 0:5e68215d973a 76 * @endcode
ansond 0:5e68215d973a 77 */
ansond 0:5e68215d973a 78
ansond 0:5e68215d973a 79 class SocketIO
ansond 0:5e68215d973a 80 {
ansond 0:5e68215d973a 81 public:
ansond 0:5e68215d973a 82 /**
ansond 0:5e68215d973a 83 * Constructor
ansond 0:5e68215d973a 84 *
ansond 0:5e68215d973a 85 * @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 86 */
ansond 0:5e68215d973a 87 SocketIO(char * url);
ansond 0:5e68215d973a 88
ansond 0:5e68215d973a 89 /**
ansond 0:5e68215d973a 90 * Constructor
ansond 0:5e68215d973a 91 *
ansond 0:5e68215d973a 92 * @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 93 * @param version The SocketIO version for the session URL (by default version = 1)
ansond 0:5e68215d973a 94 */
ansond 0:5e68215d973a 95 SocketIO(char * url, int version);
ansond 0:5e68215d973a 96
ansond 0:5e68215d973a 97 /**
ansond 0:5e68215d973a 98 * Connect to the SocketIO url
ansond 0:5e68215d973a 99 *
ansond 0:5e68215d973a 100 *@return true if the connection is established, false otherwise
ansond 0:5e68215d973a 101 */
ansond 0:5e68215d973a 102 bool connect();
ansond 0:5e68215d973a 103
ansond 0:5e68215d973a 104 /**
ansond 0:5e68215d973a 105 * Emit (Broadcast) a socket.io message to the SocketIO server
ansond 0:5e68215d973a 106 *
ansond 0:5e68215d973a 107 * Socket.IO message Format (JSON): { "name": <name>, "args":<args> }
ansond 0:5e68215d973a 108 * name: the "name" of the socket.io message
ansond 0:5e68215d973a 109 * args: the argument(s) (must always be a JSON array) of the message. Example: "[ \"foo\", {\"bar\": \"none\"}]"
ansond 0:5e68215d973a 110 *
ansond 0:5e68215d973a 111 * @param name "name" of the socket.io message to broadcast
ansond 0:5e68215d973a 112 * @param args argument string to be sent ( must be in a JSON array format. Example: "[ \"foo\", {\"bar\": \"none\"}]" )
ansond 0:5e68215d973a 113 *
ansond 0:5e68215d973a 114 * @returns the number of bytes sent
ansond 0:5e68215d973a 115 */
ansond 0:5e68215d973a 116 int emit(char * name, char * args);
ansond 0:5e68215d973a 117
ansond 0:5e68215d973a 118 /**
ansond 0:5e68215d973a 119 * Read a SocketIO message
ansond 0:5e68215d973a 120 *
ansond 0:5e68215d973a 121 * @param message pointer to the string to be read (null if drop frame)
ansond 0:5e68215d973a 122 *
ansond 0:5e68215d973a 123 * @return true if a SocketIO frame has been read
ansond 0:5e68215d973a 124 */
ansond 0:5e68215d973a 125 bool read(char * message);
ansond 0:5e68215d973a 126
ansond 0:5e68215d973a 127 /**
ansond 0:5e68215d973a 128 * To see if there is a SocketIO connection active
ansond 0:5e68215d973a 129 *
ansond 0:5e68215d973a 130 * @return true if there is a connection active
ansond 0:5e68215d973a 131 */
ansond 0:5e68215d973a 132 bool is_connected();
ansond 0:5e68215d973a 133
ansond 0:5e68215d973a 134 /**
ansond 0:5e68215d973a 135 * Close the SocketIO connection
ansond 0:5e68215d973a 136 *
ansond 0:5e68215d973a 137 * @return true if the connection has been closed, false otherwise
ansond 0:5e68215d973a 138 */
ansond 0:5e68215d973a 139 bool close();
ansond 0:5e68215d973a 140
ansond 2:2979735cb379 141 protected:
ansond 2:2979735cb379 142 Websocket *ws; // websocket endpoint
ansond 2:2979735cb379 143
ansond 0:5e68215d973a 144 private:
ansond 0:5e68215d973a 145 // Variables
ansond 0:5e68215d973a 146 int version; // default socket.io version
ansond 0:5e68215d973a 147 char *url; // base URL endpoint to connect to. Example: "myapp.herokuapp.com"
ansond 0:5e68215d973a 148 char *url_session_key; // generated session key URL - used to extract the session key
ansond 2:2979735cb379 149 char *url_session; // session URL - specific socket.io session to bind to
ansond 0:5e68215d973a 150 char *session_key; // our session key
ansond 0:5e68215d973a 151 char *ws_channel; // our websocket channel for the session
ansond 0:5e68215d973a 152
ansond 0:5e68215d973a 153 // Methods
ansond 0:5e68215d973a 154 char *prepareSocketIOJSONMessage(char *name, char *args, char *buffer);
ansond 0:5e68215d973a 155 void prepareSessionURL();
ansond 0:5e68215d973a 156 bool attemptWebSocketConnect();
ansond 0:5e68215d973a 157 void parseSessionKey(char *response, char *sessionkey, char *ws_channel);
ansond 0:5e68215d973a 158 bool acquireSessionKey();
ansond 0:5e68215d973a 159 void prepareSessionKeyURL(char *myurl, int myversion);
ansond 0:5e68215d973a 160
ansond 0:5e68215d973a 161
ansond 0:5e68215d973a 162 };
ansond 0:5e68215d973a 163
ansond 0:5e68215d973a 164 #endif