165 lines
4.2 KiB
JavaScript
165 lines
4.2 KiB
JavaScript
const net = require("net");
|
|
const { Stick, MaxBodyLen } = require("./lib/stick");
|
|
const port = 4999;
|
|
const host = "192.168.100.173";
|
|
const { Message } = require("./Message");
|
|
|
|
const stick = new Stick(1024);
|
|
stick.setMaxBodyLen(MaxBodyLen["32K"], true);
|
|
|
|
var client = new net.Socket();
|
|
var game;
|
|
|
|
let _cb_ready_promise = null;
|
|
|
|
let _cb_ready = null;
|
|
let export_ready = (cb) => {
|
|
_cb_ready_promise = new Promise((resolve, reject) => {
|
|
_cb_ready = cb;
|
|
});
|
|
return _cb_ready_promise;
|
|
|
|
}
|
|
|
|
client.connect(port, host, function () {
|
|
console.log("Connected");
|
|
let param = {
|
|
serial: 123456,
|
|
route: "gate.gateHandler.queryEntry",
|
|
params: {
|
|
uid: "6516_2006_0xef59f6cc4d190a0ae576c46d4583e92b61174340",
|
|
},
|
|
};
|
|
client.write(stick.makeData(Message.TYPE_REQUEST, JSON.stringify(param)));
|
|
});
|
|
|
|
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");
|
|
});
|
|
|
|
stick.onBody((msgId, data) => {
|
|
const len = data.readUInt16LE(0);
|
|
const buf1 = data.subarray(2, 2 + len);
|
|
const msg = JSON.parse(buf1);
|
|
console.log(msgId, msg);
|
|
data = JSON.parse(buf1);
|
|
setTimeout(() => {
|
|
const stick = new Stick(1024);
|
|
stick.setMaxBodyLen(MaxBodyLen["32K"], true);
|
|
game = new net.Socket();
|
|
let step = 0;
|
|
let serialno = 0;
|
|
let serialcbs = {};
|
|
let onPushed = {};
|
|
|
|
onPushed["onChat"] = (msg) => {
|
|
console.log(msg);
|
|
};
|
|
let _ready;
|
|
|
|
function on(route) {
|
|
return new Promise((resolve, reject) => {
|
|
onPushed[route] = (msg) => {
|
|
resolve(msg);
|
|
};
|
|
});
|
|
}
|
|
|
|
game.connect(data.port, data.host, async function () {
|
|
console.log("Connected");
|
|
if (_cb_ready_promise) {
|
|
_cb_ready_promise.resolve();
|
|
}
|
|
|
|
let msg;
|
|
msg = await rpc("connector.entryHandler.entry", { uid: "6516_2006_0x44b9bd78ed5e9d00bd83f270b3d97909e8c904dc" });
|
|
console.log(msg);
|
|
msg = await rpc("chat.chatHandler.joinWorldChannel", {});
|
|
console.log(msg);
|
|
msg = await rpc("chat.chatHandler.worldChat", { content: "hello" });
|
|
console.log(msg);
|
|
msg = await rpc("friend.friendHandler.findUser", { name: "Xiao" });
|
|
console.log(msg);
|
|
|
|
msg = await rpc("guild.guildHandler.createGuild", { guildName: "newGuild1", logo: "test001"});
|
|
console.log(msg);
|
|
|
|
msg = await rpc("guild.guildHandler.listGuild");
|
|
console.log(msg);
|
|
|
|
msg = await rpc("friend.friendHandler.sendFriendRequest", { fid: "6513_2006_md9nVaGIooStTM1D56NLblLosFhWhgxB"});
|
|
console.log(msg);
|
|
|
|
msg = await rpc("friend.friendHandler.acceptFriendRequest", { fid: "6513_2006_md9nVaGIooStTM1D56NLblLosFhWhgxB"});
|
|
console.log(msg);
|
|
|
|
msg = await rpc("friend.friendHandler.deleteFriend", { fid: "6513_2006_md9nVaGIooStTM1D56NLblLosFhWhgxB"});
|
|
console.log(msg);
|
|
|
|
game.destroy();
|
|
});
|
|
|
|
game.on("error", function (error) {
|
|
console.log("Error: " + error);
|
|
});
|
|
game.on("close", function () {
|
|
console.log("Connection closed");
|
|
});
|
|
game.on("data", function (data) {
|
|
stick.putData(data);
|
|
});
|
|
|
|
stick.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);
|
|
}
|
|
});
|
|
|
|
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);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}, 500);
|
|
});
|
|
|
|
module.exports = {
|
|
ready: export_ready,
|
|
} |