Usage

Initialization

Declare a variable wsMgr of type WSConnectionManager_typ, and configure. Also declare a variable wsStream of type WSStream_typ.

In the task Init function:

// Configure wsMgr
strcpy(wsMgr.in.cfg.localIPAddress, "127.0.0.1");
wsMgr.in.cfg.localPort = 8000;
strcpy(wsMgr.in.cfg.remoteIPAddress, "127.0.0.1");
wsMgr.in.cfg.remotePort = 8081;
wsMgr.in.cfg.mode = WS_MODE_SERVER;
wsMgr.in.cmd.enable = 1;

// Configure wsStream
wsStream.in.cfg.bufferSize = 1000; // Set once, and can not be changed after initialization

Operation

Each time a new client attempts to connect or the server allows a connection, wsMgr reports a new connection available. New connections must be passed to a WSStream_typ. wsStream will handle negotiating the upgrade to WebSocket.

WebSocket Manager

wsManageConnection(&wsMgr);

if(wsMgr.out.newConnectionAvailable) {
    // Copy connection info to stream
    memcpy(&wsStream.in.par.connection, &wsMgr.out.connection, sizeof(wsStream.in.par.connection));
    
    wsMgr.in.cmd.acknowledgeConnection = 1;

    // Configure stream and start receiving data
    wsStream.in.cmd.receive = 1;
    wsStream.in.par.pSendData = &sendBuffer;
    wsStream.in.par.pReceiveData = &receiveBuffer;
    wsStream.in.par.maxReceiveLength = sizeof(receiveBuffer);
    wsStream.in.par.allowContinuousReceive = 1;
    wsStream.in.par.allowContinuousSend = 1;
}

Once wsStream reports connected, messages can be received and sent from the application.

webSocket Stream

wsReceive(&wsStream);
	
if(wsStream.out.dataReceived){
    // Echo received data back to client
    strcpy(sendBuffer, receiveBuffer);
    wsStream.in.par.sendLength = wsStream.out.receivedDataLength;
    wsStream.in.cmd.send = 1;
}

wsSend(&wsStream);

// Reset commands
wsStream.in.cmd.send = 0;
wsStream.in.cmd.acknowledgeError = 0;

Advance

TCPComm to Websocket

Connections established with TCPComm can be then upgraded to a WebSocket connection.

Made by Loupe