Websoket Demo

```language #include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdint.h> #include <time.h> #include <ctype.h> #include <stddef.h> #include <stdio.h> #include <stdint.h> #include "teldef.h" #include "osa.h" #include "UART.h" #include "sys.h" #include "sdk_api.h" #include "UART.h" #include "osa.h" #include "sockets.h" #include "netdb.h" #include "ssl.h" #include "entropy.h" #include "ctr_drbg.h" #include "websocket.h" #define WEBSOCKET_SHAKE_KEY_LEN 16 //#define printf(fmt, args...) do { CPUartLogPrintf("websocket: "fmt, ##args); } while(0) #define printf(fmt, args...) fatal_printf("websocket---: "fmt, ##args) #define sleep(x) OSATaskSleep((x) * 200)//second #define _TASK_STACK_SIZE 1024*256 static UINT32 _task_stack[_TASK_STACK_SIZE/sizeof(UINT32)]; static OSTaskRef _task_ref = NULL; static OSATimerRef _timer_ref = NULL; static OSFlagRef _flag_ref = NULL; #define TASK_TIMER_CHANGE_FLAG_BIT 0x01 //int port = 9999; //char ip[] = "203.156.205.55"; int port = 8080; char ip[] = "52.80.19.131"; static void _timer_callback(UINT32 tmrId); static void _task(void *ptr); // Device bootup hook before Phase1Inits. // If you have some work to be init, you may implete it here. // ex: you may start your task here. or do some initialize here. extern void Phase1Inits_enter(void); // Device bootup hook after Phase1Inits. // If you have some work to be init, you may implete it here. // ex: you may start your task here. or do some initialize here. extern void Phase1Inits_exit(void); // Device bootup hook before Phase2Inits. // If you have some work to be init, you may implete it here. // ex: you may start your task here. or do some initialize here. extern void Phase2Inits_enter(void); // Device bootup hook after Phase2Inits. // If you have some work to be init, you may implete it here. // ex: you may start your task here. or do some initialize here. extern void Phase2Inits_exit(void); void Phase1Inits_enter(void) { } void Phase1Inits_exit(void) { } void Phase2Inits_enter(void) { } void Phase2Inits_exit(void) { int ret; ret = OSAFlagCreate(&_flag_ref); ASSERT(ret == OS_SUCCESS); ret = OSATaskCreate(&_task_ref, _task_stack, _TASK_STACK_SIZE, 200, "test-task", _task, NULL); ASSERT(ret == OS_SUCCESS); } extern int SendATCMDWaitResp(int sATPInd,char *in_str, int timeout, char *ok_fmt, int ok_flag, char *err_fmt, char *out_str, int resplen); static void _task(void *ptr) { int ret, timeCount = -1; int fd; char buff[4096]; char at_str[32]={'\0'}; sprintf(at_str, "AT^SYSINFO\r"); char resp_str[64]={'\0'}; int ready=0; ret = SendATCMDWaitResp(TEL_AT_CMD_ATP_10, at_str, 3, "^SYSINFO:",1,NULL, resp_str, sizeof(resp_str)); printf("%s: resp_str = %s, ret = %u\n",__FUNCTION__,resp_str,ret); if(strstr(resp_str, "^SYSINFO: 2,3") != NULL || strstr(resp_str, "^SYSINFO: 2,2") != NULL){ ready = 1; } while (!ready){ sleep(1); ret = SendATCMDWaitResp(TEL_AT_CMD_ATP_10, at_str, 3, "^SYSINFO:",1,NULL, resp_str, sizeof(resp_str)); printf("%s: resp_str = %s, ret = %u\n",__FUNCTION__,resp_str,ret); printf("len = %u, resp_str[10] = %c, resp_str[12] = %c, resp_str[16] = %c\n",strlen(resp_str),resp_str[10],resp_str[12],resp_str[16]); if(strstr(resp_str, "^SYSINFO: 2,3") != NULL || strstr(resp_str, "^SYSINFO: 2,2") != NULL){ ready = 1; } } printf("\n\n\nSuccess in the net \n\n\n"); char ip[] = "52.80.19.131"; int port = 8080; char buf[1024]; memset(buff, 0, sizeof(buff)); sprintf(buff, "Say hi~ from client"); if ((fd = webSocket_clientLinkToServer(ip, port, "api/ws",1) <= 0)) //1-ssl { printf("client link to server failed !\r\n"); return ; }else { printf("websocket connect success!\n"); } // webSocket_delayms(100); // while(1){ ret = webSocket_send(fd, buff, strlen(buff), true, WDT_TXTDATA); if(ret <= 0) printf("websocet send err\n"); else printf("websocket send buf:%s\n",buff); memset(buf, 0, sizeof(buf)); ret = webSocket_recv(fd,buf,sizeof(buf) - 1,NULL); if (ret <= 0) { printf("_testsocket: recv error: %d", ret); //return; } printf("ssl_read buf: %s,ret:%d\n",buf,ret); // } websocket_close(fd); sleep(5); return 0; } ```