node-kcp-x
Version:
KCP protocol for Node.js
108 lines (75 loc) • 2.16 kB
Markdown
<p align="center">
| <b>English</b> | <a href="./README_CN.md"><b>简体中文</b></a> |
</p>
[]: https://api.travis-ci.org/leenjewel/node-kcp.svg?branch=master
[]: https://travis-ci.org/leenjewel/node-kcp
[](https://github.com/skywind3000/kcp) for Node.js
A fork of [node-kcp](https://github.com/leenjewel/node-kcp), rewritten using [node-addon-api](https://github.com/nodejs/node-addon-api)
- **High Performance**: 30%-40% reduction in average RTT compared to traditional ARQ protocols like TCP
- **Low Latency**: Maximum RTT reduced by 3x compared to TCP
- **Lightweight**: Distributed as a single source file
- **Modern API**: Built with node-addon-api for better Node.js compatibility
- **Cross Platform**: Supports Windows, Linux, and macOS
```bash
npm install node-kcp-x
```
```bash
npm install -g node-gyp prebuildify
```
```bash
git clone https://github.com/bruce48x/node-kcp
cd node-kcp
npm i
npm run build
```
```bash
npm run install
node test/udpserver.js
node test/udpclient.js
```
```bash
node examples/server.js
node examples/client.js
```
```bash
node examples/stream-server.js
node examples/stream-client.js
```
```javascript
const kcp = require('node-kcp-x');
const Kcp = kcp.KCP;
// Create KCP instance
const kcpObj = new Kcp(conv, context);
// Set output callback
kcpObj.output((data, size, context) => {
// Send data to network
});
// Send data
kcpObj.send(buffer);
// Receive data
const buffer = kcpObj.recv();
// Update KCP state
kcpObj.update(timestamp);
// Check next update time
const nextUpdate = kcpObj.check(timestamp);
```
If you want to use [node-kcp](https://github.com/leenjewel/node-kcp) in [pomelo](https://github.com/NetEase/pomelo/) server, you need [pomelo-kcp](https://github.com/leenjewel/pomelo-kcp)
Apache-2.0