websocketstart()
{
exampleSocket = new WebSocket('ws://127.0.0.1:8000');
exampleSocket.onopen = function() {
// alert('handshake successfully established. May send data now...');
// exampleSocket.send('hello!!!')
};
exampleSocket.onmessage = function(event) {
let result = JSON.parse(event.data);
if(result.error == false)
{
console.log("ERROR : " + result.parent.message);
alert('error');
return;
}
this.wantcallfunction(); //<---- SCRIPT438: Object doesn't support property or method 'wantcallfunction'
return;
};
exampleSocket.onclose = function() {
alert('connection closed');
};
}
wantcallfunction()
{
}
this.wantcallfunction(); // <---- SCRIPT438:对象不支持属性或方法“ wantcallfunction”
还有其他方法可以从onmessage中调用该函数吗?
答案 0 :(得分:0)
使用箭头函数,以便将this
的上下文保留为类。
更改
exampleSocket.onmessage = function(event) {
// `this` is not the class
收件人
exampleSocket.onmessage = (event) => {
// `this` is the class
答案 1 :(得分:0)
static instance : MySocketTest
MySocketTest.instance = this;
exampleSocket.onmessage = function(event) {
MySocketTest.instance.wantcallfunction();
已解决。
没有错误,但是我不知道这是否正确。