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)
 Constructor for 3g communication.
bool connect ()
 Connect to the websocket url.
void send (char *str)
 Send a string according to the websocket format: 00 str ff.
bool read (char *message)
 Read a websocket message.
bool connected ()
 To see if there is a websocket connection active.
bool close ()
 Close the websocket connection.
std::string getPath ()
 Accessor: get path from the websocket url.

Detailed Description

Websocket client Class.

Warning: you must use a wifi module (Wifly RN131-C) or an ethernet network to use this class

Example (wifi network):

 #include "mbed.h"
 #include "Wifly.h"
 #include "Websocket.h"

 Serial pc(USBTX, USBRX);
 Wifly * wifly;
 Websocket * ws;

 int main()
 {
   wifly = new Wifly(p9, p10, p20, "network", "password", true);
   ws = new Websocket("ws://ip_domain/path", wifly);

   if(wifly->join())
   {
       if(ws->connect())
       {
           pc.printf("ws connected\r\n");
           while(1)
           {
               wait(0.1);
               ws->send("test");
           }
       }
       else
           pc.printf("ws not connected\r\n");
   }
   else
       pc.printf("join network failed\r\n");

 }

Example (ethernet network):

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

 Serial pc(USBTX, USBRX);
 Websocket * ws;

 int main()
 {
   ws = new Websocket("ws://ip_domain/path");

   if(ws->connect())
   {
      pc.printf("ws connected\r\n");
      while(1)
      {
         wait(0.1);
         ws->send("test");
      }
   }
   else
      pc.printf("ws not connected\r\n");
 }

Definition at line 106 of file Websocket.h.


Constructor & Destructor Documentation

Websocket ( char *  url )

Constructor for 3g communication.

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

Definition at line 12 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 298 of file Websocket.cpp.

bool connect (  )

Connect to the websocket url.

Returns:
true if the connection is established, false otherwise

Definition at line 80 of file Websocket.cpp.

bool connected (  )

To see if there is a websocket connection active.

Returns:
true if there is a connection active

Definition at line 315 of file Websocket.cpp.

std::string getPath (  )

Accessor: get path from the websocket url.

Returns:
path

Definition at line 319 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 string has been read, false otherwise

Definition at line 231 of file Websocket.cpp.

void send ( char *  str )

Send a string according to the websocket format: 00 str ff.

Parameters:
strstring to be sent

Definition at line 221 of file Websocket.cpp.