当前位置: 首页>>代码示例>>C++>>正文


C++ client::start_perpetual方法代码示例

本文整理汇总了C++中client::start_perpetual方法的典型用法代码示例。如果您正苦于以下问题:C++ client::start_perpetual方法的具体用法?C++ client::start_perpetual怎么用?C++ client::start_perpetual使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在client的用法示例。


在下文中一共展示了client::start_perpetual方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main()
{
	// Remove superfluous logging
	test_client.clear_access_channels(websocketpp::log::alevel::all);
    test_client.clear_error_channels(websocketpp::log::elevel::all);
        
	// Normal endpoint setup (just as you would with the regular websocketpp::client)
	test_client.init_asio();
	// The endpoint must be perpetual. TODO look at supporting non perpetual (will have to use .reset())
	test_client.start_perpetual();
	// Start spinning the thread
	test_thread.reset(new websocketpp::lib::thread(&client::run, &test_client));
	
	// Done boilerplate initialization, now our connection code:
	websocketpp::lib::error_code ec;
	client::connection_ptr con = test_client.get_connection("ws://some.fake.server.that.should.not.exist:9001", ec);
	if(ec)
	{
		throw ec;
	}
	// Setup any retry_data settings here
	con->m_retry = true;		// Indicates that we do want to attempt to retry connecting (if first attempt fails)
	con->m_retry_delay = 300;	// Will wait 300ms between attempts
	con->m_max_attempts = 10;	// Will stop attempting to retry after 10 attempts
	
	try
	{
		// Delibrately call connect without setting a configure_handler
		// This shows the importance that when setting handlers such as
		// open, message, closed... handlers it must be done within the
		// configure handler because each retry attempt creates a new
		// connection using get_connection(...)
		test_client.connect(con);
    } catch (const websocketpp::exception & e) {
        std::cout << e.what() << std::endl;
    }
    
    // Must setup a configure handler, where we register specific connection items (eg. handlers, etc)...
	con->set_configure_handler(bind(&configure_con, &test_client, ::_1));
    
    std::cout << "Sleeping for 4 seconds to simulate a server that we cannot connect to" << std::endl;
    std::cout << "and 4 seconds is enough for the 10 retries (@ 300ms delay) to run it's course" << std::endl;
    
    // Now connect will start attempting to connect
    test_client.connect(con);
    
    std::this_thread::sleep_for(std::chrono::seconds(6));
    
    test_client.stop_perpetual();
    test_thread->join();
    return 0;
}
开发者ID:maldworth,项目名称:websocketpp-retry-client,代码行数:52,代码来源:example2.cpp


注:本文中的client::start_perpetual方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。