164 lines
3.8 KiB
JavaScript
164 lines
3.8 KiB
JavaScript
const net = require("net");
|
|
const { Stick, MaxBodyLen } = require("./lib/stick");
|
|
const { Message } = require("./Message");
|
|
|
|
const port = 4999;
|
|
// const host = "192.168.100.173";
|
|
// const host = "62.234.46.11";
|
|
// const host = "r2.cebggame.com";
|
|
const host = "192.168.100.83";
|
|
const test_uuid = "6516_2006_0xef59f6cc4d190a0ae576c46d4583e92b61174340";
|
|
// const test_uuid = "6516_2006_0xa5069f54f2ed021e0ac47dbb385420a0d5f41be3";
|
|
// const test_uuid = "6517_2006_0_104162729566475397176";
|
|
const stick = new Stick(1024);
|
|
stick.setMaxBodyLen(MaxBodyLen["32K"], true);
|
|
|
|
var client = new net.Socket();
|
|
var game = new net.Socket();
|
|
|
|
const stickGame = new Stick(1024);
|
|
stickGame.setMaxBodyLen(MaxBodyLen["32K"], true);
|
|
|
|
client.on("data", function (data) {
|
|
client.destroy(); // kill client after server's response
|
|
stick.putData(data);
|
|
});
|
|
|
|
client.on("error", function (error) {
|
|
console.log("Error: " + error);
|
|
});
|
|
|
|
client.on("close", function () {
|
|
// console.log("Connection closed");
|
|
});
|
|
|
|
game.on("error", function (error) {
|
|
console.log("Error: " + error);
|
|
});
|
|
game.on("close", function () {
|
|
// console.log("Connection closed");
|
|
});
|
|
game.on("data", function (data) {
|
|
stickGame.putData(data);
|
|
});
|
|
|
|
let serialno = 0;
|
|
let serialcbs = {};
|
|
let onPushed = {};
|
|
|
|
function on(route) {
|
|
return new Promise((resolve, reject) => {
|
|
onPushed[route] = (msg) => {
|
|
resolve(msg);
|
|
};
|
|
});
|
|
}
|
|
|
|
function sendMessage(route, params, cb) {
|
|
let param = {
|
|
serial: serialno,
|
|
route: route,
|
|
params: params,
|
|
};
|
|
game.write(stick.makeData(Message.TYPE_REQUEST, JSON.stringify(param)));
|
|
serialcbs[serialno] = cb;
|
|
serialno++;
|
|
}
|
|
|
|
function rpc(route, params) {
|
|
return new Promise((resolve, reject) => {
|
|
sendMessage(route, params, (msg) => {
|
|
if (msg.code == 200) {
|
|
resolve(msg);
|
|
} else {
|
|
resolve(msg);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
stickGame.onBody((msgId, data) => {
|
|
const len = data.readUInt16LE(0);
|
|
const buf1 = data.subarray(2, 2 + len);
|
|
const msg = JSON.parse(buf1);
|
|
|
|
if (serialcbs[msg.serial]) {
|
|
serialcbs[msg.serial](msg);
|
|
delete serialcbs[msg.serial];
|
|
}
|
|
|
|
if (onPushed[msg.route]) {
|
|
onPushed[msg.route](msg);
|
|
}
|
|
});
|
|
|
|
beforeAll(() => {
|
|
return new Promise((resolve, reject) => {
|
|
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(() => {
|
|
client.destroy();
|
|
game.connect(data.port, data.host, async function () {
|
|
resolve();
|
|
});
|
|
}, 0);
|
|
});
|
|
|
|
client.connect(port, host, function () {
|
|
let param = {
|
|
serial: 123456,
|
|
route: "gate.gateHandler.queryEntry",
|
|
params: {
|
|
uid: test_uuid,
|
|
},
|
|
};
|
|
client.write(stick.makeData(Message.TYPE_REQUEST, JSON.stringify(param)));
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("client", () => {
|
|
let msg;
|
|
|
|
test("entry", async () => {
|
|
msg = await rpc("connector.entryHandler.entry", { uid: test_uuid, });
|
|
console.log(msg);
|
|
expect(msg.code).toBe(200);
|
|
});
|
|
|
|
test("join", async () => {
|
|
msg = await rpc("chat.chatHandler.joinWorldChannel", {});
|
|
console.log(msg);
|
|
expect(msg.code).toBe(200);
|
|
});
|
|
|
|
test("chat", async () => {
|
|
msg = await rpc("chat.chatHandler.worldChat", { content: "hello" });
|
|
console.log(msg);
|
|
expect(msg.code).toBe(200);
|
|
});
|
|
|
|
test("onChat", async () => {
|
|
rpc("chat.chatHandler.worldChat", { content: "hello2" });
|
|
msg = await on("onChat")
|
|
console.log(msg);
|
|
expect(msg.code).toBe(200);
|
|
});
|
|
|
|
test("find User", async () => {
|
|
msg = await rpc("friend.friendHandler.findUser", { name: "6513_2006_md9nVaGIooStTM1D56NLblLosFhWhgxB3", last_idx: 0, limit: 5 });
|
|
console.log(JSON.stringify(msg, null, 2));
|
|
expect(msg.code).toBe(200);
|
|
});
|
|
|
|
test('end', async()=>{
|
|
game.destroy();
|
|
})
|
|
|
|
});
|