微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C++ 信号器等待回调

如何解决C++ 信号器等待回调

说明

我有一个使用 singalr 库的 C++ 应用程序。在那里我得到了一个与另一方通信的调用方法。如果我发送请求,我想返回答案(如果有)。我遇到的问题是我无法从调用回调中获取值。我试图在我的发送函数中使用自己的回调,但我也无法访问我的回调函数调用回调之外的任何其他东西。我也尝试使用 future 类来等待回调,但它也不起作用。有人在这里一个简单的解决方案吗?

来源

std::string Send(const std::string methodName,signalr::value args)
{
    if (_connection == nullptr || _connection->get_connection_state() != signalr::connection_state::connected)
    {
        SignalRLog::GetLog()->warn("[signalr,send] {} Could not send due to connection state",methodName);
        return "";
    }

    SignalRLog::GetLog()->info("[signalr,send] {} sending data",methodName);

    std::string answer;
    (*_connection).invoke(methodName,args,[&methodName](const signalr::value& value,std::exception_ptr exception)
    {
        try
        {
            if (exception)
            {
                std::rethrow_exception(exception);
            }

            if (value.is_string())
            {
                answer = value.as_string();
            }
            else
            {
                answer = std::string("hub method invocation has completed");
            }
        }
        catch (const std::exception& e)
        {
            SignalRLog::GetLog()->warn("[signalr] Error while sending data: {}",e.what());
            return std::string("");
        }
    });

    return answer;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。