1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| function Hlclient(wsURL) { this.wsURL = wsURL; this.handlers = {}; this.socket = {};
if (!wsURL) { throw new Error('wsURL can not be empty!!') } this.connect() }
Hlclient.prototype.connect = function () { console.log('begin of connect to wsURL: ' + this.wsURL); var _this = this; try { this.socket["ySocket"] = new WebSocket(this.wsURL); this.socket["ySocket"].onmessage = async function (e) { console.log("send func", await e.data.text()); _this.handlerRequest(e.data); } } catch (e) { console.log("connection failed,reconnect after 10s"); setTimeout(function () { _this.connect() }, 10000) } this.socket["ySocket"].onclose = function () { console.log("connection failed,reconnect after 10s"); setTimeout(function () { _this.connect() }, 10000) }
}; Hlclient.prototype.send = function (msg) { this.socket["ySocket"].send(msg) }
Hlclient.prototype.regAction = function (func_name, func) { if (typeof func_name !== 'string') { throw new Error("an func_name must be string"); } if (typeof func !== 'function') { throw new Error("must be function"); } console.log("register func_name: " + func_name); this.handlers[func_name] = func; return this;
} Hlclient.prototype.handlerRequest = async function (requestJson) { var _this = this; console.log(await requestJson.text()) var result = JSON.parse(await requestJson.text()); if (!result['action']) { this.sendResult('', 'need request param {action}'); return } action = result["action"] var theHandler = this.handlers[action]; if (!theHandler || theHandler == undefined) { this.sendResult(action, 'action not found'); return } try { if (!result["param"]) { theHandler(function (response) { _this.sendResult(action, response); }) } else { theHandler(function (response) { _this.sendResult(action, response); }, result["param"]) }
} catch (e) { console.log("error: " + e); _this.sendResult(action + e); } }
Hlclient.prototype.sendResult = function (action, response) { var responseJson; if (typeof response == 'string') { try { responseJson = JSON.parse(response); console.log() } catch (e) { responseJson = {}; responseJson['data'] = response; } } else if (typeof response == 'object') { responseJson = response; } else { responseJson = {}; responseJson['data'] = response; } if (Array.isArray(responseJson)) { responseJson = { data: responseJson, code: 0 } }
if (responseJson['code']) { responseJson['code'] = 0; } else if (responseJson['status']) { responseJson['status'] = 0; } else { responseJson['status'] = 0; } var responseText = JSON.stringify(responseJson); this.send(action + atob("aGxeX14") + responseText); }
let client = new Hlclient("ws://127.0.0.1:12080/ws?group=shopee&name=details");
client.regAction("id", function (resolve, param) { let url = JSON.parse(param)['url'] window.fetch(url, { "headers": { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0", "Accept": "application/json", "af-ac-enc-dat": "a" }, "method": "GET" }) .then(res => res.json()) .then(res => { console.log(res); resolve(res)
}); })
|