add example

This commit is contained in:
pvvx 2017-06-07 23:44:38 +03:00
parent 0cd01e4dc1
commit 5cd34b0c9f
75 changed files with 6023 additions and 217 deletions

View file

@ -0,0 +1,64 @@
#include "FreeRTOS.h"
#include "task.h"
#include <platform/platform_stdlib.h>
#include "wifi_conf.h"
#include "wifi_ind.h"
#include "websocket/libwsclient.h"
#include "websocket/wsclient_api.h"
#include <stdio.h>
#include "example_wsclient.h"
void handle_message(wsclient_context *wsclient, int data_len)
{
printf("\r\n>>>>>> Receiving: %s with length: %d\n", wsclient->receivedData, data_len);
if(strcmp(wsclient->receivedData, "hello") == 0)
ws_send("world", strlen("world"), 1, wsclient);
else if (strcmp(wsclient->receivedData, "world") == 0){
ws_close(wsclient);
wsclient = NULL;
}
}
static void example_wsclient_thread(void *param)
{
printf("\r\n\r\n\r\n>>>>>>>>>>>>>>>wsclient example<<<<<<<<<<<<<<<<<\r\n\r\n\r\n");
vTaskDelay(10000);
while(wifi_is_ready_to_transceive(RTW_STA_INTERFACE) != RTW_SUCCESS){
printf("\r\n\r\n\r\n>>>>>>>>>>>>>>Wifi is disconnected!!Please connect!!<<<<<<<<<<<<<<<<<\r\n\r\n\r\n");
vTaskDelay(10000);
}
int ret;
//wsclient_context *wsclient = create_wsclient("wss://echo.websocket.org", 0, NULL, NULL);
wsclient_context *wsclient = create_wsclient("wss://sandbox.kaazing.net", 0, "echo", NULL);
if(wsclient != NULL){
if(wsclient->use_ssl == 1){
#ifndef USING_SSL
printf("\r\nNot Support the wss server!\r\n");
vTaskDelete(NULL);
#endif
}
ret = ws_connect_url(wsclient);
if(ret >= 0){
ws_send("hello", strlen("hello"), 1, wsclient);
while (ws_getReadyState(wsclient) != CLOSED) {
ws_dispatch(handle_message);
ws_poll(0, wsclient);
}
}
else
printf("\r\nConnect to websocket server failed!\r\n");
}
else
printf("\r\nCreat websocket context failed!\r\n");
vTaskDelete(NULL);
}
void example_wsclient(void)
{
if(xTaskCreate(example_wsclient_thread, ((const char*)"example_wsclient_thread"), 1024, NULL, tskIDLE_PRIORITY + 1, NULL) != pdPASS)
printf("\n\r%s xTaskCreate(init_thread) failed", __FUNCTION__);
}

View file

@ -0,0 +1,6 @@
#ifndef _EXAMPLE_WSCLIENT_H
#define _EXAMPLE_WSCLIENT_H
void example_wsclient(void);
#endif /* _EXAMPLE_WSCLIENT_H */

View file

@ -0,0 +1,48 @@
Websocket Client Example
Description:
A simple websocket client example which send "hello" and "world" to server.
The server will reply the message it received.
Once the client received "world", it will disconnect with server.
The SSL websocket server:
wss://echo.websocket.org
wss://sandbox.kaazing.net/echo
The websocket server without SSL:
ws://echo.websocket.org
ws://sandbox.kaazing.net/echo
Configuration:
[platform_opts.h]
#define CONFIG_EXAMPLE_WEBSOCKET 1
If using the WSS server:
[wsclient_api.h]
#define USING_SSL
[config_rsa.h]
#define SSL_MAX_CONTENT_LEN 5120
[example_wsclient.c]
wsclient_context *wsclient = create_wsclient("wss://sandbox.kaazing.net", 0, "echo", NULL);
or
wsclient_context *wsclient = create_wsclient("wss://echo.websocket.org", 0, NULL, NULL);
If using the WS server:
[example_wsclient.c]
wsclient_context *wsclient = create_wsclient("ws://sandbox.kaazing.net", 0, "echo", NULL);
or
wsclient_context *wsclient = create_wsclient("ws://echo.websocket.org", 0, NULL, NULL);
Execution:
Can make automatical Wi-Fi connection when booting by using wlan fast connect example.
A websocket client example thread will be started automatically when booting.
If using other websocekt server, modify the create_wsclient() API and the handle_message() function depending on the condition of the server.