Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WX小程序DEVtools 无法使用 #25

Open
userFund opened this issue May 11, 2024 · 9 comments
Open

WX小程序DEVtools 无法使用 #25

userFund opened this issue May 11, 2024 · 9 comments

Comments

@userFund
Copy link

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

@userFund
Copy link
Author

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
Copy link
Owner

jxhczhl commented May 12, 2024

image

@userFund
Copy link
Author

图像

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

@jxhczhl
Copy link
Owner

jxhczhl commented May 12, 2024

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

@zjlhcnlay
Copy link

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

@mrknow001
Copy link

我也是wx is not define

@userFund
Copy link
Author

我也是wx is not defined

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

@userFund
Copy link
Author

我也是wx is not defined

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

@mrknow001
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants