3

我正在开发用于客户端和服务器之间异步通信的boost websockets 。

现在我正在打印程序期间经过的时间boost::timer::auto_cpu_timer。它以秒为单位显示经过的时间。

我的程序片段如下:

此函数数据发送到 websocket:

void WebSocketSession::WriteSocket(beast::error_code ec) {
    if (ec)
         return fail(ec, "ssl_handshake");
    cout << "Authorised" <<endl;
    
    //Start the timer
    BoostTimer.start();
    
    // Send the Data message
    ws_.async_write(net::buffer(DATAMSG),
            bind(&WebSocketSession::ReadSocket, shared_from_this(), placeholders::_1,placeholders::_2));
}

此函数读取websocket 响应

void WebSocketSession::ReadSocket(beast::error_code ec, size_t bytes_transferred) {
    boost::ignore_unused(bytes_transferred);
    if (ec)
         return fail(ec, "WriteSocket Failed");

    cout << "Time Elapsed before reading WS : " << TimeElapsed() << endl;

    try {
        ws_.read(ReadBuffer_);
    } catch (exception *e) {
        cerr << "Error: " << e->what() << endl;
    }

    cout << "Time Elapsed after reading WS : " << TimeElapsed() << endl;
    
    // Display the buffer into stringstream
    cout << beast::buffers(ReadBuffer_.data());
    
    // Clear the websocket buffer
    ReadBuffer_.consume(ReadBuffer_.size());

    cout << "Time Elapsed before moving ahead : " << TimeElapsed() << endl;

    // Decision tree

    // IsGstFileWriteDone() gives "true" when the file is written... that file is not related to this context. An event is pushed saying FILE_WRITE_DONE
        if (mIbmWatsonobj->IsGstFileWriteDone()){
            cout<< "Going to CloseSocket" << endl;
            WebSocketSession::CloseSocket(ec, 0);
        }else{
            cout<< "Going to ReadSocket" << endl;
            WebSocketSession::ReadSocket(ec, 0);
        }
}

此函数关闭Web 套接字

void WebSocketSession::CloseSocket(beast::error_code ec, size_t bytes_transferred) {
    boost::ignore_unused(bytes_transferred);
    if (ec)
        return fail(ec, "ReadSocket Failed");

    cout << "CLOSING" <<endl;
    
    // Close the WebSocket connection
    ws_.close(websocket::close_code::normal);
}

这是我的程序输出的样子: 从 websocket 收到的响应显示为灰色(输出cout << beast::buffers(ReadBuffer_.data());)其余是打印在程序中各个位置的 couts。经过的时间以秒为单位

读取 WS 之前经过的IBM 授权
时间:0
读取 WS 之后经过的时间:0.3

{  
    "state": "listening"  
}

前进前经过的时间:0.3
转到 ReadSocket
阅读 WS 前经过的
时间:0.3 阅读 WS 后经过的时间:2.1

{
   "results": [
      {
         "alternatives": [
            {
               "confidence": 0.89, 
               "transcript": "several tornadoes touch down as a line of severe some "
            }
         ], 
         "final": true
      }
   ], 
   "result_index": 0
}

前进前经过的时间:2.1
转到 ReadSocket
阅读 WS 前经过的
时间:2.1 阅读 WS 后经过的时间:2.1

{
   "state": "listening"
}

前进前经过的时间:2.1
转到 ReadSocket
阅读 WS 前经过的时间:2.1

事件推送:FILE_WRITE_DONE

阅读 WS 后经过的时间:34

{
   "error": "Session timed out."
}

前进前经过的时间:34

//程序退出 -1

问题:

几秒钟后2.1,程序再次进入 ReadSocket,在该处ws_.read(ReadBuffer_);阻塞执行近 32 秒,直到它从套接字接收到某些东西,在这种情况下,它接收到“会话超时”。

当这个块打开 5 秒时,我怎样才能移动到 CloseSocket。也就是说,如果在任何时候我ws_.read阻止我的代码超过 5 秒,我想把我的行为说出来,比如 CloseSocket。

4

1 回答 1

0

或者您可以只使用 1.70.0 中的新 Boost.Beast,它支持 websocket 操作的内置超时:https ://www.boost.org/doc/libs/1_70_0/libs/beast/doc/html/beast/ using_websocket/timeouts.html

于 2019-04-19T01:21:22.197 回答