最近感觉rpc挺好玩的,就学习了一下,虽说大佬们都说rpc拿不出手,怎么说呢,对小白来说还是比较不错的,本文纯粹分型,各位大佬别吐槽就好,新手的话可以跟着教程一步一步玩一玩。还是拿5代的rui数来举例。
大佬的框架我们直接用,他的教程也很详细 ,js我就直接放文末尾,自行提取
sekiro框架:GitHub - virjar/sekiro: 基于长链接和代码注入的Android private API暴露框架
一个5代的网站:aHR0cDovL2NkYnB3LmNoZW5nZHUuZ292LmNuLw==
简单的以后缀做案例:
将加密函数导入全局
放掉断点执行LSL:
将大佬的服务器地址改成我们本地的:
执行一下:
浏览器中先发个请求看看:
最后协程测试一下高并发:
完全没有问题,感兴趣的小伙伴可以看看,强调一下,只是学习,可以去研究一下大佬写的js。
/*
Copyright (C) 2020 virjar for https://github.com/virjar/sekiro
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
function SekiroClient(wsURL) {
this.wsURL = wsURL;
this.handlers = {};
this.socket = {};
// check
if (!wsURL) {
throw new Error('wsURL can not be empty!!')
}
this.webSocketFactory = this.resolveWebSocketFactory();
this.connect()
}
SekiroClient.prototype.resolveWebSocketFactory = function() {
if (typeof window === 'object') {
var theWebSocket = window.WebSocket ? window.WebSocket : window.MozWebSocket;
return function(wsURL) {
function WindowWebSocketWrapper(wsURL) {
this.mSocket = new theWebSocket(wsURL);
}
WindowWebSocketWrapper.prototype.close = function() {
this.mSocket.close();
}
;
WindowWebSocketWrapper.prototype.onmessage = function(onMessageFunction) {
this.mSocket.onmessage = onMessageFunction;
}
;
WindowWebSocketWrapper.prototype.onopen = function(onOpenFunction) {
this.mSocket.onopen = onOpenFunction;
}
;
WindowWebSocketWrapper.prototype.onclose = function(onCloseFunction) {
this.mSocket.onclose = onCloseFunction;
}
;
WindowWebSocketWrapper.prototype.send = function(message) {
this.mSocket.send(message);
}
;
return new WindowWebSocketWrapper(wsURL);
}
}
if (typeof weex === 'object') {
// this is weex env : https://weex.apache.org/zh/docs/modules/websockets.html
try {
console.log("test webSocket for weex");
var ws = weex.requireModule('webSocket');
console.log("find webSocket for weex:" + ws);
return function(wsURL) {
try {
ws.close();
} catch (e) {}
ws.WebSocket(wsURL, '');
return ws;
}
} catch (e) {
console.log(e);
//ignore
}
}
//TODO support ReactNative
if (typeof WebSocket === 'object') {
return function(wsURL) {
return new theWebSocket(wsURL);
}
}
// weex 鍜� PC鐜鐨剋ebsocket API涓嶅畬鍏ㄤ竴鑷达紝鎵€浠ュ仛浜嗘娊璞″吋瀹�
throw new Error("the js environment do not support websocket");
}
;
SekiroClient.prototype.connect = function() {
console.log('sekiro: begin of connect to wsURL: ' + this.wsURL);
var _this = this;
// 涓峜heck close锛岃
// if (this.socket && this.socket.readyState === 1) {
// this.socket.close();
// }
try {
this.socket = this.webSocketFactory(this.wsURL);
} catch (e) {
console.log("sekiro: create connection failed,reconnect after 2s");
setTimeout(function() {
_this.connect()
}, 2000)
}
this.socket.onmessage(function(event) {
_this.handleSekiroRequest(event.data)
});
this.socket.onopen(function(event) {
console.log('sekiro: open a sekiro client connection')
});
this.socket.onclose(function(event) {
console.log('sekiro: disconnected ,reconnection after 2s');
setTimeout(function() {
_this.connect()
}, 2000)
});
}
;
SekiroClient.prototype.handleSekiroRequest = function(requestJson) {
console.log("receive sekiro request: " + requestJson);
var request = JSON.parse(requestJson);
var seq = request['__sekiro_seq__'];
if (!request['action']) {
this.sendFailed(seq, 'need request param {action}');
return
}
var action = request['action'];
if (!this.handlers[action]) {
this.sendFailed(seq, 'no action handler: ' + action + ' defined');
return
}
var theHandler = this.handlers[action];
var _this = this;
try {
theHandler(request, function(response) {
try {
_this.sendSuccess(seq, response)
} catch (e) {
_this.sendFailed(seq, "e:" + e);
}
}, function(errorMessage) {
_this.sendFailed(seq, errorMessage)
})
} catch (e) {
console.log("error: " + e);
_this.sendFailed(seq, ":" + e);
}
}
;
SekiroClient.prototype.sendSuccess = function(seq, response) {
var responseJson;
if (typeof response == 'string') {
try {
responseJson = JSON.parse(response);
} 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;
}
responseJson['__sekiro_seq__'] = seq;
var responseText = JSON.stringify(responseJson);
console.log("response :" + responseText);
this.socket.send(responseText);
}
;
SekiroClient.prototype.sendFailed = function(seq, errorMessage) {
if (typeof errorMessage != 'string') {
errorMessage = JSON.stringify(errorMessage);
}
var responseJson = {};
responseJson['message'] = errorMessage;
responseJson['status'] = -1;
responseJson['__sekiro_seq__'] = seq;
var responseText = JSON.stringify(responseJson);
console.log("sekiro: response :" + responseText);
this.socket.send(responseText)
}
;
SekiroClient.prototype.registerAction = function(action, handler) {
if (typeof action !== 'string') {
throw new Error("an action must be string");
}
if (typeof handler !== 'function') {
throw new Error("a handler must be function");
}
console.log("sekiro: register action: " + action);
this.handlers[action] = handler;
return this;
}
;
function guid() {
function S4() {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}
var client = new SekiroClient("wss://sekiro.virjar.com/business/register?group=ws-group-2&clientId=" + guid());
client.registerAction("clientTime", function (request, resolve, reject) {
resolve("SekiroTest:" + new Date());
});
var client = new SekiroClient("ws://127.0.0.1:5620/business-demo/register?group=vmRS&clientId=" + guid());
client.registerAction("houzhui_2", function(request, resolve, reject) {
resolve(window.LSL('/es-search/search/72505d2ee79f464dad71e2d201766238?_template=trs/cdcsgyjs_list&_isAgg=1&_pageSize=20&page=3'));
});
提示:qq:2695733665,若是侵权请联系作者删除。