This commit is contained in:
aozhiwei 2023-04-18 13:29:46 +08:00
parent c79b8453a9
commit cb37f0db76

View File

@ -2,6 +2,8 @@ const protobuf = require('protobufjs');
const kcp = require('./node_modules/node-kcp/build/Release/kcp');
const dgram = require('dgram');
const update_interval = 200;
function prettyJsonEncode(obj) {
return JSON.stringify(obj, "", " ");
}
@ -29,6 +31,7 @@ class ClientNet {
this.kcpObj.nodelay(0, 0, 0, 0);
this.kcpObj.output((data, size, context) => {
console.log('kcp output', data, size, context);
this.conn.send(data, 0, size, context.port, context.address);
});
@ -38,6 +41,11 @@ class ClientNet {
this.conn.on('message', (msg, rinfo) => {
this.kcpObj.input(msg);
});
setInterval(() => {
this.kcpObj.update(Date.now());
}, update_interval);
}
selfRegisterMsgHandle(msgName) {
@ -175,8 +183,10 @@ class ClientNet {
buf.writeUInt8('K'.charCodeAt(0), 6);
buf.writeUInt8('S'.charCodeAt(0), 7);
buf.writeInt32LE(0, 8);
//this.conn.sendBinary(Buffer.concat([buf, msgBuf]));
console.log(name, msg, (Buffer.concat([buf, msgBuf])).length);
const buff = Buffer.concat([buf, msgBuf]);
this.kcpObj.send(buff, buff.length);
console.log(name, msg, buff.length);
}
}