Coder Social home page Coder Social logo

Comments (3)

HsuJv avatar HsuJv commented on May 26, 2024

@fullee
run_local mode is not designed to be running in the multiple-thread/coroutine context.
Please use run_backend to get the compatibility of async/await keywords
Examples can be found in exec_backend

Brs.

from ssh-rs.

HsuJv avatar HsuJv commented on May 26, 2024

The following codes can be compiled with axum server but I didn't try 'em out as a real WebSocket server.

async fn ws_handler(ws: WebSocketUpgrade) -> impl IntoResponse {
    ws.protocols(["binary"]).on_upgrade(handle_client)
}

pub async fn handle_client(mut client: WebSocket) {
    let mut ssh = ssh_rs::ssh::create_session()
        .username("ubuntu")
        .password("password")
        .connect("127.0.0.1:22")
        .unwrap()
        .run_backend();

    loop {
        if let Some(msg) = client.recv().await {
            if let Ok(msg) = msg {
                match msg {
                    Message::Text(t) => {
                        println!("client sent str: {:?}", t);
                        // let exec = ssh_cloned.lock().unwrap().open_exec().unwrap();
                        let exec = ssh.open_exec().unwrap();
                        exec.send_command(&t).unwrap();
                        let vec = exec.get_result().unwrap();
                        if client
                            .send(Message::Text(String::from_utf8(vec).unwrap()))
                            .await
                            .is_err()
                        {
                            println!("client disconnected");
                        };
                    }
                    Message::Binary(_) => {
                        println!("client sent binary data");
                    }
                    Message::Ping(_) => {
                        println!("socket ping");
                    }
                    Message::Pong(_) => {
                        println!("socket pong");
                    }
                    Message::Close(_) => {
                        println!("client disconnected");
                        return;
                    }
                }
            } else {
                println!("client disconnected");
                return;
            }
        } else {
            println!("client disconnected");
            return;
        }
    }
}

from ssh-rs.

fullee avatar fullee commented on May 26, 2024

ok,i love you

from ssh-rs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.