powered by Jive Software

Problem using createChat method from XMPPConnection. Program hangs

I am currently attempting to implement a chat program by using JFrame. I have the gui set up and everything, and when i had this in the main method it worked fine:

// try {

// con= new XMPPConnection(“im167”,5222);

// con.login(“test”, “test”);

//

// chat=con.createChat(“phil@im167/Smack”);

// textarea.append(“Connection established with phil@im167\n\n”);

//

// while(true) {

// Message message = chat.nextMessage();

// textarea.append(“phil@im167: “message.getBody()”\n”);

// }

//

// }catch (XMPPException e) {

// e.getXMPPError();

// System.out.println(“Failed”);

// }

Here is a pic of my gui for reference:

http://www.calpoly.edu/~pchoi/chat.JPG

Now when I click “update” it updates the list with users currently logged into the server. (For some reason it lists all users who are registered with the server and not only those who are online, but that’'s a bug for later!)

When I highlight a user, and then click connect, I want to be able to connect to the user. I have a method that is responsible for connecting users.

public void connectToUser(boolean firstConnect) throws IOException {

if (firstConnect == true) { //for initial connection to server

try {

con= new XMPPConnection(“im167”,5222);

con.login(“test”, “test”);

textarea.append(“Connection Successful\n”);

}catch (XMPPException e) {

e.getXMPPError();

}

}

else {

//Connecting to another user to chat

chat=con.createChat(“phil@im167/Smack”); //causes program to hang!!!

textarea.append(“Connection established with "userSelected"\n\n”);

while(true) {

Message message = chat.nextMessage();

textarea.append(userSelected*": “*message.getBody()+”\n");

}

}

}

This call:

chat=con.createChat(“phil@im167/Smack”);

causes my program to hang. Does anyone have any ideas on why it would cause my program to hang? I’'ve been stumped on this for hours!

I greatly appreciate your time and help!

I need a little more context of your program. Which thread are you running your program on… is it the smack read/write thread? In other words where is this method run from. Another point, though I don’‘t think this is the origin of your problem, is that you are updating Swing objects not on the swing thread. You’'ll want to do a SwingUtilities.invokeLater when utilizing any and all swing objects.

Alex

I am pretty new to this.

I am not using threads. Can you please give me an example of how I would use threads in this chat program?

Here is a working version of my program:

http://www.calpoly.edu/~pchoi/ChatClient.java

Please feel free to run it and test it. I appreciate the fast response.

Hi Phil,

are you creating a new connection for every user you want to chat with instead of using #createChat() ?

LG

Hi,

does[code]

public void connectToUser(boolean firstConnect) throws IOException {

if (firstConnect == true) {

try {

con= new XMPPConnection(“im167”,5222);

con.login(“test”, “test”);

textarea.append(“Connection Successful\n”);

}catch (XMPPException e) {

e.getXMPPError();

  1. TODO

return;

}

}

  1. TODO else {

  2. TODO connect to user should be called after login

//connect to user

/code help you a little bit?

LG

Hi,

I’‘d also remove firstConnect and make connectToUser() to check if a connection is established and available and if this is not the case close the old one if any and establish a new one. Doing it this way you’'ll have also a reconnect feature.

LG

basically the purpose of the boolean firstConnect, is if it’'s true, it will create a connection. later, the “connectToUser” method is called again, but with a false flag, to indicate that a connection is already made.

In other words, I call connectToUser with a true flag when the GUI is created…me as a user is successfully connected.

Then when I click the “Connect” button on the GUI, connectToUser is called again, but witha false flag so it just requests the chat. Then that is where the program hangs.

So I should implement a connection listener, but I don’‘t think that’'s the root of my problem.

Please let me know if I can clarify anything for you, and than you for your help.

Hi Phil,

the problem you have is that you are not using threads. If you create a thread dump (see http://www.jivesoftware.org/community/entry.jspa?externalID=521&categoryID=20 ) you may see that the AWT-EventQueue is locked. The fireActionPerformed was executed successfully but the while(true) loop locks the queue.

LG