initial commit
This commit is contained in:
77
lib/amb1_sdk/common/example/websocket/example_wsclient.c
Normal file
77
lib/amb1_sdk/common/example/websocket/example_wsclient.c
Normal file
@@ -0,0 +1,77 @@
|
||||
#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)
|
||||
{
|
||||
wsclient_context *wsc = *wsclient;
|
||||
printf("\r\n>>>>>> Receiving: %s with length: %d\n", wsc->receivedData, data_len);
|
||||
|
||||
if(strcmp((char const*)wsc->receivedData, "hello") == 0)
|
||||
ws_send("world", strlen("world"), 1, wsc);
|
||||
else if (strcmp((char const*)wsc->receivedData, "world") == 0){
|
||||
ws_close(wsclient);
|
||||
}
|
||||
}
|
||||
|
||||
static void example_wsclient_thread(void *param)
|
||||
{
|
||||
/* To avoid gcc warnings */
|
||||
( 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;
|
||||
|
||||
//Please set SSL_MAX_CONTENT_LEN to 7680 for maximum input msglen 7067 Bytes
|
||||
//wsclient_context *wsclient = create_wsclient("wss://echo.websocket.org", 0, NULL, NULL, 1500, 3);
|
||||
|
||||
//Please set SSL_MAX_CONTENT_LEN to 5120 for maximum input msglen 4849 Bytes
|
||||
wsclient_context *wsclient = create_wsclient("wss://sandbox.kaazing.net", 0, "echo", NULL, 1500, 3);
|
||||
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);
|
||||
ws_dispatch(handle_message);
|
||||
while (wsclient->readyState != CLOSED){
|
||||
|
||||
ws_poll(0, &wsclient);
|
||||
}
|
||||
}
|
||||
else
|
||||
printf("\r\nConnect to websocket server failed!\r\n");
|
||||
|
||||
if(wsclient){
|
||||
ws_free(wsclient);
|
||||
wsclient = NULL;
|
||||
}
|
||||
}
|
||||
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__);
|
||||
}
|
||||
6
lib/amb1_sdk/common/example/websocket/example_wsclient.h
Normal file
6
lib/amb1_sdk/common/example/websocket/example_wsclient.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef _EXAMPLE_WSCLIENT_H
|
||||
#define _EXAMPLE_WSCLIENT_H
|
||||
|
||||
void example_wsclient(void);
|
||||
|
||||
#endif /* _EXAMPLE_WSCLIENT_H */
|
||||
59
lib/amb1_sdk/common/example/websocket/readme.txt
Normal file
59
lib/amb1_sdk/common/example/websocket/readme.txt
Normal file
@@ -0,0 +1,59 @@
|
||||
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_CLIENT 1
|
||||
|
||||
|
||||
If using the WSS server:
|
||||
|
||||
[wsclient_api.h]
|
||||
#define USING_SSL
|
||||
|
||||
[config_rsa.h]
|
||||
if connecting to wss://sandbox.kaazing.net
|
||||
#define SSL_MAX_CONTENT_LEN 5120
|
||||
or
|
||||
if connecting to wss://echo.websocket.org
|
||||
#define SSL_MAX_CONTENT_LEN 7680
|
||||
|
||||
[example_wsclient.c]
|
||||
wsclient_context *wsclient = create_wsclient("wss://sandbox.kaazing.net", 0, "echo", NULL, 1500);
|
||||
or
|
||||
wsclient_context *wsclient = create_wsclient("wss://echo.websocket.org", 0, NULL, NULL, 1500);
|
||||
|
||||
|
||||
If using the WS server:
|
||||
|
||||
[example_wsclient.c]
|
||||
wsclient_context *wsclient = create_wsclient("ws://sandbox.kaazing.net", 0, "echo", NULL, 1500);
|
||||
or
|
||||
wsclient_context *wsclient = create_wsclient("ws://echo.websocket.org", 0, NULL, NULL, 1500);
|
||||
|
||||
|
||||
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.
|
||||
|
||||
|
||||
Note :
|
||||
AmebaPro doesn't support polarssl, only support mbedtls.
|
||||
|
||||
[Supported List]
|
||||
Supported :
|
||||
Ameba-1, Ameba-z, Ameba-pro
|
||||
Reference in New Issue
Block a user