My web based WebSocket client connects to my Java server this way:
this.ws = new WebSocket(this.url);
this.ws.onmessage = m => {
let data;
try {
data = JSON.parse(m.data);
} catch (e) {
// Log error
return;
}
doALotOfStuff(data);
};
But then sometimes I get this error in Chrome:
WebSocket connection to 'myServerUrl' failed: Received start of new message but previous message is unfinished.
I found the related source code in Chromium and some discussion about it but this is all about continuation frames and OpCode, a much lower API than the one I have access to from what I understood.
So does this mean I received a message while I was processing the previous one? Meaning I have to call setTimeout(...,0) to handle all my messages asynchronously? But then everybody should do it so why does the browser not handle this?
I have a similar error in Firefox:
The connection to myServerUrl was interrupted while the page was loading.
Which is more confusing I guess because I am not reloading a page, it happens in the middle of a websocket session.
So I guess it might not be Chromium fault but then how do I handle it?
Or am I doing something wrong when sending the messages? I use javax-websocket-api-1.0 and I simply do:
try (Writer writer = session.getBasicRemote().getSendWriter()) {
JacksonSerializer.serialize(object, writer);
} catch (final SerializerException | IOException e) {
throw ...;
}
I close the writer everytime I write an object in it so this looks fine to me.
This was indeed due to a bug in the server code. The code I quoted is correct but was not thread-safe so I wrote concurrently into the same session. I fixed it by synchronizing it on the session.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.