关闭websocket ++连接 [英] Shut down websocket++ connection

查看:654
本文介绍了关闭websocket ++连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASIO模式下使用Websocket ++时,我通过以下方式开始连接:

When using Websocket++ with the ASIO mode, I start my connection with:

boost::shared_ptr<client> x(new client());
x->init_asio();
websocketpp::lib::error_code ec;
client::connection_pt con = x->get_connection(url, ec);
x->connect(con);
new thread(boost::bind(&LocalCallbacks::run, x)); // which just runs x->run()

此模式是从提供的示例中复制的(带有修改的).用户按下按钮取消WebSocket时应如何正确清理?我目前正在做:

This pattern is copied (with modifications) from the examples provided. How should I cleanup properly when a user presses a button to cancel the websocket? I am currently doing:

x->stop();

我也应该打电话给x->close()吗?在调用close之后是否需要等待,然后再调用stop?我需要杀死创建的thread,还是会自动停止?我有报告说当前代码使websocket会话保持打开状态.

Should I also be calling x->close()? Do I need to wait after calling close before calling stop? Do I need to kill the thread that was created, or will that stop automatically? I have had reports of the current code leaving the websocket session open.

推荐答案

下面的教程中介绍了彻底关闭客户端端点的理想方法:

The ideal method for cleanly shutting down a client endpoint is described at this point in the following tutorial:

https://github.com/zaphoyd/websocketpp/blob/master/tutorials/utility_client/utility_client.md#close-all-outstanding-connections-in-websocket_endpoint-destructor

简而言之,您可以通过con->close()关闭连接,而不是关闭端点.这将干净地执行WebSocket和TCP关闭握手(具有可配置的超时,以确保恶意或损坏的客户端不会永远挂起).

In short, rather than closing the endpoint, you close connections via con->close(). This will cleanly perform the WebSocket and TCP closing handshakes (with a configurable timeout to make sure a malicious or broken client doesn't hang forever).

一旦在端点上运行的所有连接都关闭,端点将停止运行,并返回其run()方法. (永久模式除外.如果使用永久模式,请在关闭连接之前将其关闭).这样,您可以在关闭连接后在运行端点的线程上使用thread.join(),以等待它们全部关闭.

Once all connections running on an endpoint are closed the endpoint stops running and its run() method(s) return. (Except in the case of perpetual mode. If you are using perpetual mode turn it off before closing connections). As such, you can use thread.join() on your thread running the endpoint after closing connections to wait until they are all closed.

此过程不是瞬时的,因为它需要进行几次网络往返来清理所有内容.如果出于任何原因必须立即结束 ,请致电endpoint.stop().这将立即停止处理io_service作业.这将使所有现有连接处于不确定状态. WebSocket连接似乎不会在另一端关闭,但在尝试写入时,它们会收到损坏的TCP/不干净的断开连接错误或超时错误.您的本地操作系统可能会保持套接字打开以及占用资源或端口.您的本地WebSocket ++连接将立即使用不干净的断开关闭代码调用它们的关闭/失败处理程序.

This process is not instantaneous, as it requires a few network round trips to clean everything up. If you must end right now for whatever reason then call endpoint.stop(). This will immediately cease processing io_service jobs. This will leave all existing connections in a limbo state. WebSocket connections will not appear to have closed on the other end but when they try and write they will get a broken TCP / unclean disconnect error or timeout error. Your local OS may keep sockets open as well tying up resources or ports. You local WebSocket++ connections will all immediately have their close/fail handlers called with an unclean disconnect close code.

这篇关于关闭websocket ++连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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