React Native
React Native
SSL Client Example
See more Socket/SSL/TLS Examples
Demonstrates how to connect to an SSL server, send a simple message, receive a simple response, and disconnect.Chilkat React Native Downloads
import { Socket } from '@chilkat/react-native'
async function chilkatExample() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const socket = new Socket();
const ssl = true;
const maxWaitMillisec = 20000;
// The SSL server hostname may be an IP address, a domain name,
// or "localhost". You'll need to change this:
const sslServerHost = '123.123.88.88';
const sslServerPort = 8123;
// Connect to the SSL server:
try {
await socket.connectAsync(sslServerHost, sslServerPort, ssl, maxWaitMillisec);
} catch {
console.log(socket.lastErrorText);
return;
}
// Set maximum timeouts for reading an writing (in millisec)
socket.maxReadIdleMs = 20000;
socket.maxSendIdleMs = 20000;
// Send a "Hello Server! -EOM-" message:
try {
await socket.sendStringAsync('Hello Server! -EOM-');
} catch {
console.log(socket.lastErrorText);
return;
}
// The server (in this example) is going to send a "Hello Client! -EOM-"
// message. Read it:
let receivedMsg: string;
try {
receivedMsg = await socket.receiveUntilMatchAsync('-EOM-');
} catch {
console.log(socket.lastErrorText);
return;
}
// Close the connection with the server
// Wait a max of 20 seconds (20000 millsec)
await socket.closeAsync(20000);
console.log(receivedMsg);
}