1
    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: Object doesn't support property or method 'wantcallfunction'

Is there any other way to call the function from within onmessage?

0

2 Answers 2

7

Use arrow functions so you keep context of this as the class.

Change

exampleSocket.onmessage = function(event) {
    // `this` is not the class

To

exampleSocket.onmessage = (event) => {
    // `this` is  the class
Sign up to request clarification or add additional context in comments.

1 Comment

It works well within the script. Just call function in another script in wantcallfunction (). mywebsockt.ts ㄴ wantcallfunction() -> [other.ts] othercallback() other.ts ㄴothercallback -> otherfunction() // <- SCRIPT5007: Unable to get property 'otherfunction' of undefined or null reference
0
static instance : MySocketTest 

MySocketTest.instance = this;

exampleSocket.onmessage = function(event) {
MySocketTest.instance.wantcallfunction();

Resolved.

There is no error, but I do not know if this is correct.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.