Simple websocket client based on the original with a few added features such as: - setBaud() - set the baud rate for the communication - Initialize() - mimics the constructor - chaged read() to readmsg() to avoid confusion with other functions - Added SerialCommPort

Fork of WebSocketClient by Damien Frost

Committer:
defrost
Date:
Wed Aug 23 10:59:19 2017 +0000
Revision:
30:0b5a84149a79
Parent:
29:33d77a538611
- Fixed bug in receiveBytes, it now works

Who changed what in which revision?

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