http://mbed.org/users/okini3939/notebook/node_websocket/

Dependencies:   EthernetNetIf mbed MbedJSONValue

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, Wifly *wifi)
 Constructor.
 Websocket (char *url, EthernetNetIf *e)
 Constructor for an ethernet 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 118 of file Websocket.h.


Constructor & Destructor Documentation

Websocket ( char *  url,
Wifly *  wifi 
)

Constructor.

Parameters:
urlThe Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80)
wifipointer to a wifi module (the communication will be establish by this module)

Definition at line 8 of file Websocket.cpp.

Websocket ( char *  url,
EthernetNetIf *  e 
)

Constructor for an ethernet communication.

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

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

bool connect (  )

Connect to the websocket url.

Returns:
true if the connection is established, false otherwise

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

std::string getPath (  )

Accessor: get path from the websocket url.

Returns:
path

Definition at line 421 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 303 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 285 of file Websocket.cpp.