[java] Same topic, new problem!!

Started by
11 comments, last by TreizeSG 20 years, 8 months ago
quote:Original post by TreizeSG
RMI isn''t a two-way street is it? Clients can make a connection to the server, but the server can''t talk to other clients, can it?


You are correct that RMI is not a two-way street, sort of. What you are missing is that methods can have a return type. So what you can do is set up a thread to poll the server every second or whatever and get a String back from the server with the latest chat messages. Something like:
class MyThread extends Thread {...public void run() {  while(!done) {    String message= chatClassRMIInstance.update("username");    if(message!=null) {      //update client area    }    try {      Thread.sleep(1000);    }    catch(InterruptedException e) {      e.printStackTrace();    }  }}...}



First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
Advertisement
Thanks CJ, that idea worked really well, however I have a few questions about it.

First, how would the computer tell which chat updates were "the latest" without reprinting the entire chatlog over again? Wouldn''t it be a bit inefficient to reprint the same thing in the chatbox over and over again, even if there was nothing new? I was thinking that the server could keep a StringBuffer and purge it every second, so that all the clients are guaranteed to get the messages, and you don''t have to keep reprinting a 100 line chatlog every second.

Secondly, although I''m able to display all of the fighters on the screen, regardless of what comp their playing from, no one can move their characters. It doesn''t take a friggin programmer to figure that this is a problem. I know this involves... the dreaded... MULTITHREADING!!! Can I start this thread separately from the main() thread in the JavaRPG class?
Project ARPEG: Product, Darkness SeigeThe seige begins...
For each message that comes in, keep it in a Vector

Vector messageList=new Vector();public void processMessage(String newMsg) {  messageList.add(newMessage);}


Then keep a pointer for each user that points to which message was the last one they retreived. When tehy asked for the latest messages, concatenate all the new ones together and update the pointer.

If you have a main game loop somewhere, you could start the chat thread before you enter the main loop, or you could build the updates as part of the loop.


First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]

This topic is closed to new replies.

Advertisement