添加用户输入消息支持

This commit is contained in:
azw 2023-08-16 21:19:06 +08:00
parent 9b7ce4eb86
commit b0ce39ec54

View File

@ -1,3 +1,4 @@
const readline = require('readline');
const protobuf = require('protobufjs');
const ws = require('nodejs-websocket');
@ -19,6 +20,13 @@ class ClientNet {
this.recvBuf = Buffer.alloc(0);
this.uniqId = 1000;
this.msgHandlerMap = new Map();
this.rl = readline.createInterface({
input: process.stdin,
//output: process.stdout
});
this.rl.on("line", (line)=> {
this.parseCmdLine(line);
});
}
selfRegisterMsgHandle(msgName) {
@ -174,6 +182,35 @@ class ClientNet {
}
parseCmdLine(line) {
const params = line.split(' ');
if (params.length <= 0) {
console.log('unknown command');
return;
}
const msgName = params[0];
const msgFields = params.slice(1);
const msgType = this.protoPb.lookupType(msgName);
if (!msgType) {
console.log('unknown command');
return;
}
const msg = {};
for (let i = 0; i < msgType.fieldsArray.length; ++i) {
if (i < msgFields.length) {
const name = msgType.fieldsArray[i].name;
const type = msgType.fieldsArray[i].type;
if (['int32', 'int', 'float', 'double'].indexOf(type) >= 0) {
msg[name] = Number(msgFields[i]);
} else {
msg[name] = msgFields[i];
}
}
}
console.log(msg);
this.sendMsg(msgName, msg);
}
}
module.exports = ClientNet;