135 lines
3.2 KiB
JavaScript
135 lines
3.2 KiB
JavaScript
const net = require("net");
|
|
const { Stick, MaxBodyLen } = require("./lib/stick");
|
|
const { Message } = require("./Message");
|
|
|
|
class TestBase {
|
|
constructor() {
|
|
this.port = 4999;
|
|
this.host = "192.168.100.83";
|
|
// this.host = "62.234.46.11";
|
|
// this.host = "r2.cebggame.com";
|
|
this.stick = new Stick(1024);
|
|
this.stick.setMaxBodyLen(MaxBodyLen["32K"], true);
|
|
|
|
this.client = new net.Socket();
|
|
this.game = new net.Socket();
|
|
|
|
this.stickGame = new Stick(1024);
|
|
this.stickGame.setMaxBodyLen(MaxBodyLen["32K"], true);
|
|
|
|
this.serialno = 0;
|
|
this.serialcbs = {};
|
|
this.onPushed = {};
|
|
|
|
this.client.on("data", (data) => {
|
|
this.client.destroy(); // kill client after server's response
|
|
this.stick.putData(data);
|
|
});
|
|
|
|
this.client.on("error", function (error) {
|
|
console.log("Error: " + error);
|
|
});
|
|
|
|
this.client.on("close", function () {
|
|
// console.log("Connection closed");
|
|
});
|
|
|
|
this.game.on("error", function (error) {
|
|
console.log("Error: " + error);
|
|
});
|
|
this.game.on("close", function () {
|
|
// console.log("Connection closed");
|
|
});
|
|
this.game.on("data", (data) => {
|
|
this.stickGame.putData(data);
|
|
});
|
|
|
|
this.stickGame.onBody((msgId, data) => {
|
|
const len = data.readUInt16LE(0);
|
|
const buf1 = data.subarray(2, 2 + len);
|
|
const msg = JSON.parse(buf1);
|
|
|
|
if (this.serialcbs[msg.serial]) {
|
|
this.serialcbs[msg.serial](msg);
|
|
delete this.serialcbs[msg.serial];
|
|
}
|
|
|
|
if (this.onPushed[msg.route]) {
|
|
this.onPushed[msg.route](msg);
|
|
this.onPushed[msg.route] = null;
|
|
}
|
|
});
|
|
}
|
|
|
|
on(route) {
|
|
return new Promise((resolve, reject) => {
|
|
this.onPushed[route] = (msg) => {
|
|
resolve(msg);
|
|
};
|
|
});
|
|
}
|
|
|
|
sendMessage(route, params, cb) {
|
|
let param = {
|
|
serial: this.serialno,
|
|
route: route,
|
|
params: params,
|
|
};
|
|
this.game.write(
|
|
this.stick.makeData(Message.TYPE_REQUEST, JSON.stringify(param))
|
|
);
|
|
this.serialcbs[this.serialno] = cb;
|
|
this.serialno++;
|
|
}
|
|
|
|
rpc(route, params) {
|
|
return new Promise((resolve, reject) => {
|
|
this.sendMessage(route, params, (msg) => {
|
|
if (msg.code == 200) {
|
|
resolve(msg);
|
|
} else {
|
|
resolve(msg);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
init(uid) {
|
|
return new Promise((resolve, reject) => {
|
|
this.stick.onBody((msgId, data) => {
|
|
const len = data.readUInt16LE(0);
|
|
const buf1 = data.subarray(2, 2 + len);
|
|
const msg = JSON.parse(buf1);
|
|
|
|
data = JSON.parse(buf1);
|
|
|
|
setTimeout(() => {
|
|
this.client.destroy();
|
|
this.game.connect(data.port, data.host, async function () {
|
|
resolve();
|
|
});
|
|
}, 0);
|
|
});
|
|
|
|
this.client.connect(this.port, this.host, () => {
|
|
let param = {
|
|
serial: 123456,
|
|
route: "gate.gateHandler.queryEntry",
|
|
params: {
|
|
uid: uid,
|
|
},
|
|
};
|
|
this.client.write(
|
|
this.stick.makeData(Message.TYPE_REQUEST, JSON.stringify(param))
|
|
);
|
|
});
|
|
});
|
|
}
|
|
|
|
destroy() {
|
|
this.game.destroy();
|
|
}
|
|
}
|
|
|
|
module.exports = TestBase;
|