如何访问awc WebSocket客户端的连接状态?

huangapple go评论56阅读模式
英文:

How to access the connection status of an awc websocket client?

问题

我已经设置了一个基本的Actix Web服务器,并且成功地在我的测试中使用`awc::client::Client`创建了WebSocket连接

现在我正在尝试测试当我告诉服务器重置应用程序的状态时,它是否关闭了所有的WebSocket连接

我目前计划的测试如下:

```rs
#[test]
async fn reset_game_works_basic() {
    let server: TestServer = test_fixtures::get_test_server();
    let (_resp, mut chris_connection) = Client::new()
        .ws(server.url("/join-game?username=Chris"))
        .connect()
        .await
        .unwrap();

    let _ = server.post("/reset-game").send().await;
    let websocket_connected = chris_connection.websocket_connected_status();
                                            // ^^^^ 不是一个真实的函数 
    assert_eq!(websocket_connected, false);
}

awc websocket文档中,我只能找到一个CloseCode枚举,但那似乎是用于客户端告诉服务器它为什么关闭连接。

我还试图使用is_write_ready()检查连接是否仍然打开,但它返回了true

我已经用Postman进行了手动测试,当你向/reset-game发送POST请求时,客户端会被断开连接。

我应该如何询问客户端它是否仍然保持着开放的连接?


<details>
<summary>英文:</summary>

I have a basic Actix Web server set up, and I have successfully been creating websocket connections in my tests using `awc::client::Client`. 

Now I am trying to test that my server is closing all of the websocket connections when I tell it reset the status of the app.


My current planned test for this is:

```rs
#[test]
async fn reset_game_works_basic() {
    let server: TestServer = test_fixtures::get_test_server();
    let (_resp, mut chris_connection) = Client::new()
        .ws(server.url(&quot;/join-game?username=Chris&quot;))
        .connect()
        .await
        .unwrap();

    let _ = server.post(&quot;/reset-game&quot;).send().await;
    let websocket_connected = chris_connection.websocket_connected_status();
                                            // ^^^^ Not a real function 
    assert_eq!(websocket_connected, false);
}

From the awc websocket docs, I have only been able to find a CloseCode enum, but that seems to be used for the client to tell the server why it is closing the connection.

I also fruitlessly tried to check if the connection was open by using is_write_ready(), but that returned true.

I have done manual testing with Postman, and the clients are being disconnected when you send a post request to /reset-game.

How should I ask the client if it still has an open connection?

答案1

得分: 0

当WebSocket连接关闭时,.next().await将返回None,当它处于打开状态时,它将返回Some,即使没有下一条消息!

所以,这个测试在工作代码的情况下成功通过,并且如果我移除实际关闭WebSocket的代码,测试就会失败!

#[test]
async fn reset_game_works_basic() {
    let server: TestServer = test_fixtures::get_test_server();
    let (_resp, mut chris_connection) = Client::new()
        .ws(server.url("/join-game?username=Chris"))
        .connect()
        .await
        .unwrap();

    let _ = server.post("/reset-game").send().await;

    let _join = chris_connection.next().await;

    let no_message = chris_connection.next().await;

    let websocket_disconnected = no_message.is_none();

    assert_eq!(websocket_disconnected, true);
}

这显然对问题很脆弱,如果在断开连接之前使WebSocket发送第二条消息,它就会出问题,但这是我尝试在这里解决的问题之外的另一个问题。

英文:

I ended up finding a working (albiet fragile) test for this.

When the websocket connection is closed, .next().await will return None, and when it is open, it will return Some, even if there is no next message!

So this test successfully passes with the working code, and fails if I remove the code that actually closes the websocket!

#[test]
async fn reset_game_works_basic() {
    let server: TestServer = test_fixtures::get_test_server();
    let (_resp, mut chris_connection) = Client::new()
        .ws(server.url(&quot;/join-game?username=Chris&quot;))
        .connect()
        .await
        .unwrap();

    let _ = server.post(&quot;/reset-game&quot;).send().await;

    let _join = chris_connection.next().await;

    let no_message = chris_connection.next().await;

    let websocket_disconnected = no_message.is_none();

    assert_eq!(websocket_disconnected, true);
}

It certainly is fragile to the problem that if I make the websocket send a second message before disconnecting it will break, but that is a separate problem to the one I was trying to solve here.

huangapple
  • 本文由 发表于 2023年2月6日 02:52:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75354720.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定