Thursday, 17 December 2020

WebSocket Closes with Protocol Error 1002

I m implementing WebSocket messages command-line client. I have checked that this error corresponds to the problem with the protocol. I upgraded ws to the newest 7.4.1. At backend I use Spring Boot Websockets at version 2.3.4.RELEASE.

The 2 main causes of this are said to be packet loss or malformed messages. I have made some checks to check those but none seem valid. The messages I test are small so it shouldn't be the case with message size. The connection is fully on localhost. I test the solution with 3 users and sometimes I get this error sometimes not.

Can someone help me figure out how to get rid of this type of error?

Here is the code I use for client to send messages:

async function test(number_of_messages, break_between_messages) {
const websocket = new WebSocket(url...)

websocket.on('message', function incoming(data) {
    console.log(getMessage("Received", data))
});

websocket.on('close', function(data) {
    console.log('Disconnected!!!! ' + data.toString());
});

const opened = await connection(websocket)

//Wait 5 seconds
await sleep(5_000);

if (opened) {
    for (i = 0; i < number_of_messages; i++) {

        for (const chatId of chatIds) {
            let content = i.toString() + " from " + user;
            let msg = JSON.stringify({
                "chatId": chatId,
                "author": user,
                "content": content
            })
            websocket.send(msg)

            let message = getMessage("Sent", msg)
            console.log(message)
        }

        await sleep(break_between_messages);
    }

} else {
    console.log("ERROR on Opening Connection")
    return
}

// Wait 1 minute
await sleep(60_000);
websocket.close()

}



from WebSocket Closes with Protocol Error 1002

No comments:

Post a Comment