# forwarder-node

Node.js binding for [forwarder](https://github.com/voyagingmk/forwarder)


# Install

npm i forwarder-node

# Requirement

cmake-js >= 3.3.1

You should install [cmake-js](https://www.npmjs.com/package/cmake-js) by running the following command:

npm i cmake-js -g

# Usage

## Basically, create a forwader object

```js
let forwarder = require('forwarder-node');
let fwd = new forwarder.Forwarder();
```

## member functions

```js
fwd.setupLogger(filename);
```

```js
fwd.setDebug(enabled);
```

```js
// useful in client mode
fwd.initProtocolMap(
    /* 
    {protocol: Cmd}
    {2: "Process"}
    */
);
```

```js
const option = {
    "id": 1, // server's unique ID
    "desc": "description of this server",
    "netType": "enet", // or "ws"
    "port": 9999,
    "peers": 3000,
    "encrypt": true, // optional. AES-128 encryption. if encrypt is true, then must provide an encryptkey
    "encryptkey": "1234567812345678", // length must be 16 (128 bits)
    "compress": true, // optional
    "base64": true, // optional
    // below options are used for client mode
    "isClient": true,
    "address": "localhost",
    "reconnect": true
};
const serverId = fwd.createServer(option);
```

```js
fwd.pollOnce(serverId);
```


```js
const evt = fwd.getCurEvent();
```

```js
let serverId = fwd.getCurProcessServerID();
```

```js
let clientId = fwd.getCurProcessClientID();
```

```js
fwd.sendText(serverId, clientId, "hello,world", forwardClientId);
```

```js
fwd.sendBinary(serverId, clientId, new Buffer("hello,world"), forwardClientId);
```


```js
let packet = fwd.getCurProcessPacket();// packet is JS Buffer
```

```js
fwd.stat();
```

# Tutorial

run the three js file: 

test_server.js 

test_client1.js

test_client2.js

# Reference

Please go to the [forwarder](https://github.com/voyagingmk/forwarder) homepage.
