This package includes the SharkSSL lite library and header files.

Dependents:   WebSocket-Client-Example SharkMQ-LED-Demo

Embed: (wiki syntax)

« Back to documentation index

WsClientLib

Data Structures

struct  WscReadState
 The WebSocket protocol is frame based and the following struct keeps state information for wscRead. More...

Modules

 WebSocket Opcodes
 

WebSocket Opcodes.


Functions

int wscProtocolHandshake (WscReadState *wss, SharkSslCon *s, SOCKET *sock, U32 tmo, const char *host, const char *path, const char *origin)
 Upgrades (morphs) an HTTPS request/response pair to a WebSocket connection.
int wscSendBin (SharkSslCon *s, SOCKET *sock, U8 *buf, int len)
 Sends binary data to server.
int wscSendCtrl (SharkSslCon *s, SOCKET *sock, U8 opCode, const U8 *buf, int len)
 Sends a WebSocket control frame.
int wscClose (SharkSslCon *s, SOCKET *sock, int statusCode)
 Sends a WebSocket close control frame to the server and closes the connection.
int wscRead (WscReadState *wss, SharkSslCon *s, SOCKET *sock, U8 **buf, U32 timeout)
 Wait for WebSocket frames sent by the server.

Function Documentation

int wscClose ( SharkSslCon s,
SOCKET *  sock,
int  statusCode 
)

Sends a WebSocket close control frame to the server and closes the connection.

Parameters:
sthe SharkSslCon object.
sockthe SOCKET object.
statusCodeis a WebSocket status code.
int wscProtocolHandshake ( WscReadState wss,
SharkSslCon s,
SOCKET *  sock,
U32  tmo,
const char *  host,
const char *  path,
const char *  origin 
)

Upgrades (morphs) an HTTPS request/response pair to a WebSocket connection.

Sends the HTTP request header to the server and validates the server's HTTP response header -- the function simulates a very basic HTTP client library. The function is designed to be as simple as possible and the code is, for this reason, making a few assumptions that could fail when used with a non traditional HTTP server. Read the comments in the source code file WsClientLib.c if you should experience problems.

Parameters:
wssthe WebSocket protocol state information is stored in this structure. All wss attributes must be initialized to zero before calling this function for the first time.
sthe SharkSslCon object
sockthe SOCKET object
tmoin milliseconds. The timeout can be set to INFINITE_TMO.
hostis the server's host name
pathis the path component of the wss URL and the path must be to the server's WebSocket service.
originsome WebSocket server's may require an origin URL: http://tools.ietf.org/html/rfc6455#section-10.2. Set the parameter to NULL if it's not required by the server. The Origin header should only be required by a server when the request is sent from a browser.
Returns:
Zero success.
int wscRead ( WscReadState wss,
SharkSslCon s,
SOCKET *  sock,
U8 **  buf,
U32  timeout 
)

Wait for WebSocket frames sent by the server.

The function returns when data is available or on timeout. The function returns zero on timeout, but the peer can send zero length frames so you must verify that it is a timeout by checking the status of WscReadState::isTimeout.

The WebSocket protocol is frame based, but the function can return a fragment before the complete WebSocket frame is received if the frame sent by the peer is larger than the SharkSSL receive buffer. The frame length is returned in WscReadState::frameLen and the data consumed thus far is returned in WscReadState::bytesRead. The complete frame is consumed when frameLen == bytesRead.

Parameters:
wssis the WebSocket read state.
sthe SharkSslCon object.
sockthe SOCKET object.
bufis a pointer set to the SharkSSL receive buffer offset to the start of the WebSocket payload data.
timeoutin milliseconds. The timeout can be set to INFINITE_TMO.
Returns:
The payload data length or zero for zero length frames and timeout. The function returns a negative value on error.
int wscSendBin ( SharkSslCon s,
SOCKET *  sock,
U8 *  buf,
int  len 
)

Sends binary data to server.

The function sets the WS frame header's opcode to binary. The WS protocol supports two payload frame types, UTF8 and binary (RFC6455: 5.6 Data Frames). We are assuming that you will be using the binary protocol for all data exchange.

int wscSendCtrl ( SharkSslCon s,
SOCKET *  sock,
U8  opCode,
const U8 *  buf,
int  len 
)

Sends a WebSocket control frame.

The code is used internally by the WebSocket functions. You can also use this function to send your own control frames such as WSOP_Ping.

See RFC6455: 5.5. Control Frames