Fork of https://os.mbed.com/teams/mbed_example/code/WebSocketClient/ Update to MbedOS6

Embed: (wiki syntax)

« Back to documentation index

Websocket Class Reference

Websocket Class Reference

Websocket client Class. More...

#include <Websocket.h>

Public Member Functions

 Websocket (char *url, NetworkInterface *iface, const char *ca=nullptr)
 Constructor.
bool connect ()
 Connect to the websocket url.
int send (char *str)
 Send a string according to the websocket format (see rfc 6455)
bool read (char *message)
 Read a websocket message.
bool close ()
 Close the websocket connection.

Detailed Description

Websocket client Class.

Example (ethernet network):

 #include "mbed.h"
 #include "EthernetInterface.h"
 #include "Websocket.h"

 int main() {
    EthernetInterface eth;
    eth.connect();
    SocketAddress ip; 
    eth.get_ip_address(&ip);
    const char *p_ip = ip.get_ip_address();
    printf("IP address: %s\n", p_ip ? p_ip : "None");
   
    Websocket ws("ws://echo.websocket.org/");
    ws.connect();
    char recv[20];
    while (1) {
        int res = ws.send((char*)"WebSocket Hello World!");
        printf("Error: %d\n", res);
        if (ws.read(recv)) {
            printf("rcv: %s\r\n", recv);
        }

        thread_sleep_for(100ms);
    }
 }

Definition at line 68 of file Websocket.h.


Constructor & Destructor Documentation

Websocket ( char *  url,
NetworkInterface *  iface,
const char *  ca = nullptr 
)

Constructor.

Parameters:
urlThe Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80)
ifaceThe NetworkInterface
caTLS certificate (root_ca_cert)

Definition at line 19 of file Websocket.cpp.


Member Function Documentation

bool close (  )

Close the websocket connection.

Returns:
true if the connection has been closed, false otherwise

Definition at line 322 of file Websocket.cpp.

bool connect (  )

Connect to the websocket url.

Returns:
true if the connection is established, false otherwise

Definition at line 143 of file Websocket.cpp.

bool read ( char *  message )

Read a websocket message.

Parameters:
messagepointer to the string to be read (null if drop frame)
Returns:
true if a websocket frame has been read

Definition at line 251 of file Websocket.cpp.

int send ( char *  str )

Send a string according to the websocket format (see rfc 6455)

Parameters:
strstring to be sent
Returns:
the number of bytes sent

Definition at line 239 of file Websocket.cpp.