在网络请求里更新外部UI

刚刚接触cocos,不是很了解在网络的请求里想要修改UI的话cocos应该怎么操作么?ws的请求好像是异步的,不可以在异步里直接调用方法,提示refreshUI方法undefined。
start() {
var ws = new WebSocket(“ws://localhost:8080”);

    ws.onopen = function (event) {
        console.log("onOpen");
    };
    ws.onmessage = function (event) {
        refreshUI(event.data);
    };
    ws.onerror = function (event) {
        console.log("onError");
    };
    ws.onclose = function (event) {
        console.log("closed.");
    };
},

文字缩进4格

手动置顶。。。

换箭头函数试下;

像this的问题吧 要么bind(this) 要么用箭头函数()=>{}

1赞

成员函数要this.refreshUI(event.data);全局函数或局部函数才不需要this.
用了this可能也会存在指向错误问题,建议把function(){}改成()=>{}箭头函数

感谢各位老板的热心解答,问题已经解决了。解决方案如下:
也是通过通知的形式来更新UI,可以参考官方文档。或者直接使用第三方的npm插件类似onfire
https://docs.cocos.com/creator/manual/zh/scripting/events.html
还有就是方法undefined我是放在cc.class外面就可以了