Threading, web sockets and JME

I’m using a web socket in my little JME application to receive data updates from a web server. Weird I know, but bear with me! I create the web socket in a new thread but obviously that means I can’t easily access the scenegraph from the websocket thread without synchronisation. So I tried using a ConcurrentLinkedQueue; the websocket thread adds messages to the queue and the main application thread removes them inside the simpleUpdate function. This function then updates the scene graph.

However, it looks like I’m losing updates as what I see visually is not what should be happening. Objects aren’t being moved, they disappear at random and I only have a fraction of the objects visible or existing in the scenegraph that I should have.

So is this design a bad idea? Should I store a reference to the root node in the websocket thread and use synchronisation? The websocket thread is the only one that modifies the scenegraph. The user is just a spectator.

I’m using this library to implement my web socket client:

GitHub - TooTallNate/Java-WebSocket: A barebones WebSocket client and server implementation written in 100% Java.


Have you tried using app.enqueue() to process the incoming data? That’s what I do and it works fine…

Thanks I’ll take a look.

I may be having a blonde moment but I don’t see how this will work for me. If I understand correctly, a Callable is a “task” that expects to finish whereas my websocket client extends a base client and provides functions like onMessage. So my websocket clients is a long living instance.

Thanks - I have it working. I realised afterwards that I was looking at it from the wrong angle. I enqueue the callable in the websocket thread.

1 Like