uniapp Implementation of the KCP Protocol.
Code from bruce48x/kcpjs , as3long/kcpjs and koreymadden/react-native-kcp.
Note: The forward error correction (FEC) feature has been removed from the codebase due to the presence of C++ code. Encrytion is also not supported with this package.
npm install react-native-udp
npm install react-native-kcp
Listen
Parameter | Type | Description |
---|---|---|
UDP Socket | UdpSocket | Socket created by react-native-udp. |
Callback | ListenCallback | Callback when UDP Session is created. |
Note: You will need to make sure to bind the socket before you pass it into the Listen function.
Dial
Parameter | Type | Description |
---|---|---|
UDP Socket | UdpSocket | Socket created by react-native-udp. |
Options | DialOptions | Options to configure KCP client. |
Note: You will need to make sure to bind the socket before you pass it into the Dial function.
Property | Type | Description |
---|---|---|
host | string | Server address. |
port | number | Server port. |
conv | number | Session ID. |
import dgram from 'react-native-udp';
import { Listen } from 'react-native-kcp';
export const kcpServer = () => {
const socketInstance = dgram.createSocket({ type: 'udp4' });
socketInstance.bind(port);
const listener = Listen(socketInstance, (session) => {
session.on('recv', (buff: Buffer) => {
const messageFromClient = buff.toString();
console.debug('[MESSAGE RECEIVED FROM CLIENT]:', messageFromClient);
console.debug('[SENDING MESSAGE BACK]');
session.write(Buffer.from(`[GREETINGS FROM HOST]: ${messageFromClient}`));
});
});
};
import dgram from 'react-native-udp';
import { Dial } from 'react-native-kcp';
export const kcpClient = () => {
const socketInstance = dgram.createSocket({ type: 'udp4' });
socketInstance.bind(port);
const socket = Dial(socketInstance, {
conv,
host,
port,
});
socket.on('recv', (buff: Buffer) => {
console.debug('[RECEIVED KCP MESSAGE]:', buff.toString());
});
setInterval(() => {
const message = Buffer.from(new Date().toISOString());
console.debug(`[SENDING MESSAGE]: ${message.toString()}`);
console.debug('[DESTINATION]:', `${socket.host}:${socket.port}`);
socket.write(message);
}, 5000);
};