This commit is contained in:
aozhiwei 2022-05-04 00:48:02 +08:00
parent 2b4b0393a7
commit 574f62c47b
4 changed files with 82 additions and 8 deletions

View File

@ -6,6 +6,7 @@ function add(name) {
} }
function init() { function init() {
add('gm');
} }
exports.init = init; exports.init = init;

View File

@ -0,0 +1,51 @@
const ws = require("nodejs-websocket")
const app = require('j7/app');
const http = require('j7/http');
const utils = require('j7/utils');
const cmdHash = {};
let seqId = 0;
let conn = null;
async function test(session, params) {
const msg = {
'url': 'https://game2006api-z1-test.cebg.games/webapp/index.php',
'params': {
'c': 'Ops',
'a': 'selfChecking'
},
'seqId': ++seqId,
'payload': ''
};
const msgText = utils.jsonEncode(msg);
const text = '#' + utils.pad(msgText.length, 7) + msgText;
if (!conn) {
conn = await ws.connect('ws://127.0.0.1:8381');
conn.on('connect', () => {
console.log('connect');
conn.sendText(text);
});
}
if (conn.readyState == conn.OPEN) {
conn.sendText(text);
}
session.rspOk();
}
async function execCmd(session) {
const cmdLine = session.request('cmd').split('.');
const cmd = cmdLine[0];
const params = cmdLine.length > 1 ? cmdLine.slice(1) : [] ;
if (cmdHash[cmd]) {
cmdHash[cmd](session, params);
} else {
session.rspErr(404, 'not found');
}
}
function init() {
app.registerHandler('GM', 'execCmd', execCmd);
cmdHash['test'] = test;
}
exports.init = init;

View File

@ -3,17 +3,19 @@ const ws = require('nodejs-websocket');
const app = require('j7/app'); const app = require('j7/app');
const utils = require('j7/utils'); const utils = require('j7/utils');
const log = require('j7/log'); const log = require('j7/log');
const config = require('j7/config');
const http = require('j7/http');
const BaseService = require('./baseservice'); const BaseService = require('./baseservice');
const metaFactory = require('../metadata/factory'); const metaFactory = require('../metadata/factory');
class WsServer extends BaseService { class WsServer extends BaseService {
async init() { async init() {
const data = ''; let data = '';
const server = ws.createServer((socket) => { const server = ws.createServer((socket) => {
socket.on('text', (str) => { socket.on('text', (str) => {
console.log(str); //console.log(str);
data += str; data += str;
//#0000000 //#0000000
while (data.length >= 8) { while (data.length >= 8) {
@ -21,14 +23,16 @@ class WsServer extends BaseService {
socket.close('error package'); socket.close('error package');
break; break;
} }
const pkgLen = parseInt(data.slice(1, 7), 10); const pkgLen = parseInt(data.slice(1, 8), 10);
if (isNaN(pkgLen)) { if (isNaN(pkgLen)) {
socket.close('error pkgLen'); socket.close('error pkgLen');
break; break;
} }
//console.log(data, pkgLen, data.slice(1,8));
if (data.length >= 8 + pkgLen) { if (data.length >= 8 + pkgLen) {
this.parsePackage(data[8, 8 + pkgLen]); this.parsePackage(socket, data.slice(8, 8 + pkgLen));
data = data.slice(8 + pkgLen); data = data.slice(8 + pkgLen);
console.log(data);
} }
} }
}); });
@ -37,11 +41,29 @@ class WsServer extends BaseService {
console.log('connection close', code, reason); console.log('connection close', code, reason);
}); });
}).listen(3000); }).listen(config('ws_listen_port'));
} }
async parsePackage(data) { async parsePackage(socket, data) {
console.log(data); console.log('parsePackage', data);
const msg = utils.jsonDecode(data);
{
const beginTick = utils.getTickCount();
const {err, data} = await http.get(msg['url'], msg['params']);
const endTick = utils.getTickCount();
if (socket.readyState == socket.OPEN) {
const rspText = utils.jsonEncode({
'seqId': msg['seqId'],
'payload': msg['payload'],
'time_cost': endTick - beginTick,
'err': err,
'data': data
});
const text = '#' + utils.pad(rspText.length, 7) + rspText;
console.log(text);
socket.sendText(text);
}
}
} }
} }

2
third_party/j7 vendored

@ -1 +1 @@
Subproject commit 786102288f4bd7e2184838531fc211803c2ce7e1 Subproject commit 59b3ef222d3468ccb7096a801a43da7d9b158412