This commit is contained in:
aozhiwei 2022-04-28 09:52:54 +08:00
parent 91f03316a2
commit 3aa216f5f2
4 changed files with 134 additions and 14 deletions

View File

@ -1,5 +1,6 @@
const axios = require('axios').default;
const protobuf = require('protobufjs');
const ws = require('nodejs-websocket');
const ClientNet = require('./clientnet');
function get(url, params) {
return new Promise((resolve) => {
@ -24,20 +25,36 @@ function get(url, params) {
}
async function start() {
const fiendMsg = await protobuf.load('proto/friend/cs_proto.proto');
const fiendMsgId = await protobuf.load('proto/friend/cs_msgid.proto');
console.log(friendMsg, friendMsgId);
const {err, response} = await get(
'https://login-z2-test.cebg.games/webapp/index.php',
{
'c': 'Login',
'a': 'auth',
'gameid': 2006,
'channel': 6513,
'account': '123456',
'openid': '123456'
let loginData = null;
//console.log(err, root);
{
const {err, response} = await get(
'https://login-z2-test.cebg.games/webapp/index.php',
{
'c': 'Login',
'a': 'auth',
'gameid': 2006,
'channel': 6513,
'account': '123456',
'openid': '123456'
});
loginData = response;
console.log(loginData);
}
const clientNet = new ClientNet(
'wss://relation-test.kingsome.cn/friend/websocket',
'proto/friend/cs_proto.proto',
'proto/friend/cs_msgid.proto'
);
await clientNet.init();
await clientNet.connect();
clientNet.on('connect', () => {
console.log('onConnect');
clientNet.sendMsg('CMLogin', {
accountId: loginData['account_id'],
sessionId: loginData['session_id'],
});
console.log(err, response);
});
}
start();

View File

@ -0,0 +1,88 @@
const protobuf = require('protobufjs');
const ws = require('nodejs-websocket');
class ClientNet {
constructor(url, protoPbFile, msgIdPbFile) {
this.url = url;
this.conn = null;
this.protoPbFile = protoPbFile;
this.msgIdPbFile = msgIdPbFile;
this.protoPb = null;
this.msgIdPb = null;
this.cmMsgId = null;
this.smMsgId = null;
this.recvBuf = Buffer.alloc(0);
}
async init() {
{
const {err, root} = await protobuf.load(this.protoPbFile);
this.protoPb = root;
}
{
const {err, root} = await protobuf.load(this.msgIdPbFile);
this.msgIdPb = root;
this.cmMsgId = this.msgIdPb.lookup('CMMessageId_e');
this.smMsgId = this.msgIdPb.lookup('SMMessageId_e');
}
}
async connect() {
this.conn = await ws.connect(this.url);
this.on('binary', this.#onReceive.bind(this));
}
async #onReceive(inStream) {
inStream.on('readable', async () => {
console.log('inStream.readable');
const newData = inStream.read();
if (newData) {
this.recvBuf = Buffer.concat([this.recvBuf, newData]);
await this.#onParsePacket();
}
});
inStream.on('end', () => {
console.log('inStream.end', this.recvBuf.length);
});
inStream.on('close', () => {
console.log('inStream.close');
});
}
async #onParsePacket() {
while (this.recvBuf.length > 12) {
const msgSize = this.recvBuf.readUInt16LE(0);
const msgId = this.recvBuf.readUInt16LE(2);
const seqId = this.recvBuf.readUInt32LE(4);
const magicCode = this.recvBuf.readUInt16LE(8);
const ext = this.recvBuf.readUInt16LE(10);
if (this.recvBuf.length >= 12 + msgSize) {
this.recvBuf = this.recvBuf.slice(12 + msgSize);
}
}
}
on(eventName, cb) {
this.conn.on(eventName, cb);
}
async sendMsg(name, msg) {
const msgType = this.protoPb.lookupType(name);
const msgId = this.cmMsgId.values['_' + name];
const msgPb = msgType.create(msg);
const msgBuf = msgType.encode(msg).finish();
let buf = Buffer.alloc(12);
buf.writeInt16LE(msgBuf.length, 0);
buf.writeInt16LE(msgId, 2);
buf.writeInt32LE(0, 4);
buf.writeUInt8('K'.charCodeAt(0), 8);
buf.writeUInt8('S'.charCodeAt(0), 9);
buf.writeInt16LE(0, 10);
this.conn.sendBinary(Buffer.concat([buf, msgBuf]));
}
}
module.exports = ClientNet;

View File

@ -9,6 +9,7 @@
"version": "1.0.0",
"dependencies": {
"axios": "^0.27.0",
"nodejs-websocket": "^1.7.2",
"protobufjs": "^6.11.2"
}
},
@ -165,6 +166,14 @@
"node": ">= 0.6"
}
},
"node_modules/nodejs-websocket": {
"version": "1.7.2",
"resolved": "https://registry.npmjs.org/nodejs-websocket/-/nodejs-websocket-1.7.2.tgz",
"integrity": "sha512-PFX6ypJcCNDs7obRellR0DGTebfUhw1SXGKe2zpB+Ng1DQJhdzbzx1ob+AvJCLzy2TJF4r8cCDqMQqei1CZdPQ==",
"engines": {
"node": ">=4"
}
},
"node_modules/protobufjs": {
"version": "6.11.2",
"resolved": "https://registry.npmjs.org/protobufjs/-/protobufjs-6.11.2.tgz",
@ -316,6 +325,11 @@
"mime-db": "1.52.0"
}
},
"nodejs-websocket": {
"version": "1.7.2",
"resolved": "https://registry.npmjs.org/nodejs-websocket/-/nodejs-websocket-1.7.2.tgz",
"integrity": "sha512-PFX6ypJcCNDs7obRellR0DGTebfUhw1SXGKe2zpB+Ng1DQJhdzbzx1ob+AvJCLzy2TJF4r8cCDqMQqei1CZdPQ=="
},
"protobufjs": {
"version": "6.11.2",
"resolved": "https://registry.npmjs.org/protobufjs/-/protobufjs-6.11.2.tgz",

View File

@ -6,6 +6,7 @@
"scripts": {},
"dependencies": {
"axios": "^0.27.0",
"nodejs-websocket": "^1.7.2",
"protobufjs": "^6.11.2"
}
}