0.0.4 • Published 7 months ago
socketconnectioncapacitor v0.0.4
socketconnectioncapacitor
TCP/UDP socket connection plugin for Capacitor applications.
Installation
npm install socketconnectioncapacitor
npx cap sync
Requirements
- iOS 14.0+
- Android API level 23+ (Android 6.0+)
- Capacitor 6.2.0+
Features
- TCP socket support
- UDP socket communication
- iOS and Android platform support
- Native socket implementations
- Event-based message handling
API Documentation
echo(...)
echo(options: { value: string; }) => Promise<{ value: string; }>
Param | Type |
---|---|
options | { value: string; } |
Returns: Promise<{ value: string; }>
Usage
Here is an example of how to use the socketconnectioncapacitor
plugin in an Ionic Capacitor project:
import { Plugins } from '@capacitor/core';
const { SocketConnectionCapacitor } = Plugins;
async function connectToServer() {
try {
const result = await SocketConnectionCapacitor.connect({
host: 'example.com',
port: 8080,
ssl: false
});
console.log('Connected:', result);
} catch (error) {
console.error('Connection failed:', error);
}
}
async function sendMessage(message: string) {
try {
const result = await SocketConnectionCapacitor.sendMessage({ message });
console.log('Message sent:', result);
} catch (error) {
console.error('Failed to send message:', error);
}
}
function listenForMessages() {
SocketConnectionCapacitor.addListener('messageReceived', (info: any) => {
console.log('Message received:', info.message);
});
}
async function disconnectFromServer() {
try {
const result = await SocketConnectionCapacitor.disconnect();
console.log('Disconnected:', result);
} catch (error) {
console.error('Disconnection failed:', error);
}
}
// Example usage
connectToServer();
listenForMessages();
sendMessage('Hello, server!');
disconnectFromServer();
Development
Run the following commands to set up for development:
npm install
npx cap sync
Testing
Run the test suite with:
npm run test
This will run:
- iOS tests (
verify:ios
) - Android tests (
verify:android
) - Web tests (
verify:web
)
Code Style
Format code using:
npm run format
License
MIT
Author
touya.r@gmail.com