React Native
React Native
Disconnect All SSH Tunnel Clients
See more SSH Tunnel Examples
Demonstrates the Chilkat SshTunnel.DisconnectAllClients method, which disconnects all active tunnel clients while leaving the listener and SSH connection available. The only argument (waitForThreads) selects whether to wait for the client threads to exit.
Background: This is the complement of
StopAccepting: it clears out current clients but keeps accepting new ones, so the listener and SSH transport stay up. It is useful for forcibly resetting stuck or unwanted connections without tearing down the whole tunnel — for example, dropping all sessions before a maintenance action while leaving the tunnel ready to serve fresh connections immediately afterward.Chilkat React Native Downloads
import { SshTunnel } from '@chilkat/react-native'
async function chilkatExample() {
// Demonstrates the SshTunnel.DisconnectAllClients method, which disconnects all active tunnel
// clients while leaving the listener and SSH connection available. The only argument
// (waitForThreads) selects whether to wait for the client threads to exit.
const tunnel = new SshTunnel();
tunnel.destHostname = 'db.internal.example.com';
tunnel.destPort = 5432;
const sshPort = 22;
try {
await tunnel.connectAsync('ssh.example.com', sshPort);
} catch {
console.log(tunnel.lastErrorText);
return;
}
// Normally you would not hard-code the password in source. You should instead obtain it
// from an interactive prompt, environment variable, or a secrets vault.
const password = 'mySshPassword';
try {
await tunnel.authenticatePwAsync('mySshLogin', password);
} catch {
console.log(tunnel.lastErrorText);
return;
}
const listenPort = 1080;
try {
await tunnel.beginAcceptingAsync(listenPort);
} catch {
console.log(tunnel.lastErrorText);
return;
}
// ... clients connect and transfer data ...
// Disconnect all current clients. The listener keeps running and will accept new clients.
const waitForThreads = true;
try {
tunnel.disconnectAllClients(waitForThreads);
} catch {
console.log(tunnel.lastErrorText);
return;
}
console.log('All current clients disconnected; still accepting new ones.');
const waitForThreadExit = true;
try {
tunnel.closeTunnel(waitForThreadExit);
} catch {
console.log(tunnel.lastErrorText);
return;
}
}