SocketIO updated for K64F usage

Fork of SocketIO by Doug Anson

Committer:
ansond
Date:
Thu Oct 09 16:13:12 2014 +0000
Revision:
13:1b8ff174aea6
Parent:
12:8fe60d9ca3bf
updates for K64F

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