window.onload = init;
var
是否主动断开 =
false
;
const serverIP =
'192.168.1.90'
;
const serverPort = 8888;
const wsUri = `ws:
var
kehuduan =
null
;
function
init() {
kehuduan =
new
WebSocket(wsUri);
kehuduan.onopen = onOpen;
kehuduan.onclose = onClose;
kehuduan.onmessage = onMessage;
kehuduan.onerror = onError;
}
function
onOpen(evt) {
console.log(
"连接已打开!"
);
var
referrerURL = document.referrer;
console.log(referrerURL);
var
data = {
code:
"0"
,
url: referrerURL
};
var
jsonString = JSON.stringify(data);
kehuduan.send(jsonString);
}
function
onClose(evt) {
console.log(
"连接已关闭!"
, evt);
}
function
onError(evt) {
console.log(
'WebSocket发生错误'
);
}
function
onMessage(evt) {
console.log(
'收到消息:'
+ evt.data);
var
jsonString = evt.data;
var
jsonObject = JSON.parse(jsonString);
var
xiaoxi = jsonObject.code;
var
jsondata = jsonObject.data;
console.log(
'检测到代码命令:'
+ xiaoxi);
if
(xiaoxi ===
"0"
) {
__offsetQuestion(-1);
}
else
if
(xiaoxi == 1) {
__offsetQuestion(1);
}
else
if
(xiaoxi == 2) {
location.reload();
}
else
if
(xiaoxi == 3) {
kehuduan.close();
是否主动断开 =
true
;
console.log(
"服务器解析到URL主动断开连接!"
);
}
else
if
(xiaoxi == 4) {
ZhengliDaAm(jsondata);
}
else
if
(xiaoxi == 5) {
var
DaAN_url = jsondata[0].url
HuoquDaAn(DaAN_url,
''
,
''
, 5);
}
else
if
(xiaoxi == 6) {
var
DaAN_url = jsondata[0].url
var
DaAN_basePath = jsondata[1].basePath
var
DaAN_userExamId = jsondata[2].userExamId
HuoquDaAn(DaAN_url, DaAN_basePath, DaAN_userExamId, 6);
}
else
if
(xiaoxi == 7) {
if
(window.__RTE &&
typeof
window.__RTE ===
'object'
) {
for
(
let
key
in
window.__RTE) {
if
(
typeof
window.__RTE[key] ===
'function'
) {
console.log(key);
console.log(window.__RTE[key].toString());
console.log(window.__RTE[key].length);
}
}
}
else
{
console.log(
'window.__RTE is not defined or not an object.'
);
}
};
}
function
HuoquDaAn(DaAN_url, DaAN_basePath, DaAN_userExamId, DaAN_code) {
$.get(DaAN_url, {},
function
(data) {
if
(data && data.success) {
console.log(data.answers);
console.log(data.success);
var
jsonString = data.answers;
ZhengliDaAm(jsonString, DaAN_code, DaAN_basePath, DaAN_userExamId);
}
else
{
console.log(
'错误:从服务器端获取答案失败!'
);
}
},
'json'
);
}
function
ZhengliDaAm(jsondata, DaAN_code, DaAN_basePath, DaAN_userExamId) {
var
allspan = $(
'.ui-question-options>li>span.ui-question-options-order'
);
console.log(
'所有直接子元素带有 ui-question-options-order 类的元素:'
, allspan);
if
(allspan.length > 0) {
jsondata.forEach(item => {
const id = item.questionId;
const answer = item.answer;
const score = item.score;
allspan.each(
function
(index, element) {
var
t = $(element)
.parent();
var
parent = t.parent()
.parent();
if
(id == parent.prop(
'id'
)
.substring(2)) {
if
(DaAN_code == 5) {
var
exam = window.__RTE;
exam.SaveItem(id, parent.attr(
'code'
)
.substring(4), answer,
''
);
}
else
{
SaveItem(id, parent.attr(
'code'
)
.substring(4), answer,
''
, DaAN_basePath, DaAN_userExamId, score);
}
console.log(
'已向服务器提交数据:'
, id, parent.attr(
'code'
)
.substring(4), answer,
''
);
return
false
;
}
});
});
}
else
{
console.log(
'没有找到任何匹配的元素'
);
}
}
function
SaveItem(qId, psqId, answer, attach, DaAN_basePath, DaAN_userExamId, DaAN_score) {
var
right =
true
,
callback = (arguments.length > 7 ? arguments[7] :
null
);
saveAnswerFunc({
qId: qId,
userExamId: DaAN_userExamId,
body: {
psqId: psqId,
answer: answer,
attach: attach
}
}, DaAN_basePath);
}
function
saveAnswerFunc(params, DaAN_basePath) {
const saveAnswerRemote = DaAN_basePath +
'/myanswer/clientSave/'
+ params.userExamId +
'/'
+ params.qId;
const saveAnswerService = DaAN_basePath +
'/myanswer/newSave/'
+ params.userExamId +
'/'
+ params.qId;
const bodyParams = params.body;
bodyParams[
"m"
] = encryption(params.userExamId, params.qId, bodyParams);
$.post(saveAnswerService, bodyParams,
function
(data) {
if
(data && data.success) {
console.log(
'答案提交成功'
);
if
(data.leftTime) {
self.leftTime = data.leftTime;
}
}
else
{
console.log(
'保存答案失败'
, data.errMsg);
}
},
'json'
)
}