Simple websocket client
Dependents: WebsocketClient Web_suck_et APS SO - ALARME CONTROLADO VIA SOCKETS F411-mbed-os-iot-project ... more
Fork of WebSocketClient by
Revision 9:efa2c147bee1, committed 2017-03-16
- Comitter:
- mbed_official
- Date:
- Thu Mar 16 21:10:27 2017 +0000
- Parent:
- 8:ccedee13be8d
- Commit message:
- Updated library to utilize mbed-os
Changed in this revision
Websocket.cpp | Show annotated file Show diff for this revision Revisions of this file |
Websocket.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r ccedee13be8d -r efa2c147bee1 Websocket.cpp --- a/Websocket.cpp Wed Oct 23 09:58:39 2013 +0000 +++ b/Websocket.cpp Thu Mar 16 21:10:27 2017 +0000 @@ -1,18 +1,3 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ #include "Websocket.h" #define MAX_TRY_WRITE 20 @@ -31,9 +16,10 @@ #define INFO(x, ...) printf("[WebSocket : INFO]"x"\r\n", ##__VA_ARGS__); -Websocket::Websocket(char * url) { +Websocket::Websocket(char * url, NetworkInterface * iface) { fillFields(url); - socket.set_blocking(false, 400); + socket.open(iface); + socket.set_timeout(400); } void Websocket::fillFields(char * url) { @@ -93,6 +79,11 @@ *port=0; } char* pathPtr = strchr(hostPtr, '/'); + if(pathPtr == NULL) + { + WARN("Path not specified. Please add /[path] to the end of the websocket address"); + return -1; + } if( hostLen == 0 ) { hostLen = pathPtr - hostPtr; @@ -181,7 +172,6 @@ return false; } cmd[ret] = '\0'; - printf("%s",cmd); } while (ret > 0); close(); return false; @@ -254,20 +244,14 @@ DBG("timeout ws\r\n"); return false; } - - if(!socket.is_connected()) - { - WARN("Connection was closed by server"); + + socket.set_timeout(1); + if (socket.recv(&opcode, 1) != 1) { + socket.set_timeout(2000); return false; } - socket.set_blocking(false, 1); - if (socket.receive(&opcode, 1) != 1) { - socket.set_blocking(false, 2000); - return false; - } - - socket.set_blocking(false, 2000); + socket.set_timeout(2000); if (opcode == 0x81) break; @@ -296,9 +280,10 @@ DBG("length: %d\r\n", len_msg); if (is_masked) { - for (i = 0; i < 4; i++) - readChar(&c); - mask[i] = c; + for (i = 0; i < 4; i++) { + readChar(&c); + mask[i] = c; + } } int nb = read(message, len_msg, len_msg); @@ -315,8 +300,6 @@ } bool Websocket::close() { - if (!is_connected()) - return false; int ret = socket.close(); if (ret < 0) { @@ -326,10 +309,6 @@ return true; } -bool Websocket::is_connected() { - return socket.is_connected(); -} - char* Websocket::getPath() { return path; } @@ -338,14 +317,8 @@ int res = 0, idx = 0; for (int j = 0; j < MAX_TRY_WRITE; j++) { - - if(!socket.is_connected()) - { - WARN("Connection was closed by server"); - break; - } - if ((res = socket.send_all(str + idx, len - idx)) == -1) + if ((res = socket.send(str + idx, len - idx)) < 0) continue; idx += res; @@ -362,8 +335,8 @@ for (int j = 0; j < MAX_TRY_WRITE; j++) { - if ((res = socket.receive_all(str + idx, len - idx)) == -1) - continue; + if ((res = socket.recv(str + idx, len - idx)) < 0) + continue; idx += res;
diff -r ccedee13be8d -r efa2c147bee1 Websocket.h --- a/Websocket.h Wed Oct 23 09:58:39 2013 +0000 +++ b/Websocket.h Thu Mar 16 21:10:27 2017 +0000 @@ -1,25 +1,38 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +/** +* @author Samuel Mokrani +* +* @section LICENSE +* +* Copyright (c) 2011 mbed +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +* +* @section DESCRIPTION +* Simple websocket client +* +*/ + #ifndef WEBSOCKET_H #define WEBSOCKET_H #include "mbed.h" -#include "TCPSocketConnection.h" - /** Websocket client Class. * * Example (ethernet network): @@ -58,7 +71,7 @@ * * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80) */ - Websocket(char * url); + Websocket(char * url, NetworkInterface * iface); /** * Connect to the websocket url @@ -86,13 +99,6 @@ bool read(char * message); /** - * To see if there is a websocket connection active - * - * @return true if there is a connection active - */ - bool is_connected(); - - /** * Close the websocket connection * * @return true if the connection has been closed, false otherwise @@ -107,7 +113,6 @@ char* getPath(); private: - void fillFields(char * url); 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 int sendOpcode(uint8_t opcode, char * msg); @@ -120,7 +125,7 @@ char host[32]; char path[64]; - TCPSocketConnection socket; + TCPSocket socket; int read(char * buf, int len, int min_len = -1); int write(char * buf, int len);