sample program of Web socket server.
Dependencies: mbed WS_SERVER EthernetInterface HTTP_SERVER
main.cpp@0:6a6f021d1f40, 2020-10-21 (annotated)
- Committer:
- aktk
- Date:
- Wed Oct 21 19:34:01 2020 +0000
- Revision:
- 0:6a6f021d1f40
I verified it worked in 2016. but some libraries seemed modified which I have forgot, so idk if it works now.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aktk | 0:6a6f021d1f40 | 1 | #include "mbed.h" |
aktk | 0:6a6f021d1f40 | 2 | #include "WS_SERVER.h" |
aktk | 0:6a6f021d1f40 | 3 | #include "HTTP_SERVER.h" |
aktk | 0:6a6f021d1f40 | 4 | #include "rtos.h" |
aktk | 0:6a6f021d1f40 | 5 | #ifndef DEBUG |
aktk | 0:6a6f021d1f40 | 6 | //#define DEBUG |
aktk | 0:6a6f021d1f40 | 7 | #endif |
aktk | 0:6a6f021d1f40 | 8 | |
aktk | 0:6a6f021d1f40 | 9 | WSS wss(1024); |
aktk | 0:6a6f021d1f40 | 10 | HttpServer httpsvr; |
aktk | 0:6a6f021d1f40 | 11 | DigitalOut led1(LED1); |
aktk | 0:6a6f021d1f40 | 12 | DigitalOut led2(LED2); |
aktk | 0:6a6f021d1f40 | 13 | DigitalOut led3(LED3); |
aktk | 0:6a6f021d1f40 | 14 | DigitalOut led4(LED4); |
aktk | 0:6a6f021d1f40 | 15 | Serial pc(USBTX, USBRX); |
aktk | 0:6a6f021d1f40 | 16 | |
aktk | 0:6a6f021d1f40 | 17 | void httpServerThread(void const* argument) |
aktk | 0:6a6f021d1f40 | 18 | { |
aktk | 0:6a6f021d1f40 | 19 | if(httpsvr.init()) { |
aktk | 0:6a6f021d1f40 | 20 | if(httpsvr.run() == 0) |
aktk | 0:6a6f021d1f40 | 21 | printf("end\r\n"); |
aktk | 0:6a6f021d1f40 | 22 | else |
aktk | 0:6a6f021d1f40 | 23 | printf("error end\r\n"); |
aktk | 0:6a6f021d1f40 | 24 | } |
aktk | 0:6a6f021d1f40 | 25 | } |
aktk | 0:6a6f021d1f40 | 26 | void test(void const* argument) |
aktk | 0:6a6f021d1f40 | 27 | { |
aktk | 0:6a6f021d1f40 | 28 | while(true) { //wss.isListening()) { |
aktk | 0:6a6f021d1f40 | 29 | led1 = !led1; |
aktk | 0:6a6f021d1f40 | 30 | Thread::wait(500); |
aktk | 0:6a6f021d1f40 | 31 | } |
aktk | 0:6a6f021d1f40 | 32 | } |
aktk | 0:6a6f021d1f40 | 33 | |
aktk | 0:6a6f021d1f40 | 34 | void websocketServerThread()//void const* argument) |
aktk | 0:6a6f021d1f40 | 35 | { |
aktk | 0:6a6f021d1f40 | 36 | // |
aktk | 0:6a6f021d1f40 | 37 | // Websocket communication |
aktk | 0:6a6f021d1f40 | 38 | // |
aktk | 0:6a6f021d1f40 | 39 | //wss.txPing(); |
aktk | 0:6a6f021d1f40 | 40 | if(wss.init()) { |
aktk | 0:6a6f021d1f40 | 41 | while(wss.isListening())wss.run(); |
aktk | 0:6a6f021d1f40 | 42 | } |
aktk | 0:6a6f021d1f40 | 43 | |
aktk | 0:6a6f021d1f40 | 44 | // -----Websocket com end----- |
aktk | 0:6a6f021d1f40 | 45 | led3=led4=0; |
aktk | 0:6a6f021d1f40 | 46 | } |
aktk | 0:6a6f021d1f40 | 47 | |
aktk | 0:6a6f021d1f40 | 48 | void printRxData(void const* argument) |
aktk | 0:6a6f021d1f40 | 49 | { |
aktk | 0:6a6f021d1f40 | 50 | char l_data; |
aktk | 0:6a6f021d1f40 | 51 | wss.txPing(); |
aktk | 0:6a6f021d1f40 | 52 | while(1) { |
aktk | 0:6a6f021d1f40 | 53 | if(wss.isReadable()) { |
aktk | 0:6a6f021d1f40 | 54 | l_data = (char)wss.getRxData(); |
aktk | 0:6a6f021d1f40 | 55 | printf("(printRxData) %c(%d) \r\n", l_data, l_data); |
aktk | 0:6a6f021d1f40 | 56 | } |
aktk | 0:6a6f021d1f40 | 57 | else{ |
aktk | 0:6a6f021d1f40 | 58 | printf("no printings\r\n"); |
aktk | 0:6a6f021d1f40 | 59 | Thread::wait(500); |
aktk | 0:6a6f021d1f40 | 60 | } |
aktk | 0:6a6f021d1f40 | 61 | } |
aktk | 0:6a6f021d1f40 | 62 | } |
aktk | 0:6a6f021d1f40 | 63 | |
aktk | 0:6a6f021d1f40 | 64 | int main() |
aktk | 0:6a6f021d1f40 | 65 | { |
aktk | 0:6a6f021d1f40 | 66 | //Thread httpListener(httpServerThread, NULL, osPriorityNormal, (DEFAULT_STACK_SIZE * 2.25)); |
aktk | 0:6a6f021d1f40 | 67 | //httpServerThread(); |
aktk | 0:6a6f021d1f40 | 68 | Thread wssListener(websocketServerThread, NULL, osPriorityNormal, (DEFAULT_STACK_SIZE * 2.25)); |
aktk | 0:6a6f021d1f40 | 69 | //Thread::wait(5000); |
aktk | 0:6a6f021d1f40 | 70 | Thread printTaske(printRxData); |
aktk | 0:6a6f021d1f40 | 71 | //websocketServerThread(); |
aktk | 0:6a6f021d1f40 | 72 | //wssListener.start(websocketServerThread); |
aktk | 0:6a6f021d1f40 | 73 | //printTaske.start(printRxData); |
aktk | 0:6a6f021d1f40 | 74 | //wssListener.terminate(); |
aktk | 0:6a6f021d1f40 | 75 | //printTaske.terminate(); |
aktk | 0:6a6f021d1f40 | 76 | return 0; |
aktk | 0:6a6f021d1f40 | 77 | } |
aktk | 0:6a6f021d1f40 | 78 |