javascript WebSocket:C服务器-->浏览器无效的帧头

dfddblmv  于 5个月前  发布在  Java
关注(0)|答案(1)|浏览(76)

亲爱的stackoverflow论坛,
我的C服务器和客户端(JavaScript)之间的通信有问题。我使用Rasperry Pi 4作为服务器,Chrome作为浏览器(我也用Firefox和Edge测试了我的东西)。
我的问题是握手的帧头是什么样子的。握手应该将通信协议从HTTP更改为WebSocket。我的头看起来像这样:

GET / HTTP/1.1
Host: 192.168.1.100:8000
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/119.0.0.0 Safari/537.36
Upgrade: websocket
Origin: http://192.168.1.100
Sec-WebSocket-Version: 13
Accept-Encoding: gzip, deflate
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
Sec-WebSocket-Key: 6Hw/S8rXFY4ExVDMGWWn6Q==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits

字符串
来自服务器的应答(附上生成应答请求的代码):

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: 57dCnQc8YJrJ51osdI/I71QHYi4=


现在我在浏览器中得到消息:

script.js:8 WebSocket connection to 'ws://192.168.1.100:8000/' failed: Invalid frame header.


在wireshark中,我可以看到帧头看起来和上面的一样。浏览器(客户端)用红色突出显示的消息进行响应:响应上设置了重置标志。
有没有人看到帧头中的错误?或者哈希码是错误的?
图比

1wnzp6jl

1wnzp6jl1#

好吧,我找到了一个解决方案,我有一个错误的处理输入数据。数据必须像这样解码:

int rxDataLen = 0;
char rxBuf[rxBufferSize];
char selectedObject;
int16_t controlledValue;
WH_Status_t ret = OK;
uint8_t k = 0;

char rxBuf[rxBufferSize];
int rx_data_len = recv (comSockId, (void *)rxBuf, rxBufferSize, MSG_DONTWAIT);
        // Has a new WebSocket message have been received
        if (rx_data_len > 0) {
            rxBuf[rx_data_len] = '\0';
            // Is the message a handshake request
            if (strncmp(rxBuf, "GET", 3) == 0) {
                printf("%s\n", rxBuf);
                // Yes -> create the handshake response and send it back
                char response[WS_HS_ACCLEN];
                get_handshake_response(rxBuf, response);
                send(comSockId, (void *)response, strlen(response), 0);
                printf("Handshake done\n");
            }
            else {
                /* No -> decode incoming message,
                process the command and
                send back an acknowledge message */
                char command[rx_data_len];
                decode_incoming_request(rxBuf, command);
                command[strlen(command)] = '\0';

                /* Check, if the website has been closed. */
                if (strncmp(command, "close", 5) == 0){
                    printf("Cliend closed connection.\n");
                    return closeClient;
                }

                // Decode the message string to control the webhouse.
                analyseMessage((char*)&command, &selectedObject, &controlledValue);

                char response[6] = {0};
                sendMessage((char*)&(response), &selectedObject, actualTemperature, alarmState);
                printf("Answer: %s\n", response);
                char codedResponse[strlen(response)+1];
                code_outgoing_response (response, codedResponse);
                send(comSockId, (void *)codedResponse,
                strlen(codedResponse), 0);
            }
        }

字符串
当一个连接处于活动状态时,你必须在这个代码行中循环,直到连接关闭或者你从服务器端关闭它。这样,就不会再有无效的帧头了。

相关问题