如何使用 POCO 发送 websocket PONG 响应 [英] How to send websocket PONG response using POCO

查看:51
本文介绍了如何使用 POCO 发送 websocket PONG 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 POCO 1.7.5 设置 websocket 服务器.

I am trying to set up a websocket server using POCO 1.7.5.

来自 POCO 的示例在 here 效果很好.第 111-122 行读取(稍微美化):

The sample from POCO found here works well. Lines 111-122 reads (sligthly prettified):

WebSocket ws(request, response);
char buffer[1024];
int n, flags;

do
{
    n = ws.receiveFrame(buffer, sizeof(buffer), flags);
    ws.sendFrame(buffer, n, flags);
}
while (n > 0 && (flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE);

但是,这种方法不负责通过 pong 帧来回答 ping 帧.如何使用 POCO 正确执行此操作?我尝试了以下方法,但不知道是否正确:

However, this approach does not take care of answering ping frames by pong frames. How do I do this properly using POCO? I tried the following, but I dont know if it is correct:

WebSocket ws(request, response);
char buffer[1024];
int n, flags;

do
{
    n = ws.receiveFrame(buffer, sizeof(buffer), flags);
    if ((flags & WebSocket::FRAME_OP_BITMASK) == WebSocket::FRAME_OP_PING){
        buffer[0] = WebSocket::FRAME_OP_PING;
        ws.sendFrame(buffer, 1, WebSocket::FRAME_OP_PONG);
    }
    else{
        ws.sendFrame(buffer, n, flags);
    }
}               
while (n > 0 && (flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE);

不知道这是否是正确的做法,也无法在网上找到如何做,包括 POCO 文档.websocket RFC 6465 包含大量信息,但我不想去那里,因为我只是想要在这里使用 websocket 作为应用程序员

Dont know if this is the right way of doing it, and cannot find how to do it online, including the POCO documentation. The websocket RFC 6465 holds loads of info, but I dont want to go there, as I just want to use the websocket as an application programmer here

推荐答案

从 RFC 开始,您必须使用 WebSocket::FRAME_OP_PONG 标志发送相同的缓冲区.试试这个:

From RFC you must send the same buffer with WebSocket::FRAME_OP_PONG flag. Try this:

do
{
    n = ws.receiveFrame(buffer, sizeof(buffer), flags);
    if ((flags & WebSocket::FRAME_OP_BITMASK) == WebSocket::FRAME_OP_PING) {
        ws.sendFrame(buffer, n, WebSocket::FRAME_OP_PONG);
    }
    else {
        ws.sendFrame(buffer, n, flags);
    }
}               
while (n > 0 && (flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE);

这篇关于如何使用 POCO 发送 websocket PONG 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆