Websocket_Sample for MurataTypeYD

Dependencies:   mbed picojson

Committer:
komoritan
Date:
Thu Mar 12 12:15:46 2015 +0000
Revision:
1:b5ac0f971f43
Parent:
0:14bd24b5a77f
Fixed

Who changed what in which revision?

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