STM32——WebSocket

本文档记录了如何在STM32设备上实现WebSocket服务器,以解决HTTP/HTTPS通信时处理大数据报文导致延迟的问题。通过使用WebSocket,可以实现更高效、实时的双向通信。提供的代码适用于STM32F107VET6+LAN8720以及STM32F407VGT6+LAN8720(基于rt-thread)的系统,但不完整,需根据实际需求进行补充。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目的:
该文章主要作用是记下设备实现websocket server的代码

原因:
一般打开web页面时,不刷新页面,用http/https也是能通信的;
但有个问题:每次web页面发信息到设备时,是以报文的形式发出,报文的数据是很长的,导致处理数据的时间变长

结果:
对这方面有要求的然后又不知怎么优化的,就直接使用websocket

说明:
代码如下,代码不完整,需根据自己的代码补充
stm32f107vet6+lan8720上能使用,stm32f407vgt6+lan8720(rt-thread)上能使用
代码是参考w5500的例程



//---------------------------------------
这里添加需要的头文件,我已删除原来的头文件
//---------------------------------------
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/*
 *  Define the circular shift macro
 */
#define SHA1CircularShift(bits,word)   ((((word) << (bits)) & 0xFFFFFFFF) | ((word) >> (32-(bits))))

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/*
 *  This structure will hold context information for the hashing
 *  operation
 */
typedef struct SHA1Context
{
   
    unsigned Message_Digest[5]; /* Message Digest (output)          */

    unsigned Length_Low;        /* Message length in bits           */
    unsigned Length_High;       /* Message length in bits           */

    unsigned char Message_Block[64]; /* 512-bit message blocks      */
    int Message_Block_Index;    /* Index into message block array   */

    int Computed;               /* Is the digest computed?          */
    int Corrupted;              /* Is the message digest corruped?  */
} SHA1Context;

#define WS_SERVER_PORT  1818

char tx_buf_web2[1024]={
   0x00,};
int weblen=0;

/* Pulic variables */
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/*
 *  SHA1ProcessMessageBlock
 *
 *  Description:
 *      This function will process the next 512 bits of the message
 *      stored in the Message_Block array.
 *
 *  Parameters:
 *      None.
 *
 *  Returns:
 *      Nothing.
 *
 *  Comments:
 *      Many of the variable names in the SHAContext, especially the
 *      single character names, were used because those were the names
 *      used in the publication.
 *
 *
 */
void SHA1ProcessMessageBlock(SHA1Context *context)
{
   
    const unsigned K[] =            /* Constants defined in SHA-1   */
    {
   
        0x5A827999,
        0x6ED9EBA1,
        0x8F1BBCDC,
        0xCA62C1D6
    };
    int         t;                  /* Loop counter                 */
    unsigned    temp;               /* Temporary word value         */
    unsigned    W[80];              /* Word sequence                */
    unsigned    A, B, C, D, E;      /* Word buffers                 */

    /*
     *  Initialize the first 16 words in the array W
     */
    for(t = 0; t < 16; t++)
    {
   
        W[t] = ((unsigned) context->Message_Block[t * 4]) << 24;
        W[t] |= ((unsigned) context->Message_Block[t * 4 + 1]) << 16;
        W[t] |= ((unsigned) context->Message_Block[t * 4 + 2]) << 8;
        W[t] |= ((unsigned) context->Message_Block[t * 4 + 3]);
    }

    for(t = 16; t < 80; t++)
    {
   
       W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
    }

    A = context->Message_Digest[0];
    B = context->Message_Digest[1];
    C = context->Message_Digest[2];
    D = context->Message_Digest[3];
    E = context->Message_Digest[4];

    for(t = 0; t < 20; t++)
    {
   
        temp =  SHA1CircularShift(5,A) +
                ((B & C) | ((~B) & D)) + E + W[t] + K[0];
        temp &= 0xFFFFFFFF;
        E = D;
        D = C;
        C = SHA1CircularShift(30,B);
        B = A;
        A = temp;
    }

    for(t = 20; t < 40; t++)
    {
   
        temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值