Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to close websocket after it has been moved into select? #128

Closed
hp8wvvvgnj6asjm7 opened this issue Jan 7, 2024 · 12 comments
Closed

Comments

@hp8wvvvgnj6asjm7
Copy link

hp8wvvvgnj6asjm7 commented Jan 7, 2024

#[tokio::main]
async fn main(){

    let base = String::from("wss://");

    let (stream, _) = connect_async(&base).await.unwrap();

    let (tx, rx) = mpsc::unbounded_channel();

    let (write, read) = stream.split();

    let ws_to_stdout  = { 
        
        read.for_each(|message| async {

            // dbg!(&message);

            let thread_tx = tx.clone();

            if !message.as_ref().unwrap().is_ping() {

                let data:X= serde_json::from_str(&message.unwrap().into_text().unwrap()).unwrap(); 
                
                // dbg!(&data);
                
                tx.send(data).unwrap();
            }
        })
    };

    pin!(rx, ws_to_stdout);

    let (a, b, c) = tokio::join!(async {

        loop {

            let x = rx.recv().await;

            // dbg!(&x);
        }

    }, ws_to_stdout, async {

        tokio::time::timeout(Duration::from_secs(5), async {

            

            // let x = ws_to_stdout.as_mut();

            

            write.close(Some(CloseFrame { code: CloseCode::Normal, reason: String::from("").into() })).await.unwrap();
            
        }).await

    });

    println!("end.");
    
}
@sdroege
Copy link
Owner

sdroege commented Jan 7, 2024

I'm not sure what the exact question here is. You'd close it by calling close() on it, or by dropping the websocket (i.e. in your case the read and write side of the socket).

What's the problem you're running into?

@hp8wvvvgnj6asjm7
Copy link
Author

I don't know on which object I have to call the close method into which i put Some(CloseFrame { code: CloseCode::Normal, reason: String::from("").into() })

@hp8wvvvgnj6asjm7
Copy link
Author

I can't use the websocket because it is moved into the join! macro

@sdroege
Copy link
Owner

sdroege commented Jan 7, 2024

You can write a close frame.

@hp8wvvvgnj6asjm7
Copy link
Author

   |
107 |             stream.send(Some(CloseFrame { code: CloseCode::Normal, reason: String::from("").into() }));
    |             ^^^^^^^^^^^^-----------------------------------------------------------------------------^
    |                         |
    |                         this argument influences the return type of `send

@hp8wvvvgnj6asjm7
Copy link
Author

 write.send(Some(CloseFrame { code: CloseCode::Normal, reason: String::from("").into() }));
    |             ^^^^^^^^^^^-----------------------------------------------------------------------------^
    |                        |
    |                        this argument influences the return type of `send

@hp8wvvvgnj6asjm7
Copy link
Author


109 |             write.close(CloseFrame { code: CloseCode::Normal, reason: String::from("").into() });
    |                   ^^^^^ -----------------------------------------------------------------------
    |                         |
    |                         unexpected argument of type `CloseFrame<'_>`
    |                         help: remove the extra argument

@hp8wvvvgnj6asjm7
Copy link
Author

I guess this is the correct way ?

    write.send(
                
                Message::Close(Some(CloseFrame { code: CloseCode::Normal, reason: String::from("").into() }))
            ).await;

@hp8wvvvgnj6asjm7
Copy link
Author

It just causes a panic.

@sdroege
Copy link
Owner

sdroege commented Jan 7, 2024

What panic?

@hp8wvvvgnj6asjm7
Copy link
Author

Thanks a lot, I think I got the close frame right, but because I'm not parsing the incoming data correctly the panic is caused inside the for_each.

@sdroege
Copy link
Owner

sdroege commented Jan 8, 2024

Great :) So I think this can be closed? Please re-open if there's still some kind of problem

@sdroege sdroege closed this as not planned Won't fix, can't repro, duplicate, stale Jan 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants