0.2.32 • Published 10 days ago

react-native-kcp v0.2.32

Weekly downloads
-
License
MIT
Repository
github
Last release
10 days ago

react-native-kcp

React Native Implementation of the KCP Protocol.

JavaScript KCP

Code from bruce48x/kcpjs and as3long/kcpjs.

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.

Installation

Install react-native-udp library

npm install react-native-udp

Install react-native-kcp library

npm install react-native-kcp

API

Create Server

Listen

Parameters

ParameterTypeDescription
UDP SocketUdpSocketSocket created by react-native-udp.
CallbackListenCallbackCallback when UDP Session is created.

Note: You will need to make sure to bind the socket before you pass it into the Listen function.

Create Client

Dial

Parameters

ParameterTypeDescription
UDP SocketUdpSocketSocket created by react-native-udp.
OptionsDialOptionsOptions to configure KCP client.

Note: You will need to make sure to bind the socket before you pass it into the Dial function.

DialOptions Properties

PropertyTypeDescription
hoststringServer address.
portnumberServer port.
convnumberSession ID.

Server Example

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}`));
        });
    });
};

Client Example

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);
};
0.2.32

10 days ago

0.2.31

10 months ago

0.2.30

10 months ago

0.1.30

10 months ago

0.1.28

10 months ago

0.1.27

10 months ago

0.1.26

10 months ago

0.1.25

10 months ago

0.1.24

10 months ago

0.1.23

10 months ago

0.1.22

10 months ago

0.1.21

10 months ago

0.1.20

10 months ago

0.1.19

10 months ago

0.1.18

10 months ago

0.1.17

10 months ago

0.1.16

10 months ago

0.1.15

10 months ago

0.1.14

10 months ago

0.1.13

10 months ago

0.1.12

10 months ago

0.1.11

10 months ago

0.1.10

10 months ago

0.1.9

10 months ago

0.1.7

10 months ago

0.1.6

10 months ago

0.1.5

10 months ago

0.1.4

10 months ago

0.1.3

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago