game2006go/tools/robot/testcase.js
aozhiwei ab0dafc260 1
2023-08-09 18:53:34 +08:00

92 lines
2.0 KiB
JavaScript

const axios = require('axios').default;
const ClientNet = require('./clientnet');
function getTickCount() {
return Math.floor((new Date()).getTime() / 1);
}
function httpGet(url, params) {
return new Promise((resolve) => {
const ret = {
err: null,
response: null,
};
axios({
method: 'get',
url: url,
timeout: 1000 * 5,
params: params,
responseType: 'text'
}).then((response) => {
ret.response = response.data;
resolve(ret);
}).catch((error) => {
ret.err = error;
resolve(ret);
});
});
}
async function sleep(timeout) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve();
}, timeout);
});
}
class TestCase {
constructor(url) {
this.relationUrl = 'ws://127.0.0.1:7132';
this.loginData = null;
this.relationWs = new ClientNet(
this.relationUrl,
'proto/cs_proto.proto',
'proto/cs_msgid.proto'
);
}
async init() {
await this.step1();
await this.step2();
while (true) {
await sleep(1000 * 3);
}
}
async step1() {
const startTick = getTickCount();
const {err, response} = await httpGet(
'https://login-test.kingsome.cn/webapp/index.php',
{
'c': 'Login',
'a': 'auth',
'gameid': 1006,
'channel': 6000,
'account': 'test1',
'openid': 'test1'
});
this.loginData = response;
const endTick = getTickCount();
console.log('login auth@Login', endTick - startTick, String(err), response);
}
async step2() {
await this.relationWs.init();
await this.relationWs.connect();
this.relationWs.on('connect', () => {
console.log('relation onConnect');
this.relationWs.accountId = this.loginData['account_id'];
this.relationWs.sessionId = this.loginData['session_id'];
this.relationWs.sendMsg('CMLogin', {
account_id: this.loginData['account_id'],
session_id: this.loginData['session_id'],
});
});
}
}
module.exports = TestCase;