GainLos
GainLos

Reputation: 3

JavaScript Websocket ws.onmessage Error Handle lead to disconnected

I have wrote a function like

ws.onmessage = function (event) {
        let message = event.data;
        let data = JSON.parse(message);
        parseWSData(data);
}

if the message is like '{ "aa":11,"bb":22}', it works correctly

but, if the server send two message in very short time, the message will be like '{"aa":11,"bb":22}{"cc":33,"bb":22}' , which is like two JSON strings are connected.
Then, the JSON.parse will throw a error, and the websocket will throw an error and disconnect.

I have tried to use try-catch when I using JSON.parse. However, even I catch the Error, the WS will also be disconnected.

The code is {1006 ''}

I asked GPT-4 , but his suggestion doesn't work

I want to know how can I handle the '{"aa":11,"bb":22}{"cc":33,"bb":22}' so that the websocket can correctly work.

·············

3.25 Refresh

Now I use try-catch like this

    ws.onmessage = function (event) {
        let message = event.data;
        let data = {};
        try {
            let messages = message.split("}{");
            if (messages.length === 1) {

                data = JSON.parse(message);
                parseWSData(data);
            } else {
                for (let i = 0; i < messages.length; i++) {
                    if (i === 0) {
                        try {
                            data = JSON.parse(messages[i] + "}");
                        } catch (e) {
                            console.log(e);
                            console.log("Error parsing JSON message:" + messages[i] + "}");
                        }
                    } else if (i === messages.length - 1) {
                        try {
                            data = JSON.parse("{" + messages[i]);
                        } catch (e) {
                            console.log(e);
                            console.log("Error parsing JSON message:{" + messages[i]);
                        }
                    } else {
                        try {
                            data = JSON.parse("{" + messages[i] + "}");
                        } catch (e) {
                            console.log(e);
                            console.log("Error parsing JSON message:{" + messages[i] + "}");
                        }
                    }
                    parseWSData(data);
                }
            }
        } catch (e) {
            console.log(e);
            console.log("Error For On Message:" + message);
        }
    };

JSON.parse will tell me he recive something like '}' or '' and return error. More badly, this bug always occur when several minutes after the project start runnnng. Which is not very easy to debug. I was so depressed, so I finally change the backend server from sync to non-sync, then I will not recieve some message with 2 JSON str.

Still I can't handle the error of JSON.parse, but I change the backend so that there will not be msg with 2 JSON str, and my project can correctly work.

Thank you all guys for helping me in benefits!

Upvotes: 0

Views: 84

Answers (1)

kiamehr hosseini
kiamehr hosseini

Reputation: 54

You can use try, catch like this:

ws.onmessage = function (event) {
    let message = event.data;
    let data = {};
    try{
        data = JSON.parse(message);
    }
    catch {
        return false;
    }
    parseWSData(data);
}

Or

ws.onmessage = function (event) {
    let message = event.data;
    if((message.match(/{/g) || []).length  > 1)
        return false;
    let data = JSON.parse(message);
    parseWSData(data);
}

Upvotes: 0

Related Questions