jxhczhl / JsRpc

远程调用(rpc)浏览器方法,免去抠代码补环境
1.16k stars 297 forks source link

WX小程序DEVtools 无法使用 #25

Open userFund opened 4 months ago

userFund commented 4 months ago

VM105810:1 WebSocket connection to 'ws://127.0.0.1:12080/ws?group=zzz' failed: 报错

userFund commented 4 months ago

wx 小程序JS 写法 出问题自己优化

function Hlclient(wsURL) { this.wsURL = wsURL; this.handlers = { _execjs: function (resolve, param) { var res = eval(param) if (!res) { resolve("没有返回值") } else { resolve(res) }

    }
};
this.socket = undefined;
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 { wx.connectSocket({"url":this.wsURL}); wx.onSocketMessage(message => { _this.handlerRequest(message.data) }) } catch (e) { console.log("connection failed,reconnect after 10s"); setTimeout(function () { _this.connect() }, 10000) } wx.onSocketClose(() => { console.log('WebSocket 已断开') }) wx.onSocketOpen(() => { console.log('WebSocket 已连接') })

wx.onSocketError(error => {
    console.error('socket error:', error)
})

}; Hlclient.prototype.send = function (msg) { wx.sendSocketMessage({ data: 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 true

}

//收到消息后这里处理, Hlclient.prototype.handlerRequest = function (requestJson) { var _this = this; try { var result = JSON.parse(requestJson) } catch (error) { console.log("catch error", requestJson); result = transjson(requestJson) }

if (!result['action']) {
    this.sendResult('', 'need request param {action}');
    return
}
var action = result["action"]
var theHandler = this.handlers[action];
if (!theHandler) {
    this.sendResult(action, 'action not found');
    return
}
try {
    if (!result["param"]) {
        theHandler(function (response) {
            _this.sendResult(action, response);
        })
        return
    }
    var param = result["param"]
    try {
        param = JSON.parse(param)
    } catch (e) {}
    theHandler(function (response) {
        _this.sendResult(action, response);
    }, param)

} catch (e) {
    console.log("error: " + e);
    _this.sendResult(action, e);
}

}

Hlclient.prototype.sendResult = function (action, e) { if (typeof e === 'object' && e !== null) { try { e = JSON.stringify(e) } catch (v) { console.log(v)//不是json无需操作 } } this.send(action + atob("aGxeX14") + e); }

function transjson(formdata) { var regex = /"action":(?.?),/g var actionName = regex.exec(formdata).groups.actionName stringfystring = formdata.match(/{..data..:...\w+..:\s...*?..}/g).pop() stringfystring = stringfystring.replace(/\"/g, '"') paramstring = JSON.parse(stringfystring) tens = {"action": + actionName + ,"param":{}} tjson = JSON.parse(tens) tjson.param = paramstring return tjson }

jxhczhl commented 4 months ago

image

userFund commented 4 months ago

图像

devtools 能用谷歌的么 微信的太垃圾了

jxhczhl commented 4 months ago

应该不能吧,wx的f12都是特殊方法开的 (很多东东和浏览器不一样的) 还有几率封号 咋能用谷歌呢。没玩过这个

zjlhcnlay commented 2 months ago

小程序出现 wx is not define 如何解决呢

mrknow001 commented 1 month ago

我也是wx is not define

userFund commented 1 month ago

我也是wx is not defined

作用域不一样 上下文目标不一样 不要在top中调用 要在appCon 那个页面 wx不在top中

userFund commented 1 month ago

我也是wx is not defined

作用域不一样 上下文目标不一样 不要在top中调用 要在appCon 那个页面 wx不在top中

mrknow001 commented 1 month ago

这个问题解决了,但一直timeout