主页 > 编程资料 > Arduino >
发布时间:2018-08-27 作者:apizl 阅读:799次

arduino 实时通讯Websocket库下载支持server和client以及持续连接


将arduino作为一个实时通讯小服务器或者实时物联网传输数据,效率远比之前POST或GET请求来得快。

基于RFC6455的Arduino WebSocket服务器和客户端。


RFC6455支持的特性:

    文本发送

    二进制

    连接关闭

    心跳请求

    心跳包返回

    持续连接

    局限性:

    最大输入长度限制于RAM和WEBSOCKETS_MAX_DATA_SIZE 的设定

    最大输出长度没有限制(主要看硬件)

    客户端发送带有掩码的大帧0x00000000(在AVR所有帧上)

    延续帧重新组装需要在应用程序代码中处理


限制异步:

    从websocket事件上下文中调用的函数可能不支持yield()和/或delay()。有关更多信息和可能的解决方案,请参阅本期文章。

    wss / SSL是不可能的。


支持的硬件设备:

    ESP8266 Arduino for ESP8266

    ESP32 Arduino for ESP32

    ESP31B

    Particle with STM32 ARM Cortex M3

    ATmega328 with Ethernet Shield (ATmega branch)

    ATmega328 with enc28j60 (ATmega branch)

    ATmega2560 with Ethernet Shield (ATmega branch)

    ATmega2560 with enc28j60 (ATmega branch)


注意:

    版本2.0及以上不兼容AVR/ATmega,检查ATmega branch。

    Arduino for AVR不支持c++的std命名空间。


wss / SSL的说明:

    在ESP8266 wss / SSL上的wss客户端在WebSocketsServer中没有本地支持,但是通过在SSL代理后面运行设备可以实现安全的websockets。要启用此功能,请参阅Nginx的示例Nginx服务器配置文件。

 # ESP8266 nginx SSL reverse proxy configuration file (tested and working on nginx v1.10.0)
    # proxy cache location
    proxy_cache_path /opt/etc/nginx/cache levels=1:2 keys_zone=ESP8266_cache:10m max_size=10g inactive=5m use_temp_path=off;
    
    # webserver proxy
    server {
    
        # general server parameters
        listen                      50080;
        server_name                 myDomain.net;
        access_log                  /opt/var/log/nginx/myDomain.net.access.log;       
    
        # SSL configuration
        ssl                         on;
        ssl_certificate             /usr/builtin/etc/certificate/lets-encrypt/myDomain.net/fullchain.pem;
        ssl_certificate_key         /usr/builtin/etc/certificate/lets-encrypt/myDomain.net/privkey.pem;
        ssl_session_cache           builtin:1000  shared:SSL:10m;
        ssl_protocols               TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers                 HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
        ssl_prefer_server_ciphers   on;
        
        location / {
    
          # proxy caching configuration
          proxy_cache             ESP8266_cache;
          proxy_cache_revalidate  on;
          proxy_cache_min_uses    1;
          proxy_cache_use_stale   off;
          proxy_cache_lock        on;
          # proxy_cache_bypass      $http_cache_control;      
          # include the sessionId cookie value as part of the cache key - keeps the cache per user
          # proxy_cache_key         $proxy_host$request_uri$cookie_sessionId;
    
          # header pass through configuration
          proxy_set_header        Host $host;      
          proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header        X-Forwarded-Proto $scheme;      
    
          # ESP8266 custom headers which identify to the device that it's running through an SSL proxy     
          proxy_set_header        X-SSL On;
          proxy_set_header        X-SSL-WebserverPort 50080;
          proxy_set_header        X-SSL-WebsocketPort 50081;
    
          # extra debug headers      
          add_header              X-Proxy-Cache $upstream_cache_status;
          add_header              X-Forwarded-For $proxy_add_x_forwarded_for;
    
          # actual proxying configuration
          proxy_ssl_session_reuse on;
          # target the IP address of the device with proxy_pass
          proxy_pass              http://192.168.0.20;
          proxy_read_timeout      90;
        }
     }
    
    # websocket proxy
    server {
    
        # general server parameters
        listen                      50081;
        server_name                 myDomain.net;
        access_log                  /opt/var/log/nginx/myDomain.net.wss.access.log;
    
        # SSL configuration
        ssl                         on;
        ssl_certificate             /usr/builtin/etc/certificate/lets-encrypt/myDomain.net/fullchain.pem;
        ssl_certificate_key         /usr/builtin/etc/certificate/lets-encrypt/myDomain.net/privkey.pem;
        ssl_session_cache           builtin:1000  shared:SSL:10m;
        ssl_protocols               TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers                 HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
        ssl_prefer_server_ciphers   on;
        
        location / {     
    
          # websocket upgrade tunnel configuration
          proxy_pass                    http://192.168.0.20:81;
          proxy_http_version            1.1;
          proxy_set_header Upgrade      $http_upgrade;
          proxy_set_header Connection   "Upgrade";
          proxy_read_timeout            86400;
        }
     }

                                             


ESP异步TCP:

    这个libary可以在ESP上以异步TCP模式运行。

    模式可以在WebSockets被激活。h(见WEBSOCKETS_NETWORK_TYPE定义)。

    https://github.com/me-no-dev/ESPAsyncTCP



高级客户端API:

    begin:启动到websocket主机的连接序列。

 void begin(const char *host, uint16_t port, const char * url = "/", const char * protocol = "arduino");
 void begin(String host, uint16_t port, String url = "/", String protocol = "arduino");

    

    onEvent:回调来处理websocket事件

 void onEvent(WebSocketClientEvent cbEvent);

    

    WebSocketClientEvent:websocket事件的处理

    void (*WebSocketClientEvent)(WStype_t type, uint8_t * payload, size_t length)


其中WStype_t类型定义为:

typedef enum {
        WStype_ERROR,
        WStype_DISCONNECTED,
        WStype_CONNECTED,
        WStype_TEXT,
        WStype_BIN,
    WStype_FRAGMENT_TEXT_START,
    WStype_FRAGMENT_BIN_START,
    WStype_FRAGMENT,
    WStype_FRAGMENT_FIN,
    } WStype_t;


arduino库下载:

https://apizl6.ctfile.com/dir/18261410-29590696-750079/

https://pan.baidu.com/s/1YVsDLRhYEThaTk4fgXbpIQ



官方项目:

https://github.com/Links2004/arduinoWebSockets/



原文:


WebSocket Server and Client for Arduino Build Status

a WebSocket Server and Client for Arduino based on RFC6455.


Supported features of RFC6455

text frame

binary frame

connection close

ping

pong

continuation frame


Limitations

max input length is limited to the ram size and the WEBSOCKETS_MAX_DATA_SIZE define

max output length has no limit (the hardware is the limit)

Client send big frames with mask 0x00000000 (on AVR all frames)

continuation frame reassembly need to be handled in the application code


Limitations for Async

Functions called from within the context of the websocket event might not honor yield() and/or delay(). See this issue for more info and a potential workaround.

wss / SSL is not possible.


Supported Hardware

ESP8266 Arduino for ESP8266

ESP32 Arduino for ESP32

ESP31B

Particle with STM32 ARM Cortex M3

ATmega328 with Ethernet Shield (ATmega branch)

ATmega328 with enc28j60 (ATmega branch)

ATmega2560 with Ethernet Shield (ATmega branch)

ATmega2560 with enc28j60 (ATmega branch)


Note:

version 2.0 and up is not compatible with AVR/ATmega, check ATmega branch.


Arduino for AVR not supports std namespace of c++.


wss / SSL

supported for:


wss client on the ESP8266

wss / SSL is not natively supported in WebSocketsServer however it is possible to achieve secure websockets by running the device behind an SSL proxy. See Nginx for a sample Nginx server configuration file to enable this.


ESP Async TCP

This libary can run in Async TCP mode on the ESP.

The mode can be activated in the WebSockets.h (see WEBSOCKETS_NETWORK_TYPE define).

ESPAsyncTCP libary is required.


High Level Client API


begin : Initiate connection sequence to the websocket host.

void begin(const char *host, uint16_t port, const char * url = "/", const char * protocol = "arduino");

void begin(String host, uint16_t port, String url = "/", String protocol = "arduino");


onEvent: Callback to handle for websocket events

void onEvent(WebSocketClientEvent cbEvent);


WebSocketClientEvent: Handler for websocket events

void (*WebSocketClientEvent)(WStype_t type, uint8_t * payload, size_t length)


Where WStype_t type is defined as:

typedef enum {

    WStype_ERROR,

    WStype_DISCONNECTED,

    WStype_CONNECTED,

    WStype_TEXT,

    WStype_BIN,

WStype_FRAGMENT_TEXT_START,

WStype_FRAGMENT_BIN_START,

WStype_FRAGMENT,

WStype_FRAGMENT_FIN,

} WStype_t;


文章由爱资料原创本文地址:https://www.apizl.com/archives/view-134339-1.html,转载请以链接形式标明本文地址!
关键字词: