From 55516aee64107b55459745f59e67cc050deef796 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Mon, 17 Apr 2023 20:29:32 +0800 Subject: [PATCH] 1 --- server/tools/robot/kcpclient/clientnet.js | 24 +++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/server/tools/robot/kcpclient/clientnet.js b/server/tools/robot/kcpclient/clientnet.js index d82ebcba..b649bdf0 100644 --- a/server/tools/robot/kcpclient/clientnet.js +++ b/server/tools/robot/kcpclient/clientnet.js @@ -1,5 +1,6 @@ - const protobuf = require('protobufjs'); -const ws = require('nodejs-websocket'); +const protobuf = require('protobufjs'); +var kcp = require('node-kcp'); +const dgram = require('dgram'); function prettyJsonEncode(obj) { return JSON.stringify(obj, "", " "); @@ -19,6 +20,25 @@ class ClientNet { this.recvBuf = Buffer.alloc(0); this.uniqId = 1000; this.msgHandlerMap = new Map(); + console.log(kcp); + this.kcpObj = new kcp.KCP(123, + { + 'address': '192.168.100.45', + 'port': 7602 + }); + this.conn = dgram.createSocket('udp4'); + + this.kcpObj.nodelay(0, 0, 0, 0); + this.kcpObj.output((data, size, context) => { + this.conn.send(data, 0, size, context.port, context.address); + }); + + this.conn.on('error', (err) => { + console.log('client error:' + err); + }); + this.conn.on('message', (msg, rinfo) => { + this.kcpObj.input(msg); + }); } selfRegisterMsgHandle(msgName) {