0

Kind of a "noob" problem here.

I have a small application to write (like a simple game). There is server-side and client-side. It has to use websockets as the way of communication. Server has a server class (with main() that starts the server) as well as server endpoint class. However, the game is not turn based, but real time based. So the server has to do certain computations every "tick" b/c of the dynamic field. I assume that Threads would suit well in this case, but I don't know how to put threads with this kind of server.

As I can see, the only thing that can receive/send messages is endpoint. If I make it implement Runnable and pause every 0.5 of a sec, it won't accept messages during that pause time. If I define a different class for that purpose, I have no idea how I start it inside of an endpoint and make a way for them to communicate. Does anyone have any suggestions/info/links/anything that may help?

Thank you in advance.

  • Objects can't be paused. Threads can be. Just because one thread sleeps in someObject.foo() doesn't mean other threads can't call any method of someObject. So I don't see why an endpoint would not accept message while a background thread is sleeping. – JB Nizet May 08 '15 at 21:49
  • So the thing like : class ServEndPoint implements Runnable { @OnMessage public void onMessage(Session peer, Message msg) throws EncodeException { //do smth} public void run() { if(smth) {this.sleep(x);} } } won't pause the whole thing for x? (Unfortunately, I don't know how to make a new line in comments for readability) – Lily Smith May 08 '15 at 22:01
  • No. That wouldn't compile. And if you used Thread.sleep() instead of this.sleep() to make it compile, that would make the *current thread* to sleep for x millis, as the javadoc explains. Read the tutorial on concurrency, because you're missing fundamental multi-threading concepts. – JB Nizet May 08 '15 at 22:04
  • In this case, I don't understand how websockets work, and how to make them work with at least a separate thread that works on it's own. – Lily Smith May 08 '15 at 22:11

1 Answers1

0

Server endpoint will continuously receive data from client side. All you have to do is to process that data in some other thread. You can define a different class for that purpose (a thread). This thread class will have two different queues.

In queue - to receive data from the endpoint

Out queue - to send data to the endpoint (You can use ConcurrentLinkedQueue for that. more help -> How to use ConcurrentLinkedQueue?)

Start this processing thread inside the endpoint. when endpoint receives data, put them into the In Queue. Continuously listen to the Out Queue and send that data again to the client side.

Endpoint code

@OnMessage
public void onMessage(String message,Session peer) throws IOException{
     processingThread t = new processThread(peer);
     t.inQueue.add(data);
     t.start();
     String s;
     //listen to the Out Queue
     while (true) {
         while ((s =t.outQueue.poll()) != null) {
                    peer.getBasicRemote.sendText(dataToBeSent);                          
         }
     }
 }

processingThread Code

public class processingThread extends Thread{
    public ConcurrentLinkedQueue<String> inQueue = new ConcurrentLinkedQueue<String>();
    public ConcurrentLinkedQueue<String> outQueue = new ConcurrentLinkedQueue<String>();

    public void run(){
        //listen to in queue and process
        //after processing put to the out queue
    }
}

Hope this will help :)

Community
  • 1
  • 1
Pubz
  • 11
  • 2