npm install forwarder-nodeNode.js binding for forwarder
npm i forwarder-node
cmake-js >= 3.3.1
You should install cmake-js by running the following command:
npm i cmake-js -g
``js`
let forwarder = require('forwarder-node');
let fwd = new forwarder.Forwarder();
`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();
run the three js file:
test_server.js
test_client1.js
test_client2.js
Please go to the forwarder homepage.